/
client.go
404 lines (347 loc) · 9.02 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package jsonrpc2
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"strconv"
"sync"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"go.uber.org/atomic"
)
// ClientOpt is an option function that can be passed to Dial and NewClient.
type ClientOpt func(*Client)
// WithClientLogger sets the Client to use a logger.
func WithClientLogger(l log.Logger) ClientOpt {
return func(c *Client) {
if l == nil {
l = log.NewNopLogger()
} else {
c.log = l
}
}
}
type Client struct {
log log.Logger
tx *transport
// listeners holds channels waiting for a response to a specific
// message ID. It is implemented a a map of int64 to a chan of
// *txObject.
//
// The channels stored in listeners are NEVER closed, but cleaned up
// by the Go GC once the goroutine that populated listeners removes
// the entry.
listeners sync.Map
nextID *atomic.Int64
handler Handler
}
// Dial creates a connection to the target server using TCP. Handler will
// be invoked for each request received from the other side.
func Dial(target string, handler Handler, opts ...ClientOpt) (*Client, error) {
var d net.Dialer
nc, err := d.Dial("tcp", target)
if err != nil {
return nil, fmt.Errorf("failed diling to server: %w", err)
}
return NewClient(nc, handler, opts...), nil
}
// NewClient creates a client and starts reading messages from the provided
// io.ReadWriter. The given handler will be invoked for each request
// and notification that is read over rw.
//
// If rw implements io.Closer, it will be closed when the Client is closed.
func NewClient(rw io.ReadWriter, handler Handler, opts ...ClientOpt) *Client {
if handler == nil {
handler = DefaultHandler
}
cli := &Client{
log: log.NewNopLogger(),
tx: newTransport(rw),
handler: handler,
nextID: atomic.NewInt64(0),
}
for _, o := range opts {
o(cli)
}
go cli.processMessages()
return cli
}
// Close closes the underlying transport.
func (c *Client) Close() error {
return c.tx.Close()
}
// processMessages runs in the background and handles incoming messages from
// the server.
func (c *Client) processMessages() {
for {
batch, err := c.tx.ReadMessage()
if err != nil {
var txErr *transportError
if errors.As(err, &txErr) {
_ = c.tx.SendError(nil, &Error{
Code: ErrorInvalidRequest,
Message: err.Error(),
})
continue
}
level.Info(c.log).Log("msg", "closing client", "err", err)
_ = c.Close()
return
}
var resp txMessage
resp.Batched = batch.Batched
Objects:
for _, msg := range batch.Objects {
switch {
case msg.Request != nil:
r := c.handleRequest(msg.Request)
if r != nil {
resp.Objects = append(resp.Objects, &txObject{Response: r})
}
case msg.Response != nil:
// If the response ID wasn't set, then it's a generic error.
if msg.Response.ID == nil {
level.Warn(c.log).Log("msg", "received error message", "msg", msg)
continue Objects
}
msgID := convertID(msg.Response.ID)
lis, ok := c.listeners.Load(msgID)
if !ok {
// The listener either never existed or went away.
level.Warn(c.log).Log("msg", "missing listener for message response", "id", msgID)
continue Objects
}
select {
case lis.(chan *txObject) <- msg:
// Listener got message, continue as normal
case <-time.After(500 * time.Millisecond):
level.Warn(c.log).Log("msg", "unresponsive listener", "id", msgID)
break
}
}
}
if len(resp.Objects) > 0 {
if err := c.tx.SendMessage(resp); err != nil {
level.Warn(c.log).Log("msg", "error sending message, closing client", "err", err)
return
}
}
}
}
func convertID(in *string) int64 {
if in == nil {
return -1
}
res, _ := strconv.ParseInt(*in, 10, 64)
return res
}
// handleRequest handles an individual request.
func (c *Client) handleRequest(req *txRequest) *txResponse {
ww := &responseWriter{
notification: req.Notification,
resp: &txResponse{ID: req.ID},
set: atomic.NewBool(false),
}
c.handler.ServeRPC(ww, &Request{
Notification: req.Notification,
Method: req.Method,
Params: req.Params,
Client: c,
})
if ww.resp.Result == nil {
ww.resp.Result = []byte{}
}
return ww.resp
}
type responseWriter struct {
notification bool
resp *txResponse
set *atomic.Bool
}
func (w *responseWriter) WriteMessage(msg interface{}) error {
if w.notification {
return fmt.Errorf("cannot write message for notification")
}
if !w.set.CAS(false, true) {
return fmt.Errorf("response already set")
}
body, err := json.Marshal(msg)
if err != nil {
return err
}
w.resp.Result = json.RawMessage(body)
return nil
}
func (w *responseWriter) WriteError(errCode int, err error) error {
if w.notification {
return fmt.Errorf("cannot write message for notification")
}
if !w.set.CAS(false, true) {
return fmt.Errorf("response already set")
}
w.resp.Error = &Error{
Code: errCode,
Message: err.Error(),
}
return nil
}
// Batch creates a new request batch.
func (c *Client) Batch() *Batch {
return &Batch{cli: c}
}
// Notify sends a notification request to the other side of the
// connection. It does not wait for a response, and there is no way of knowing
// if the other side succesfully handled the notification. An error will be
// returned for transport-level problems.
func (c *Client) Notify(method string, msg interface{}) error {
body, err := json.Marshal(msg)
if err != nil {
return err
}
return c.tx.SendMessage(txMessage{
Batched: false,
Objects: []*txObject{{
Request: &txRequest{
Notification: true,
Method: method,
Params: body,
},
}},
})
}
// Invoke invokes an RPC on the other side of the connection and waits
// for a repsonse. Error will be set for RPC-level and transport-level
// problems.
//
// RPC-level errors will be set to the Error object.
func (c *Client) Invoke(ctx context.Context, method string, msg interface{}) (json.RawMessage, error) {
body, err := json.Marshal(msg)
if err != nil {
return nil, err
}
var (
msgID = c.nextID.Inc()
msgText = strconv.FormatInt(msgID, 10)
respCh = make(chan *txObject, 1)
)
c.listeners.Store(msgID, respCh)
defer c.listeners.Delete(msgID)
err = c.tx.SendMessage(txMessage{
Batched: false,
Objects: []*txObject{{
Request: &txRequest{
Notification: false,
ID: &msgText,
Method: method,
Params: body,
},
}},
})
if err != nil {
return nil, err
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case resp := <-respCh:
if resp.Response == nil {
return nil, fmt.Errorf("unexpected message: no response body")
}
if resp.Response.Error != nil {
return nil, *resp.Response.Error
}
return resp.Response.Result, nil
}
}
// Batch is a batch of messages to send to a client. It must be committed with
// Commit. A Batch can be created through the Batch method on a Client.
type Batch struct {
cli *Client
msg txMessage
watchers sync.Map
}
// Notify adds a notification request to the batch.
func (b *Batch) Notify(method string, msg interface{}) error {
body, err := json.Marshal(msg)
if err != nil {
return err
}
b.msg.Objects = append(b.msg.Objects, &txObject{
Request: &txRequest{
Notification: true,
Method: method,
Params: body,
},
})
return nil
}
// Invoke queues an RPC to invoke. The returned *json.RawMessage will be empty until
// the batch is commited.
func (b *Batch) Invoke(method string, msg interface{}) (*json.RawMessage, error) {
body, err := json.Marshal(msg)
if err != nil {
return nil, err
}
var (
msgID = b.cli.nextID.Inc()
msgText = strconv.FormatInt(msgID, 10)
result json.RawMessage
respCh = make(chan *txObject, 1)
)
b.watchers.Store(msgID, &result)
b.cli.listeners.Store(msgID, respCh)
b.msg.Objects = append(b.msg.Objects, &txObject{
Request: &txRequest{
Notification: false,
ID: &msgText,
Method: method,
Params: body,
},
})
return &result, nil
}
// Commit commits the batch. If the response had any errors, the first error is returned.
func (b *Batch) Commit(ctx context.Context) error {
b.msg.Batched = true
if err := b.cli.tx.SendMessage(b.msg); err != nil {
return err
}
var firstError error
// Read responses in serial. The slowest response blocks the entire chain.
// Note that all the channels are buffered, so there's no need to parallelize this.
b.watchers.Range(func(key, value interface{}) bool {
defer b.watchers.Delete(key)
defer b.cli.listeners.Delete(key)
ch, ok := b.cli.listeners.Load(key)
if !ok {
return false
}
select {
case <-ctx.Done():
if firstError != nil {
firstError = ctx.Err()
}
return true
case resp := <-ch.(chan *txObject):
if resp.Response != nil {
if firstError != nil {
firstError = fmt.Errorf("unexpected message: no response body")
}
return true
}
if resp.Response.Error != nil {
if firstError != nil {
firstError = *resp.Response.Error
}
return true
}
*value.(*json.RawMessage) = resp.Response.Result
}
return true
})
return firstError
}