-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
371 lines (299 loc) · 7.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
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
package rpcz
import (
"context"
"crypto/tls"
"net"
"sync"
"time"
"github.com/golocron/rpcz/internal/wire"
)
// ServiceError represents an error returned by a remote method.
type ServiceError string
// Error returns the string representation of e.
func (e ServiceError) Error() string {
return string(e)
}
// ClientOptions allows to setup Client.
//
// An empty value of ClientOptions uses safe defaults:
// - Protobuf encoding
// - 8192 bytes per connection for a read buffer
// - 8192 bytes per connection for a write buffer.
type ClientOptions struct {
Encoding Encoding
ReadBufSize int
WriteBufSize int
}
// Copy returns a full copy of o.
func (o *ClientOptions) Copy() *ClientOptions {
if o == nil {
return &ClientOptions{}
}
result := &ClientOptions{
Encoding: o.Encoding,
ReadBufSize: o.ReadBufSize,
WriteBufSize: o.WriteBufSize,
}
return result
}
func (o *ClientOptions) check() error {
if o == nil {
return ErrInvalidClientOptions
}
if !checkEncoding(o.Encoding) {
return ErrInvalidEncoding
}
o.adjReadBufSize()
o.adjWriteBufSize()
return nil
}
func (o *ClientOptions) adjReadBufSize() {
o.ReadBufSize = calcBufSize(o.ReadBufSize)
}
func (o *ClientOptions) adjWriteBufSize() {
o.WriteBufSize = calcBufSize(o.WriteBufSize)
}
// Client makes requests to remote methods on services registered on a remote server.
type Client struct {
cfg *ClientOptions
c *conn
enc clientEncoder
onceStop *sync.Once
onceClose *sync.Once
onceDone *sync.Once
stopping chan struct{}
closed chan struct{}
recvDone chan struct{}
reqList requestRetainer
mu *sync.Mutex
id uint64
results map[uint64]*Result
}
// NewClient returns a client connected to saddr with Protobuf encoding and default options.
func NewClient(saddr string) (*Client, error) {
return NewClientWithOptions(saddr, &ClientOptions{})
}
// NewClientWithOptions returns a client connected to saddr and set up with opts.
func NewClientWithOptions(saddr string, opts *ClientOptions) (*Client, error) {
nc, err := net.Dial("tcp", saddr)
if err != nil {
return nil, err
}
return NewClientWithConn(opts, nc)
}
// NewClientTLS returns a client connected to saddr in TLS mode.
func NewClientTLS(saddr string, cfg *tls.Config) (*Client, error) {
return NewClientTLSOptions(saddr, cfg, &ClientOptions{})
}
// NewClientTLSOptions creates a client connected to saddr over TLS with opts.
func NewClientTLSOptions(saddr string, cfg *tls.Config, opts *ClientOptions) (*Client, error) {
nc, err := tls.Dial("tcp", saddr, cfg)
if err != nil {
return nil, err
}
return NewClientWithConn(opts, nc)
}
// NewClientWithConn creates a client set up with opts connected to nc.
func NewClientWithConn(opts *ClientOptions, nc net.Conn) (*Client, error) {
cfg := opts.Copy()
if err := cfg.check(); err != nil {
return nil, err
}
c := newConn(nc.RemoteAddr().String(), nc)
result := &Client{
cfg: cfg,
c: c,
enc: newClientEncoder(cfg.Encoding, newBufReader(c.rwc, cfg.ReadBufSize), newBufWriter(c.rwc, cfg.WriteBufSize)),
onceStop: &sync.Once{},
onceClose: &sync.Once{},
onceDone: &sync.Once{},
stopping: make(chan struct{}),
closed: make(chan struct{}),
recvDone: make(chan struct{}),
reqList: newRequestRetainer(retainRequestNum),
mu: &sync.Mutex{},
results: make(map[uint64]*Result),
}
go result.recv(context.Background())
return result, nil
}
// Do asynchronously calls mtd on svc with given args and resp.
func (c *Client) Do(ctx context.Context, svc, mtd string, args, resp interface{}) *Result {
result := newResult(svc, mtd, args, resp)
c.send(ctx, result)
return result
}
// SyncDo calls Do and awaits for the response.
func (c *Client) SyncDo(ctx context.Context, svc, mtd string, args, resp interface{}) error {
result := c.Do(ctx, svc, mtd, args, resp)
return result.Err()
}
// Peer returns the address of the server c is connected to.
func (c *Client) Peer() string {
if c.c == nil {
return ""
}
return c.c.raddr
}
// Close closes the connection and shuts down the client.
//
// Any unfinished requests are aborted.
func (c *Client) Close() error {
var err error
c.closeRecv()
c.onceClose.Do(func() {
closeOrSkip(c.closed)
err = c.c.close()
})
for !c.isRecvDone() {
time.Sleep(closeLoopDelay)
}
return err
}
func (c *Client) send(ctx context.Context, fut *Result) {
if c.isStopping() {
fut.done(ErrShutdown)
return
}
if c.isClosed() {
fut.done(ErrClosed)
return
}
c.mu.Lock()
id := c.id
c.id++
c.results[id] = fut
c.mu.Unlock()
req := c.reqList.obtainReq()
req.Kind = int32(c.cfg.Encoding)
req.Id = id
req.Service = fut.svc
req.Method = fut.mtd
if err := c.enc.writeRequest(ctx, req, fut.args); err != nil {
c.mu.Lock()
fut = c.results[id]
delete(c.results, id)
c.mu.Unlock()
if fut != nil {
fut.done(err)
}
}
c.reqList.retainReq(req)
}
func (c *Client) recv(ctx context.Context) {
var (
err error
rerr error
resp = &wire.Response{}
)
for {
select {
case <-c.recvDone:
return
case <-c.stopping:
c.closeRecv()
return
case <-c.closed:
c.closeRecv()
return
case <-ctx.Done():
c.closeRecv()
c.stop(ctx.Err())
return
default:
*resp = wire.Response{}
if err = c.enc.readResponse(ctx, resp); err != nil {
if c.isClosed() && c.isRecvDone() {
c.stop(ErrClosed)
return
}
c.closeRecv()
c.stop(err)
return
}
c.mu.Lock()
fut := c.results[resp.Id]
delete(c.results, resp.Id)
c.mu.Unlock()
switch {
case fut == nil:
// No op.
case resp.Error != "":
rerr = ServiceError(resp.Error)
fut.done(rerr)
default:
if err = unmarshal(Encoding(resp.Kind), resp.Data, fut.resp); err != nil {
fut.done(err)
continue
}
fut.done(nil)
}
}
}
}
func (c *Client) stop(err error) {
c.onceStop.Do(func() { closeOrSkip(c.stopping) })
c.mu.Lock()
for k, fut := range c.results {
delete(c.results, k)
fut.done(err)
}
c.mu.Unlock()
}
func (c *Client) closeRecv() {
c.onceDone.Do(func() { closeOrSkip(c.recvDone) })
}
func (c *Client) isStopping() bool {
select {
case <-c.stopping:
return true
default:
return false
}
}
func (c *Client) isClosed() bool {
select {
case <-c.closed:
return true
default:
return false
}
}
func (c *Client) isRecvDone() bool {
select {
case <-c.recvDone:
return true
default:
return false
}
}
// Result represents a result of a request.
type Result struct {
svc string
mtd string
args interface{}
resp interface{}
err chan error
}
func newResult(svc, mtd string, args, resp interface{}) *Result {
result := &Result{
svc: svc,
mtd: mtd,
args: args,
resp: resp,
err: make(chan error, 1),
}
return result
}
// Err waits for the request to finish and returns nil on success, and an error otherwise.
func (r *Result) Err() error {
return <-r.err
}
// ErrChan returns a channel that indicates completion of the request by sending a nil or non-nil error.
func (r *Result) ErrChan() <-chan error {
return r.err
}
func (r *Result) done(err error) {
r.err <- err
close(r.err)
}