forked from aws/aws-dax-go
/
single.go
695 lines (646 loc) · 22 KB
/
single.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/*
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
A copy of the License is located at
http://www.apache.org/licenses/LICENSE-2.0
or in the "license" file accompanying this file. This file is distributed
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing
permissions and limitations under the License.
*/
package client
import (
"bytes"
"fmt"
"github.com/aws/aws-dax-go/dax/internal/cbor"
"github.com/aws/aws-dax-go/dax/internal/lru"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/client/metadata"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/dynamodb"
"time"
)
const (
userAgent = "DaxGoClient-1.0.0"
daxAddress = "https://dax.amazonaws.com"
authTtlSecs = 5 * 60
tubeAuthWindowScalar = 0.75
emptyAttributeListId = 1
)
const (
serviceName = "dax"
opDefineAttributeList = "DefineAttributeList"
opDefineAttributeListId = "DefineAttributeListId"
opDefineKeySchema = "DefineKeySchema"
opEndpoints = "Endpoints"
OpGetItem = "GetItem"
OpPutItem = "PutItem"
OpUpdateItem = "UpdateItem"
OpDeleteItem = "DeleteItem"
OpBatchGetItem = "BatchGetItem"
OpBatchWriteItem = "BatchWriteItem"
OpQuery = "Query"
OpScan = "Scan"
)
var clientInfo = metadata.ClientInfo{ServiceName: serviceName}
const (
keySchemaLruCacheSize = 100
attributeListLruCacheSize = 1000
)
type SingleDaxClient struct {
region string
credentials *credentials.Credentials
tubeAuthWindowSecs int64
handlers *request.Handlers
pool *tubePool
keySchema *lru.Lru
attrNamesListToId *lru.Lru
attrListIdToNames *lru.Lru
}
func NewSingleClient(endpoint, region string, credentials *credentials.Credentials) (*SingleDaxClient, error) {
return newSingleClientWithOptions(endpoint, region, credentials, -1)
}
func newSingleClientWithOptions(endpoint, region string, credentials *credentials.Credentials, maxPendingConnections int) (*SingleDaxClient, error) {
po := defaultTubePoolOptions
if maxPendingConnections > 0 {
po.maxConcurrentConnAttempts = maxPendingConnections
}
client := &SingleDaxClient{
region: region,
credentials: credentials,
tubeAuthWindowSecs: authTtlSecs * tubeAuthWindowScalar,
pool: newTubePoolWithOptions(endpoint, po),
}
client.handlers = client.buildHandlers()
client.keySchema = &lru.Lru{
MaxEntries: keySchemaLruCacheSize,
LoadFunc: func(ctx aws.Context, key lru.Key) (interface{}, error) {
table, ok := key.(string)
if !ok {
return nil, awserr.New(request.ErrCodeSerialization, "unexpected type for table name", nil)
}
if ctx == nil {
ctx = aws.BackgroundContext()
}
return client.defineKeySchema(ctx, table)
},
}
client.attrNamesListToId = &lru.Lru{
MaxEntries: attributeListLruCacheSize,
LoadFunc: func(ctx aws.Context, key lru.Key) (interface{}, error) {
attrNames, ok := key.([]string)
if !ok {
return nil, awserr.New(request.ErrCodeSerialization, "unexpected type for attribute list", nil)
}
if ctx == nil {
ctx = aws.BackgroundContext()
}
return client.defineAttributeListId(ctx, attrNames)
},
KeyMarshaller: func(key lru.Key) lru.Key {
var buf bytes.Buffer
w := cbor.NewWriter(&buf)
defer w.Close()
for _, v := range key.([]string) {
w.WriteString(v)
}
w.Flush()
return string(buf.Bytes())
},
}
client.attrListIdToNames = &lru.Lru{
MaxEntries: attributeListLruCacheSize,
LoadFunc: func(ctx aws.Context, key lru.Key) (interface{}, error) {
id, ok := key.(int64)
if !ok {
return nil, awserr.New(request.ErrCodeSerialization, "unexpected type for attribute list id", nil)
}
if ctx == nil {
ctx = aws.BackgroundContext()
}
return client.defineAttributeList(ctx, id)
},
}
return client, nil
}
func (client *SingleDaxClient) Close() error {
if client.pool != nil {
return client.pool.Close()
}
return nil
}
func (client *SingleDaxClient) endpoints(opt RequestOptions) ([]serviceEndpoint, error) {
encoder := func(writer *cbor.Writer) error {
return encodeEndpointsInput(writer)
}
var out []serviceEndpoint
var err error
decoder := func(reader *cbor.Reader) error {
out, err = decodeEndpointsOutput(reader)
return err
}
if err = client.executeWithRetries(opEndpoints, opt, encoder, decoder); err != nil {
return nil, err
}
return out, nil
}
func (client *SingleDaxClient) defineAttributeListId(ctx aws.Context, attrNames []string) (int64, error) {
if len(attrNames) == 0 {
return emptyAttributeListId, nil
}
encoder := func(writer *cbor.Writer) error {
return encodeDefineAttributeListIdInput(attrNames, writer)
}
var out int64
var err error
decoder := func(reader *cbor.Reader) error {
out, err = decodeDefineAttributeListIdOutput(reader)
return err
}
opt := RequestOptions{Context: ctx}
if err = client.executeWithRetries(opDefineAttributeListId, opt, encoder, decoder); err != nil {
return 0, err
}
return out, nil
}
func (client *SingleDaxClient) defineAttributeList(ctx aws.Context, id int64) ([]string, error) {
if id == emptyAttributeListId {
return []string{}, nil
}
encoder := func(writer *cbor.Writer) error {
return encodeDefineAttributeListInput(id, writer)
}
var out []string
var err error
decoder := func(reader *cbor.Reader) error {
out, err = decodeDefineAttributeListOutput(reader)
return err
}
opt := RequestOptions{Context: ctx}
if err = client.executeWithRetries(opDefineAttributeList, opt, encoder, decoder); err != nil {
return nil, err
}
return out, nil
}
func (client *SingleDaxClient) defineKeySchema(ctx aws.Context, table string) ([]dynamodb.AttributeDefinition, error) {
encoder := func(writer *cbor.Writer) error {
return encodeDefineKeySchemaInput(table, writer)
}
var out []dynamodb.AttributeDefinition
var err error
decoder := func(reader *cbor.Reader) error {
out, err = decodeDefineKeySchemaOutput(reader)
return err
}
opt := RequestOptions{Context: ctx}
if err = client.executeWithRetries(opDefineKeySchema, opt, encoder, decoder); err != nil {
return nil, err
}
return out, nil
}
func (client *SingleDaxClient) PutItemWithOptions(input *dynamodb.PutItemInput, output *dynamodb.PutItemOutput, opt RequestOptions) (*dynamodb.PutItemOutput, error) {
encoder := func(writer *cbor.Writer) error {
return encodePutItemInput(opt.Context, input, client.keySchema, client.attrNamesListToId, writer)
}
var err error
decoder := func(reader *cbor.Reader) error {
output, err = decodePutItemOutput(opt.Context, reader, input, client.keySchema, client.attrListIdToNames, output)
return err
}
if err = client.executeWithRetries(OpPutItem, opt, encoder, decoder); err != nil {
return output, err
}
return output, nil
}
func (client *SingleDaxClient) DeleteItemWithOptions(input *dynamodb.DeleteItemInput, output *dynamodb.DeleteItemOutput, opt RequestOptions) (*dynamodb.DeleteItemOutput, error) {
encoder := func(writer *cbor.Writer) error {
return encodeDeleteItemInput(opt.Context, input, client.keySchema, writer)
}
var err error
decoder := func(reader *cbor.Reader) error {
output, err = decodeDeleteItemOutput(opt.Context, reader, input, client.keySchema, client.attrListIdToNames, output)
return err
}
if err = client.executeWithRetries(OpDeleteItem, opt, encoder, decoder); err != nil {
return output, err
}
return output, nil
}
func (client *SingleDaxClient) UpdateItemWithOptions(input *dynamodb.UpdateItemInput, output *dynamodb.UpdateItemOutput, opt RequestOptions) (*dynamodb.UpdateItemOutput, error) {
encoder := func(writer *cbor.Writer) error {
return encodeUpdateItemInput(opt.Context, input, client.keySchema, writer)
}
var err error
decoder := func(reader *cbor.Reader) error {
output, err = decodeUpdateItemOutput(opt.Context, reader, input, client.keySchema, client.attrListIdToNames, output)
return err
}
if err = client.executeWithRetries(OpUpdateItem, opt, encoder, decoder); err != nil {
return output, err
}
return output, nil
}
func (client *SingleDaxClient) GetItemWithOptions(input *dynamodb.GetItemInput, output *dynamodb.GetItemOutput, opt RequestOptions) (*dynamodb.GetItemOutput, error) {
encoder := func(writer *cbor.Writer) error {
return encodeGetItemInput(opt.Context, input, client.keySchema, writer)
}
var err error
decoder := func(reader *cbor.Reader) error {
output, err = decodeGetItemOutput(opt.Context, reader, input, client.attrListIdToNames, output)
return err
}
if err = client.executeWithRetries(OpGetItem, opt, encoder, decoder); err != nil {
return output, err
}
return output, nil
}
func (client *SingleDaxClient) ScanWithOptions(input *dynamodb.ScanInput, output *dynamodb.ScanOutput, opt RequestOptions) (*dynamodb.ScanOutput, error) {
encoder := func(writer *cbor.Writer) error {
return encodeScanInput(opt.Context, input, client.keySchema, writer)
}
var err error
decoder := func(reader *cbor.Reader) error {
output, err = decodeScanOutput(opt.Context, reader, input, client.keySchema, client.attrListIdToNames, output)
return err
}
if err = client.executeWithRetries(OpScan, opt, encoder, decoder); err != nil {
return output, err
}
return output, nil
}
func (client *SingleDaxClient) QueryWithOptions(input *dynamodb.QueryInput, output *dynamodb.QueryOutput, opt RequestOptions) (*dynamodb.QueryOutput, error) {
encoder := func(writer *cbor.Writer) error {
return encodeQueryInput(opt.Context, input, client.keySchema, writer)
}
var err error
decoder := func(reader *cbor.Reader) error {
output, err = decodeQueryOutput(opt.Context, reader, input, client.keySchema, client.attrListIdToNames, output)
return err
}
if err = client.executeWithRetries(OpQuery, opt, encoder, decoder); err != nil {
return output, err
}
return output, nil
}
func (client *SingleDaxClient) BatchWriteItemWithOptions(input *dynamodb.BatchWriteItemInput, output *dynamodb.BatchWriteItemOutput, opt RequestOptions) (*dynamodb.BatchWriteItemOutput, error) {
encoder := func(writer *cbor.Writer) error {
return encodeBatchWriteItemInput(opt.Context, input, client.keySchema, client.attrNamesListToId, writer)
}
var err error
decoder := func(reader *cbor.Reader) error {
output, err = decodeBatchWriteItemOutput(opt.Context, reader, client.keySchema, client.attrListIdToNames, output)
return err
}
if err = client.executeWithRetries(OpBatchWriteItem, opt, encoder, decoder); err != nil {
return output, err
}
return output, nil
}
func (client *SingleDaxClient) BatchGetItemWithOptions(input *dynamodb.BatchGetItemInput, output *dynamodb.BatchGetItemOutput, opt RequestOptions) (*dynamodb.BatchGetItemOutput, error) {
encoder := func(writer *cbor.Writer) error {
return encodeBatchGetItemInput(opt.Context, input, client.keySchema, writer)
}
var err error
decoder := func(reader *cbor.Reader) error {
output, err = decodeBatchGetItemOutput(opt.Context, reader, input, client.keySchema, client.attrListIdToNames, output)
return err
}
if err = client.executeWithRetries(OpBatchGetItem, opt, encoder, decoder); err != nil {
return output, err
}
return output, nil
}
func (client *SingleDaxClient) NewDaxRequest(op *request.Operation, input, output interface{}, opt RequestOptions) *request.Request {
req := request.New(aws.Config{}, clientInfo, *client.handlers, nil, op, input, output)
opt.applyTo(req)
return req
}
func (client *SingleDaxClient) buildHandlers() *request.Handlers {
h := &request.Handlers{}
h.Build.PushFrontNamed(request.NamedHandler{Name: "dax.BuildHandler", Fn: client.build})
h.Send.PushFrontNamed(request.NamedHandler{Name: "dax.SendHandler", Fn: client.send})
return h
}
func (client *SingleDaxClient) build(req *request.Request) {
var buf bytes.Buffer
w := cbor.NewWriter(&buf)
defer w.Close()
switch req.Operation.Name {
case OpGetItem:
input, ok := req.Params.(*dynamodb.GetItemInput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *GetItemInput", nil)
return
}
if err := encodeGetItemInput(req.Context(), input, client.keySchema, w); err != nil {
req.Error = translateError(err)
return
}
case OpScan:
input, ok := req.Params.(*dynamodb.ScanInput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *ScanInput", nil)
return
}
if err := encodeScanInput(req.Context(), input, client.keySchema, w); err != nil {
req.Error = translateError(err)
return
}
case OpQuery:
input, ok := req.Params.(*dynamodb.QueryInput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *QueryInput", nil)
return
}
if err := encodeQueryInput(req.Context(), input, client.keySchema, w); err != nil {
req.Error = translateError(err)
return
}
case OpBatchGetItem:
input, ok := req.Params.(*dynamodb.BatchGetItemInput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *BatchGetItemInput", nil)
return
}
if err := encodeBatchGetItemInput(req.Context(), input, client.keySchema, w); err != nil {
req.Error = translateError(err)
return
}
case OpPutItem:
input, ok := req.Params.(*dynamodb.PutItemInput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *PutItemInput", nil)
return
}
if err := encodePutItemInput(req.Context(), input, client.keySchema, client.attrNamesListToId, w); err != nil {
req.Error = translateError(err)
return
}
case OpDeleteItem:
input, ok := req.Params.(*dynamodb.DeleteItemInput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *DeleteItemInput", nil)
return
}
if err := encodeDeleteItemInput(req.Context(), input, client.keySchema, w); err != nil {
req.Error = translateError(err)
return
}
case OpUpdateItem:
input, ok := req.Params.(*dynamodb.UpdateItemInput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *UpdateItemInput", nil)
return
}
if err := encodeUpdateItemInput(req.Context(), input, client.keySchema, w); err != nil {
req.Error = translateError(err)
return
}
case OpBatchWriteItem:
input, ok := req.Params.(*dynamodb.BatchWriteItemInput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *BatchWriteItemInput", nil)
return
}
if err := encodeBatchWriteItemInput(req.Context(), input, client.keySchema, client.attrNamesListToId, w); err != nil {
req.Error = translateError(err)
return
}
default:
req.Error = awserr.New(request.InvalidParameterErrCode, "unknown op "+req.Operation.Name, nil)
return
}
req.SetBufferBody(buf.Bytes())
}
func (client *SingleDaxClient) send(req *request.Request) {
opt := RequestOptions{}
if err := opt.mergeFromRequest(req, true); err != nil {
req.Error = err
return
}
switch req.Operation.Name {
case OpGetItem:
input, ok := req.Params.(*dynamodb.GetItemInput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *GetItemInput", nil)
return
}
output, ok := req.Data.(*dynamodb.GetItemOutput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *GetItemOutput", nil)
return
}
req.Data, req.Error = client.GetItemWithOptions(input, output, opt)
case OpScan:
input, ok := req.Params.(*dynamodb.ScanInput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *ScanInput", nil)
return
}
output, ok := req.Data.(*dynamodb.ScanOutput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *ScanOutput", nil)
return
}
req.Data, req.Error = client.ScanWithOptions(input, output, opt)
case OpQuery:
input, ok := req.Params.(*dynamodb.QueryInput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *QueryInput", nil)
return
}
output, ok := req.Data.(*dynamodb.QueryOutput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *QueryOutput", nil)
return
}
req.Data, req.Error = client.QueryWithOptions(input, output, opt)
case OpBatchGetItem:
input, ok := req.Params.(*dynamodb.BatchGetItemInput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *BatchGetItemInput", nil)
return
}
output, ok := req.Data.(*dynamodb.BatchGetItemOutput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *BatchGetItemOutput", nil)
return
}
req.Data, req.Error = client.BatchGetItemWithOptions(input, output, opt)
case OpPutItem:
input, ok := req.Params.(*dynamodb.PutItemInput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *PutItemInput", nil)
return
}
output, ok := req.Data.(*dynamodb.PutItemOutput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *PutItemOutput", nil)
return
}
req.Data, req.Error = client.PutItemWithOptions(input, output, opt)
case OpDeleteItem:
input, ok := req.Params.(*dynamodb.DeleteItemInput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *DeleteItemInput", nil)
return
}
output, ok := req.Data.(*dynamodb.DeleteItemOutput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *DeleteItemOutput", nil)
return
}
req.Data, req.Error = client.DeleteItemWithOptions(input, output, opt)
case OpUpdateItem:
input, ok := req.Params.(*dynamodb.UpdateItemInput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *UpdateItemInput", nil)
return
}
output, ok := req.Data.(*dynamodb.UpdateItemOutput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *UpdateItemOutput", nil)
return
}
req.Data, req.Error = client.UpdateItemWithOptions(input, output, opt)
case OpBatchWriteItem:
input, ok := req.Params.(*dynamodb.BatchWriteItemInput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *BatchWriteItemInput", nil)
return
}
output, ok := req.Data.(*dynamodb.BatchWriteItemOutput)
if !ok {
req.Error = awserr.New(request.ErrCodeSerialization, "expected *BatchWriteItemOutput", nil)
return
}
req.Data, req.Error = client.BatchWriteItemWithOptions(input, output, opt)
default:
req.Error = awserr.New(request.InvalidParameterErrCode, "unknown op "+req.Operation.Name, nil)
return
}
}
func (client *SingleDaxClient) newContext(o RequestOptions) aws.Context {
if o.Context != nil {
return o.Context
}
return aws.BackgroundContext()
}
func (client *SingleDaxClient) executeWithRetries(op string, o RequestOptions, encoder func(writer *cbor.Writer) error, decoder func(reader *cbor.Reader) error) error {
ctx := client.newContext(o)
attempts := o.MaxRetries + 1
var err error
for i := 0; i < attempts; i++ {
if i > 0 && o.Logger != nil && o.LogLevel.Matches(aws.LogDebugWithRequestRetries) {
o.Logger.Log(fmt.Sprintf("DEBUG: Retrying Request %s/%s, attempt %d", service, op, i+1))
}
if err = client.executeWithContext(ctx, op, encoder, decoder); err == nil {
return nil
} else if ctx != nil && err == ctx.Err() {
return awserr.New(request.CanceledErrorCode, "request context canceled", err)
}
d := o.RetryDelay
if d > 0 {
if s := o.SleepDelayFn; s != nil {
s(d)
} else if err = aws.SleepWithContext(ctx, d); err != nil {
return awserr.New(request.CanceledErrorCode, "request context canceled", err)
}
}
}
if err != nil {
return translateError(err)
} else {
return awserr.New("UnknownError", "ran out of retries", nil)
}
}
func (client *SingleDaxClient) executeWithContext(ctx aws.Context, op string, encoder func(writer *cbor.Writer) error, decoder func(reader *cbor.Reader) error) error {
tube, err := client.pool.getWithContext(ctx, client.isHighPriority(op))
if err != nil {
return err
}
if err = client.pool.setDeadline(tube, ctx); err != nil {
return err
}
if err = client.auth(tube); err != nil {
client.recycleTube(tube, err)
return err
}
writer := tube.cborWriter
if err = encoder(tube.cborWriter); err != nil {
client.recycleTube(tube, err)
return err
}
if err := writer.Flush(); err != nil {
client.recycleTube(tube, err)
return err
}
reader := tube.cborReader
ex, err := decodeError(reader)
if err != nil { // decode or network error
client.recycleTube(tube, err)
return err
}
if ex != nil { // user or server error
client.recycleTube(tube, nil) // do not close conn
return ex
}
err = decoder(reader)
client.recycleTube(tube, err)
return err
}
func (client *SingleDaxClient) isHighPriority(op string) bool {
switch op {
case opDefineAttributeListId, opDefineAttributeList, opDefineKeySchema:
return true
default:
return false
}
}
func (client *SingleDaxClient) recycleTube(tube *tube, err error) {
if tube == nil {
return
}
var recycle bool
if err == nil {
recycle = true
} else {
// IO streams are guaranteed to be completely drained only on daxRequestException
d, ok := err.(*daxRequestFailure)
recycle = ok
if ok && d.authError() {
tube.setAuthExpiryUnix(time.Now().Unix())
}
}
if recycle {
client.pool.put(tube)
} else {
client.pool.discard(tube)
}
}
func (client *SingleDaxClient) auth(tube *tube) error {
// TODO credentials.Get() cause a throughput drop of ~25 with 250 goroutines with DefaultCredentialChain (only instance profile credentials available)
creds, err := client.credentials.Get()
if err != nil {
return err
}
now := time.Now().UTC()
if tube.compareAndSwapAuthId(creds.AccessKeyID) || tube.getAuthExpiryUnix() <= now.Unix() {
stringToSign, signature := generateSigV4WithTime(creds, daxAddress, client.region, "", now)
writer := tube.cborWriter
if err := encodeAuthInput(creds.AccessKeyID, creds.SessionToken, stringToSign, signature, userAgent, writer); err != nil {
return err
}
if err := writer.Flush(); err != nil {
return err
}
tube.setAuthExpiryUnix(now.Unix() + client.tubeAuthWindowSecs)
}
return nil
}
func (client *SingleDaxClient) reapIdleConnections() {
client.pool.reapIdleConnections()
}