-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
334 lines (275 loc) · 6.01 KB
/
client.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package watchman
import (
"encoding/json"
"errors"
"fmt"
"io"
"net"
"os"
"os/exec"
"sync"
"sync/atomic"
"github.com/jonasi/watchman/bser"
)
var logPDU = func() bool {
return os.Getenv("WATCHMAN_LOG_PDU") != ""
}()
// Error is a Watchman API error
type Error string
func (e Error) Error() string {
return string(e)
}
// Client is a watchman client
type Client struct {
Sockname string
enc *bser.Encoder
dec *bser.Decoder
initOnce sync.Once
initErr error
inited int32
reqCh chan interface{}
cleanup func() error
}
// request to send an outgoing message
type sendReq struct {
dest interface{}
args []interface{}
errCh chan error
}
// request to listen for uniteral messages
type recReq struct {
rec chan<- interface{}
stop chan struct{}
}
// request to stop listening for unilateral messages
type stopReq struct {
idx int
}
func (c *Client) init() error {
c.initOnce.Do(func() {
if !atomic.CompareAndSwapInt32(&c.inited, 0, 1) {
c.initErr = errors.New("Cannot call send on a closed client")
return
}
var err error
if c.Sockname == "" {
c.Sockname, err = inferSockname()
if err != nil {
c.initErr = err
return
}
}
sconn, err := initSock(c.Sockname)
if err != nil {
c.initErr = err
return
}
c.cleanup = sconn.Close
var conn io.ReadWriter = sconn
if logPDU {
tap := bser.NewTap(conn, pduLogger("incoming", os.Stderr), pduLogger("outgoing", os.Stderr))
c.cleanup = func() error {
tap.Untap()
return sconn.Close()
}
conn = tap
}
c.enc = bser.NewEncoder(conn)
c.dec = bser.NewDecoder(conn)
c.reqCh = make(chan interface{})
decCh := make(chan interface{})
go c.readPDUs(decCh)
go c.handleReqs(decCh)
})
return c.initErr
}
// Close closes the connection to the watchman server
func (c *Client) Close() error {
if !atomic.CompareAndSwapInt32(&c.inited, 1, 2) {
// never been opened!
return nil
}
close(c.reqCh)
return c.cleanup()
}
func (c *Client) readPDUs(ch chan interface{}) {
for {
var m bser.RawMessage
if err := c.dec.Decode(&m); err != nil {
ch <- err
} else {
ch <- m
}
}
}
type watch struct {
sync.RWMutex
ch chan<- interface{}
closed bool
}
func (c *Client) handleReqs(ch chan interface{}) {
var (
activeReq *sendReq
queuedReqs = []*sendReq{}
watches = []*watch{}
)
processNext := func() {
if activeReq != nil || len(queuedReqs) == 0 {
return
}
activeReq = queuedReqs[0]
queuedReqs = queuedReqs[1:]
if err := c.enc.Encode(activeReq.args); err != nil {
activeReq.errCh <- err
activeReq = nil
}
}
for {
select {
case v, ok := <-c.reqCh:
if !ok {
return
}
switch req := v.(type) {
// send request - add it to the queue of messages
// and immediately process
case sendReq:
queuedReqs = append(queuedReqs, &req)
processNext()
// rec request - add it to our list of watches
// and start listen for the stop watching signal
case recReq:
watches = append(watches, &watch{ch: req.rec})
go func(idx int, stop chan struct{}) {
<-stop
c.reqCh <- stopReq{idx}
}(len(watches)-1, req.stop)
// stop request - remove the watch channel
// from the list of channels to watch and close the
// channel
case stopReq:
w := watches[req.idx]
w.Lock()
close(w.ch)
w.closed = true
w.Unlock()
watches[req.idx] = nil
}
case v := <-ch:
if activeReq == nil {
c.handleUnilateral(watches, v)
continue
}
if err, ok := v.(error); ok {
activeReq.errCh <- err
} else {
activeReq.errCh <- bser.UnmarshalValue(v.(bser.RawMessage), activeReq.dest)
}
activeReq = nil
processNext()
}
}
}
func (c *Client) handleUnilateral(watches []*watch, v interface{}) {
// should only ever be this...
msg, ok := v.(bser.RawMessage)
if !ok {
return
}
var data struct {
base
*LogEvent
*SubscribeEvent
}
if err := bser.UnmarshalValue(msg, &data); err != nil {
// todo(isao) - log?
return
}
var d interface{}
if data.LogEvent != nil {
d = data.LogEvent
} else if data.SubscribeEvent != nil {
d = data.SubscribeEvent
}
if d == nil {
// unhandled
// todo(isao) - log?
return
}
// dispatch msg too all watchers asynchronously
for _, w := range watches {
if w == nil {
continue
}
go func(w *watch, d interface{}) {
w.RLock()
defer w.RUnlock()
if w.closed {
return
}
w.ch <- d
}(w, d)
}
}
func initSock(sock string) (net.Conn, error) {
addr, err := net.ResolveUnixAddr("unix", sock)
if err != nil {
return nil, err
}
return net.DialUnix("unix", nil, addr)
}
func inferSockname() (string, error) {
if v := os.Getenv("WATCHMAN_SOCK"); v != "" {
return v, nil
}
b, err := exec.Command("watchman", "get-sockname").Output()
if err != nil {
return "", err
}
var d map[string]string
if err := json.Unmarshal(b, &d); err != nil {
return "", err
}
return d["sockname"], nil
}
// Send makes a client call
func (c *Client) Send(dest interface{}, args ...interface{}) error {
if err := c.init(); err != nil {
return err
}
r := sendReq{args: args, dest: dest, errCh: make(chan error)}
c.reqCh <- r
return <-r.errCh
}
// Receive listens for unilateral messages from the server on ch.
func (c *Client) Receive(ch chan<- interface{}) (func(), error) {
if err := c.init(); err != nil {
return nil, err
}
stop := make(chan struct{})
c.reqCh <- recReq{rec: ch, stop: stop}
return func() {
stop <- struct{}{}
}, nil
}
type base struct {
Version string `bser:"version"`
Error Error `bser:"error"`
Warning string `bser:"warning"`
Unilateral bool `bser:"unilateral"`
}
func pduLogger(dir string, w io.Writer) func([]byte) {
return func(b []byte) {
var d interface{}
if err := bser.UnmarshalValue(b, &d); err != nil {
fmt.Fprintf(w, "[pdu logger - %s - bser unmarshal error]: %s\n", dir, err)
return
}
b, err := json.Marshal(d)
if err != nil {
fmt.Fprintf(w, "[pdu logger - %s - json marshal error]: %s\n", dir, err)
return
}
fmt.Fprintf(w, "[pdu logger - %s]: %s\n", dir, string(b))
}
}