forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.go
103 lines (90 loc) · 2.12 KB
/
pipe.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package workerproto
import (
"bytes"
"encoding/json"
"io"
"log"
)
// PipeTransport implements the worker-runner protocol over a pipe. It reads
// from its input in the `Recv` method, and writes to its output in Send. It
// is safe to assume that a message has been handed to the OS when Send
// returns.
type PipeTransport struct {
input io.Reader
output io.Writer
readEOF bool
inBuffer []byte
}
// Create a new PipeTransport. The result will read from input and write to
// output.
func NewPipeTransport(input io.Reader, output io.Writer) *PipeTransport {
return &PipeTransport{
input: input,
output: output,
}
}
// workerproto.Transport interface
func (transp *PipeTransport) Send(msg Message) {
j, err := json.Marshal(&msg)
if err != nil {
log.Printf("could not marshal protocol message: %v", err)
return
}
j = append(append([]byte{'~'}, j...), '\n')
for len(j) > 0 {
n, err := transp.output.Write(j)
if err == nil {
return
}
if n == 0 {
log.Printf("could not write protocol message: %v", err)
return
}
j = j[n:]
}
}
func (transp *PipeTransport) Recv() (msg Message, ok bool) {
for {
newline := bytes.IndexRune(transp.inBuffer, '\n')
if !transp.readEOF && newline == -1 {
p := make([]byte, 4096)
n, err := transp.input.Read(p)
if n > 0 {
transp.inBuffer = append(transp.inBuffer, p[:n]...)
}
if err != nil {
if err != io.EOF {
log.Printf("error reading from protocol: %v", err)
}
transp.readEOF = true
}
}
newline = bytes.IndexRune(transp.inBuffer, '\n')
if newline != -1 {
invalid := false
line := bytes.TrimSpace(transp.inBuffer[:newline])
transp.inBuffer = transp.inBuffer[newline+1:]
if len(line) < 3 || line[0] != '~' || line[1] != '{' || line[len(line)-1] != '}' {
invalid = true
}
if !invalid {
err := json.Unmarshal(line[1:], &msg)
if err != nil {
invalid = true
}
}
if invalid {
// strip the newline and hand this to the logger as unstructured data
log.Println(string(line))
} else {
ok = true
return
}
} else {
if transp.readEOF {
ok = false
return
}
}
}
}