/
oneclient.go
352 lines (300 loc) ยท 8.58 KB
/
oneclient.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
package client
import (
"context"
"fmt"
"io"
"net"
"sync"
"github.com/smallnest/rpcx/share"
multierror "github.com/hashicorp/go-multierror"
"github.com/smallnest/rpcx/protocol"
)
// OneClient wraps servicesPath and XClients.
// Users can use a shared oneclient to access multiple services.
type OneClient struct {
xclients map[string]XClient
mu sync.RWMutex
failMode FailMode
selectMode SelectMode
discovery ServiceDiscovery
option Option
selectors map[string]Selector
Plugins PluginContainer
latitude float64
longitude float64
auth string
serverMessageChan chan<- *protocol.Message
}
// NewOneClient creates a OneClient that supports service discovery and service governance.
func NewOneClient(failMode FailMode, selectMode SelectMode, discovery ServiceDiscovery, option Option) *OneClient {
return &OneClient{
failMode: failMode,
selectMode: selectMode,
discovery: discovery,
option: option,
xclients: make(map[string]XClient),
selectors: make(map[string]Selector),
}
}
// NewBidirectionalOneClient creates a new xclient that can receive notifications from servers.
func NewBidirectionalOneClient(failMode FailMode, selectMode SelectMode, discovery ServiceDiscovery, option Option, serverMessageChan chan<- *protocol.Message) *OneClient {
return &OneClient{
failMode: failMode,
selectMode: selectMode,
discovery: discovery,
option: option,
xclients: make(map[string]XClient),
selectors: make(map[string]Selector),
serverMessageChan: serverMessageChan,
}
}
// SetSelector sets customized selector by users.
func (c *OneClient) SetSelector(servicePath string, s Selector) {
c.mu.Lock()
c.selectors[servicePath] = s
if xclient, ok := c.xclients[servicePath]; ok {
xclient.SetSelector(s)
}
c.mu.Unlock()
}
// SetPlugins sets client's plugins.
func (c *OneClient) SetPlugins(plugins PluginContainer) {
c.Plugins = plugins
c.mu.RLock()
for _, v := range c.xclients {
v.SetPlugins(plugins)
}
c.mu.RUnlock()
}
func (c *OneClient) GetPlugins() PluginContainer {
return c.Plugins
}
// ConfigGeoSelector sets location of client's latitude and longitude,
// and use newGeoSelector.
func (c *OneClient) ConfigGeoSelector(latitude, longitude float64) {
c.selectMode = Closest
c.latitude = latitude
c.longitude = longitude
c.mu.RLock()
for _, v := range c.xclients {
v.ConfigGeoSelector(latitude, longitude)
}
c.mu.RUnlock()
}
// Auth sets s token for Authentication.
func (c *OneClient) Auth(auth string) {
c.auth = auth
c.mu.RLock()
for _, v := range c.xclients {
v.Auth(auth)
}
c.mu.RUnlock()
}
// Go invokes the function asynchronously. It returns the Call structure representing the invocation. The done channel will signal when the call is complete by returning the same Call object. If done is nil, Go will allocate a new channel. If non-nil, done must be buffered or Go will deliberately crash.
// It does not use FailMode.
func (c *OneClient) Go(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}, done chan *Call) (*Call, error) {
c.mu.RLock()
xclient := c.xclients[servicePath]
c.mu.RUnlock()
if xclient == nil {
var err error
c.mu.Lock()
xclient = c.xclients[servicePath]
if xclient == nil {
xclient, err = c.newXClient(servicePath)
c.xclients[servicePath] = xclient
}
c.mu.Unlock()
if err != nil {
return nil, err
}
}
return xclient.Go(ctx, serviceMethod, args, reply, done)
}
func (c *OneClient) newXClient(servicePath string) (xclient XClient, err error) {
defer func() {
if r := recover(); r != nil {
if e, ok := r.(error); ok {
err = e
} else {
err = fmt.Errorf("%v", r)
}
}
}()
d, err := c.discovery.Clone(servicePath)
if err != nil {
return nil, err
}
if c.serverMessageChan == nil {
xclient = NewXClient(servicePath, c.failMode, c.selectMode, d, c.option)
} else {
xclient = NewBidirectionalXClient(servicePath, c.failMode, c.selectMode, d, c.option, c.serverMessageChan)
}
if c.Plugins != nil {
xclient.SetPlugins(c.Plugins)
}
if s, ok := c.selectors[servicePath]; ok {
xclient.SetSelector(s)
}
if c.selectMode == Closest {
xclient.ConfigGeoSelector(c.latitude, c.longitude)
}
if c.auth != "" {
xclient.Auth(c.auth)
}
return xclient, err
}
// Call invokes the named function, waits for it to complete, and returns its error status.
// It handles errors base on FailMode.
func (c *OneClient) Call(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) error {
c.mu.RLock()
xclient := c.xclients[servicePath]
c.mu.RUnlock()
if xclient == nil {
var err error
c.mu.Lock()
xclient = c.xclients[servicePath]
if xclient == nil {
xclient, err = c.newXClient(servicePath)
c.xclients[servicePath] = xclient
}
c.mu.Unlock()
if err != nil {
return err
}
}
return xclient.Call(ctx, serviceMethod, args, reply)
}
func (c *OneClient) SendRaw(ctx context.Context, r *protocol.Message) (map[string]string, []byte, error) {
servicePath := r.ServicePath
c.mu.RLock()
xclient := c.xclients[servicePath]
c.mu.RUnlock()
if xclient == nil {
var err error
c.mu.Lock()
xclient = c.xclients[servicePath]
if xclient == nil {
xclient, err = c.newXClient(servicePath)
c.xclients[servicePath] = xclient
}
c.mu.Unlock()
if err != nil {
return nil, nil, err
}
}
return xclient.SendRaw(ctx, r)
}
// Broadcast sends requests to all servers and Success only when all servers return OK.
// FailMode and SelectMode are meanless for this method.
// Please set timeout to avoid hanging.
func (c *OneClient) Broadcast(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) error {
c.mu.RLock()
xclient := c.xclients[servicePath]
c.mu.RUnlock()
if xclient == nil {
var err error
c.mu.Lock()
xclient = c.xclients[servicePath]
if xclient == nil {
xclient, err = c.newXClient(servicePath)
c.xclients[servicePath] = xclient
}
c.mu.Unlock()
if err != nil {
return err
}
}
return xclient.Broadcast(ctx, serviceMethod, args, reply)
}
// Fork sends requests to all servers and Success once one server returns OK.
// FailMode and SelectMode are meanless for this method.
func (c *OneClient) Fork(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) error {
c.mu.RLock()
xclient := c.xclients[servicePath]
c.mu.RUnlock()
if xclient == nil {
var err error
c.mu.Lock()
xclient = c.xclients[servicePath]
if xclient == nil {
xclient, err = c.newXClient(servicePath)
c.xclients[servicePath] = xclient
}
c.mu.Unlock()
if err != nil {
return err
}
}
return xclient.Fork(ctx, serviceMethod, args, reply)
}
func (c *OneClient) SendFile(ctx context.Context, fileName string, rateInBytesPerSecond int64, meta map[string]string) error {
c.mu.RLock()
xclient := c.xclients[share.SendFileServiceName]
c.mu.RUnlock()
if xclient == nil {
var err error
c.mu.Lock()
xclient = c.xclients[share.SendFileServiceName]
if xclient == nil {
xclient, err = c.newXClient(share.SendFileServiceName)
c.xclients[share.SendFileServiceName] = xclient
}
c.mu.Unlock()
if err != nil {
return err
}
}
return xclient.SendFile(ctx, fileName, rateInBytesPerSecond, meta)
}
func (c *OneClient) DownloadFile(ctx context.Context, requestFileName string, saveTo io.Writer, meta map[string]string) error {
c.mu.RLock()
xclient := c.xclients[share.SendFileServiceName]
c.mu.RUnlock()
if xclient == nil {
var err error
c.mu.Lock()
xclient = c.xclients[share.SendFileServiceName]
if xclient == nil {
xclient, err = c.newXClient(share.SendFileServiceName)
c.xclients[share.SendFileServiceName] = xclient
}
c.mu.Unlock()
if err != nil {
return err
}
}
return xclient.DownloadFile(ctx, requestFileName, saveTo, meta)
}
func (c *OneClient) Stream(ctx context.Context, meta map[string]string) (net.Conn, error) {
c.mu.RLock()
xclient := c.xclients[share.StreamServiceName]
c.mu.RUnlock()
if xclient == nil {
var err error
c.mu.Lock()
xclient = c.xclients[share.StreamServiceName]
if xclient == nil {
xclient, err = c.newXClient(share.StreamServiceName)
c.xclients[share.StreamServiceName] = xclient
}
c.mu.Unlock()
if err != nil {
return nil, err
}
}
return xclient.Stream(ctx, meta)
}
// Close closes all xclients and its underlying connnections to services.
func (c *OneClient) Close() error {
var result error
c.mu.RLock()
for _, v := range c.xclients {
err := v.Close()
if err != nil {
result = multierror.Append(result, err)
}
}
c.mu.RUnlock()
return result
}