forked from aws/aws-dax-go
/
service.go
129 lines (109 loc) · 2.99 KB
/
service.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
/*
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 dax
import (
"context"
"github.com/aws/aws-dax-go/dax/internal/client"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
"time"
)
// Dax makes requests to the Amazon DAX API, which conforms to the DynamoDB API.
// Dax methods are safe to use concurrently
type Dax struct {
client client.DaxAPI
config Config
}
const ServiceName = "dax"
type Config struct {
client.Config
// Default request options
RequestTimeout time.Duration
WriteRetries int
ReadRetries int
LogLevel aws.LogLevelType
Logger aws.Logger
}
var defaultConfig = Config{
Config: client.DefaultConfig(),
RequestTimeout: 1 * time.Minute,
WriteRetries: 2,
ReadRetries: 2,
LogLevel: aws.LogOff,
Logger: aws.NewDefaultLogger(),
}
func DefaultConfig() Config {
return defaultConfig
}
func New(cfg Config) (*Dax, error) {
c, err := client.New(cfg.Config)
if err != nil {
return nil, err
}
return &Dax{client: c, config: cfg}, nil
}
func NewWithSession(session session.Session) (*Dax, error) {
if err := client.ValidateHandlers(session.Handlers, false); err != nil {
return nil, err
}
dc := DefaultConfig()
if session.Config != nil {
if err := client.ValidateConfig(*session.Config, false); err != nil {
return nil, err
}
dc.mergeFrom(*session.Config)
}
return New(dc)
}
func (c *Config) mergeFrom(ac aws.Config) {
if r := ac.MaxRetries; r != nil {
c.WriteRetries = *r
c.ReadRetries = *r
}
if ac.Logger != nil {
c.Logger = ac.Logger
}
if ac.LogLevel != nil {
c.LogLevel = *ac.LogLevel
}
if ac.Credentials != nil {
c.Credentials = ac.Credentials
}
if ac.Endpoint != nil {
c.HostPorts = []string{*ac.Endpoint}
}
if ac.Region != nil {
c.Region = *ac.Region
}
}
func (c *Config) requestOptions(read bool, ctx context.Context, opts ...request.Option) (client.RequestOptions, context.CancelFunc, error) {
r := c.WriteRetries
if read {
r = c.ReadRetries
}
var cfn context.CancelFunc
if ctx == nil && c.RequestTimeout > 0 {
ctx, cfn = context.WithTimeout(aws.BackgroundContext(), c.RequestTimeout)
}
opt := client.RequestOptions{
LogLevel: c.LogLevel,
Logger: c.Logger,
MaxRetries: r,
}
if err := opt.MergeFromRequestOptions(ctx, opts...); err != nil {
return client.RequestOptions{}, nil, err
}
return opt, cfn, nil
}
var _ dynamodbiface.DynamoDBAPI = (*Dax)(nil)