-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
394 lines (331 loc) · 8.55 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
package koinosmq
import (
"context"
"errors"
"fmt"
"math/rand"
"sync"
"time"
log "github.com/koinos/koinos-log-golang/v2"
amqp "github.com/rabbitmq/amqp091-go"
)
type ContentType string
const (
OctetStream = "application/octet-stream"
)
// RPCCallResult is the result of an rpc call
type RPCCallResult struct {
Result []byte
Error error
}
type rpcRequest struct {
resultChan chan *RPCCallResult
id string
contentType ContentType
rpcService string
args []byte
expiration string
ctx context.Context
}
type rpcResult struct {
id string
data []byte
err error
}
// Client AMPQ Golang Wrapper
//
// - Each RPC message has an rpcService
// - Queue for RPC of type T is kept in queue named `koins_rpc_T`
// - RPC messages per node type
// - Single global exchange for events, all node types
type Client struct {
/**
* Remote address to connect to.
*/
Address string
/**
* Number of RPC Return consumers
*/
rpcReturnNumConsumers int
rpcReturnMap map[string]chan *RPCCallResult
rpcReplyTo string
rpcRetryPolicy RetryPolicy
requestChan chan *rpcRequest
resultChan chan *rpcResult
expirationChan chan string
conn *connection
connMutex sync.Mutex
}
// NewClient factory method.
func NewClient(addr string, rpcRetryPolicy RetryPolicy) *Client {
client := &Client{
Address: addr,
rpcRetryPolicy: rpcRetryPolicy,
rpcReturnNumConsumers: 1,
rpcReturnMap: make(map[string]chan *RPCCallResult),
requestChan: make(chan *rpcRequest, 10),
resultChan: make(chan *rpcResult, 10),
expirationChan: make(chan string, 10),
conn: &connection{},
}
return client
}
// Start begins the connection loop. Blocks until first connected to AMQP
func (c *Client) Start(ctx context.Context) <-chan struct{} {
connectedChan := make(chan struct{}, 1)
go c.connectLoop(ctx, connectedChan)
return connectedChan
}
// SetNumConsumers sets the number of consumers for queues.
//
// This sets the number of parallel goroutines that consume the respective AMQP queues.
// Must be called before Connect().
func (c *Client) SetNumConsumers(rpcReturnNumConsumers int) {
c.rpcReturnNumConsumers = rpcReturnNumConsumers
}
func (c *Client) connectLoop(ctx context.Context, connectedChan chan<- struct{}) {
const (
ConnectionTimeout = 1
RetryMinDelay = 1
RetryMaxDelay = 25
RetryDelayPerRetry = 2
)
go c.connectionLoop(ctx)
for {
retryCount := 0
log.Infof("Connecting client to AMQP server %v", c.Address)
for {
c.connMutex.Lock()
conectCtx, connectCancel := context.WithTimeout(ctx, ConnectionTimeout*time.Second)
defer connectCancel()
c.conn = &connection{}
err := c.conn.Open(conectCtx, c.Address)
if err == nil {
consumers, replyTo, err := c.conn.CreateRPCReturnChannels(c.rpcReturnNumConsumers)
if err == nil {
c.rpcReplyTo = replyTo
for _, consumer := range consumers {
go c.consumeRPCReturnLoop(ctx, consumer)
}
log.Infof("Client connected")
if connectedChan != nil {
connectedChan <- struct{}{}
close(connectedChan)
connectedChan = nil
}
break
}
}
c.connMutex.Unlock()
delay := RetryMinDelay + RetryDelayPerRetry*retryCount
if delay > RetryMaxDelay {
delay = RetryMaxDelay
}
select {
case <-time.After(time.Duration(delay) * time.Second):
retryCount++
case <-ctx.Done():
return
}
}
c.connMutex.Unlock()
select {
case <-c.conn.NotifyClose:
c.conn.Close()
continue
case <-ctx.Done():
c.conn.Close()
return
}
}
}
func randInt(min int, max int) int {
return min + rand.Intn(max-min)
}
func randomString(l int) string {
bytes := make([]byte, l)
for i := 0; i < l; i++ {
bytes[i] = byte(randInt(65, 90))
}
return string(bytes)
}
func (c *Client) tryBroadcast(ctx context.Context, contentType ContentType, topic string, args []byte) error {
c.connMutex.Lock()
defer c.connMutex.Unlock()
if (c.conn == nil) || !c.conn.IsOpen() {
return errors.New("AMQP connection is not open")
}
return c.conn.AmqpChan.PublishWithContext(
ctx,
broadcastExchangeName,
topic,
false,
false,
amqp.Publishing{
ContentType: string(contentType),
Body: args,
},
)
}
// Broadcast a message via AMQP
func (c *Client) Broadcast(ctx context.Context, contentType ContentType, topic string, args []byte) error {
// Ask the retry factory for a new policy instance
retry := getRetryPolicy(c.rpcRetryPolicy)
for {
// If the context has been cancelled, quit without a result
if ctx.Err() != nil {
return fmt.Errorf("broadcast failed, %v", ctx.Err())
}
broadcastCtx, broadcastCancel := context.WithTimeout(ctx, retry.PollTimeout())
defer broadcastCancel()
err := c.tryBroadcast(broadcastCtx, contentType, topic, args)
// If there were no errors, we are done
if err == nil {
return err
}
// See if the policy requests a retry
retryResult := retry.CheckRetry()
if !retryResult.DoRetry {
return fmt.Errorf("broadcast failed, %v", err)
}
// Sleep for the required amount of time
select {
case <-time.After(retryResult.Timeout):
case <-ctx.Done():
}
}
}
func (c *Client) tryRPC(ctx context.Context, contentType ContentType, rpcService string, expiration string, args []byte) ([]byte, error) {
corrID := randomString(32)
resultChan := make(chan *RPCCallResult, 1)
select {
case c.requestChan <- &rpcRequest{
resultChan: resultChan,
id: corrID,
contentType: contentType,
rpcService: rpcService,
args: args,
expiration: expiration,
ctx: ctx,
}:
case <-ctx.Done():
go func() {
c.expirationChan <- corrID
}()
return nil, ctx.Err()
}
select {
case res := <-resultChan:
return res.Result, res.Error
case <-ctx.Done():
return nil, ctx.Err()
}
}
// RPC makes an RPC call
func (c *Client) RPC(ctx context.Context, contentType ContentType, rpcService string, args []byte) ([]byte, error) {
// Ask the retry factory for a new policy instance
retry := getRetryPolicy(c.rpcRetryPolicy)
for {
// If the context has been cancelled, quit without a result
if ctx.Err() != nil {
return nil, fmt.Errorf("rpc failed, %v", ctx.Err())
}
rpcCtx, rpcCancel := context.WithTimeout(ctx, retry.PollTimeout())
defer rpcCancel()
result, err := c.tryRPC(rpcCtx, contentType, rpcService, durationToUnitString(retry.PollTimeout(), time.Millisecond), args)
// If there were no errors, we are done
if err == nil {
return result, err
}
// See if the policy requests a retry
retryResult := retry.CheckRetry()
if !retryResult.DoRetry {
return nil, fmt.Errorf("rpc failed, %v", err)
}
// Sleep for the required amount of time
select {
case <-time.After(retryResult.Timeout):
case <-ctx.Done():
}
}
}
func (c *Client) consumeRPCReturnLoop(ctx context.Context, consumer <-chan amqp.Delivery) {
for delivery := range consumer {
select {
case c.resultChan <- &rpcResult{
id: delivery.CorrelationId,
data: delivery.Body,
}:
case <-ctx.Done():
return
}
}
}
func (c *Client) handleRequest(req *rpcRequest) {
c.connMutex.Lock()
defer c.connMutex.Unlock()
var err error
if (c.conn == nil) || !c.conn.IsOpen() {
err = errors.New("AMQP connection is not open")
}
if err == nil {
c.rpcReturnMap[req.id] = req.resultChan
err = c.conn.AmqpChan.PublishWithContext(
req.ctx,
rpcExchangeName,
rpcQueuePrefix+req.rpcService,
false,
false,
amqp.Publishing{
ContentType: string(req.contentType),
CorrelationId: req.id,
ReplyTo: c.rpcReplyTo,
Body: req.args,
Expiration: req.expiration,
},
)
}
if err != nil {
delete(c.rpcReturnMap, req.id)
req.resultChan <- &RPCCallResult{
Error: err,
}
close(req.resultChan)
}
}
func (c *Client) handleResult(res *rpcResult) {
if resChan, ok := c.rpcReturnMap[res.id]; ok {
delete(c.rpcReturnMap, res.id)
resChan <- &RPCCallResult{
Result: res.data,
Error: res.err,
}
close(resChan)
}
}
func (c *Client) handleExpiration(id string) {
if resChan, ok := c.rpcReturnMap[id]; ok {
delete(c.rpcReturnMap, id)
resChan <- &RPCCallResult{
Error: errors.New("rpc call timeout"),
}
close(resChan)
}
}
func (c *Client) connectionLoop(ctx context.Context) {
for {
select {
case req := <-c.requestChan:
c.handleRequest(req)
case res := <-c.resultChan:
c.handleResult(res)
case id := <-c.expirationChan:
c.handleExpiration(id)
case <-ctx.Done():
return
}
}
}
func durationToUnitString(duration time.Duration, unit time.Duration) string {
return fmt.Sprint(int(duration / unit))
}