-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.go
More file actions
79 lines (65 loc) · 1.91 KB
/
Copy pathprotocol.go
File metadata and controls
79 lines (65 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"sync"
"github.com/vmihailenco/msgpack/v5"
)
// Frame types in the response stream.
const (
frameStdout = "stdout"
frameStderr = "stderr"
frameLog = "log"
frameExit = "exit"
)
// Verb names in the request.
const (
verbRun = "run"
)
// Request is sent once by the client at the start of a connection.
type Request struct {
Verb string `msgpack:"verb"`
Job string `msgpack:"job"`
Force bool `msgpack:"force,omitempty"`
}
// Frame is one element of the response stream. Exactly one payload field is
// populated per frame, determined by Type.
type Frame struct {
Type string `msgpack:"type"`
Data []byte `msgpack:"data,omitempty"`
Msg string `msgpack:"msg,omitempty"`
Code int `msgpack:"code,omitempty"`
Error string `msgpack:"error,omitempty"`
}
// frameSender serializes access to a shared msgpack encoder so the runner's
// stdout and stderr can be written from concurrent goroutines safely.
type frameSender struct {
enc *msgpack.Encoder
mu sync.Mutex
}
func newFrameSender(enc *msgpack.Encoder) *frameSender {
return &frameSender{enc: enc}
}
func (s *frameSender) send(frame Frame) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.enc.Encode(frame)
}
// frameWriter is an io.Writer that wraps each Write as a Frame of the given
// type. Multiple frameWriters can share one frameSender to interleave types
// safely on the same connection.
type frameWriter struct {
sender *frameSender
frameType string
}
func newFrameWriter(sender *frameSender, frameType string) *frameWriter {
return &frameWriter{sender: sender, frameType: frameType}
}
func (w *frameWriter) Write(p []byte) (int, error) {
// Copy the slice; msgpack marshals []byte by reference but the caller
// may reuse the buffer once Write returns.
data := make([]byte, len(p))
copy(data, p)
if err := w.sender.send(Frame{Type: w.frameType, Data: data}); err != nil {
return 0, err
}
return len(p), nil
}