forked from smallnest/rpcx
/
oneclient.go
241 lines (203 loc) · 6.35 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
package client
import (
"context"
"sync"
multierror "github.com/hashicorp/go-multierror"
"github.com/smallnest/rpcx/protocol"
)
// OneClient wraps servicesPath and XClients.
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(servicePath string, 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()
}
// 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 {
c.mu.Lock()
xclient := c.xclients[servicePath]
if xclient == nil {
xclient = c.newXClient(servicePath)
c.xclients[servicePath] = xclient
}
c.mu.Unlock()
}
return xclient.Go(ctx, serviceMethod, args, reply, done)
}
func (c *OneClient) newXClient(servicePath string) XClient {
var xclient XClient
if c.serverMessageChan == nil {
xclient = NewXClient(servicePath, c.failMode, c.selectMode, c.discovery.Clone(servicePath), c.option)
} else {
xclient = NewBidirectionalXClient(servicePath, c.failMode, c.selectMode, c.discovery.Clone(servicePath), 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
}
// 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 {
c.mu.Lock()
xclient = c.xclients[servicePath]
if xclient == nil {
xclient = c.newXClient(servicePath)
c.xclients[servicePath] = xclient
}
c.mu.Unlock()
}
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 {
c.mu.Lock()
xclient := c.xclients[servicePath]
if xclient == nil {
xclient = c.newXClient(servicePath)
c.xclients[servicePath] = xclient
}
c.mu.Unlock()
}
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 {
c.mu.Lock()
xclient := c.xclients[servicePath]
if xclient == nil {
xclient = c.newXClient(servicePath)
c.xclients[servicePath] = xclient
}
c.mu.Unlock()
}
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 {
c.mu.Lock()
xclient := c.xclients[servicePath]
if xclient == nil {
xclient = c.newXClient(servicePath)
c.xclients[servicePath] = xclient
}
c.mu.Unlock()
}
return xclient.Fork(ctx, serviceMethod, args, reply)
}
// 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
}