-
Notifications
You must be signed in to change notification settings - Fork 48
/
service.go
198 lines (176 loc) · 5.37 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
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
/*
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"
"crypto/tls"
"errors"
"fmt"
"net"
"net/url"
"time"
"github.com/aws/aws-dax-go/dax/internal/client"
"github.com/aws/aws-dax-go/dax/internal/proxy"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client/metadata"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
)
// 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
}
// DefaultConfig returns the default DAX configuration.
//
// Config.Region and Config.HostPorts still need to be configured properly
// to start up a DAX client.
func DefaultConfig() Config {
return Config{
Config: client.DefaultConfig(),
RequestTimeout: 1 * time.Minute,
WriteRetries: 2,
ReadRetries: 2,
LogLevel: aws.LogOff,
Logger: aws.NewDefaultLogger(),
}
}
// NewWithSession creates a new instance of the DAX config with a session.
//
// Only configurations relevent to DAX will be used, others will be ignored.
func NewConfigWithSession(session session.Session) Config {
dc := DefaultConfig()
if session.Config != nil {
dc.mergeFrom(*session.Config)
}
return dc
}
// New creates a new instance of the DAX client with a DAX configuration.
func New(cfg Config) (*Dax, error) {
cfg.Config.SetLogger(cfg.Logger, cfg.LogLevel)
c, err := client.New(cfg.Config)
if err != nil {
if cfg.Logger != nil {
cfg.Logger.Log(fmt.Sprintf("ERROR: Exception in initialisation of DAX Client : %s", err))
}
return nil, err
}
return &Dax{client: c, config: cfg}, nil
}
// SecureDialContext creates a secure DialContext for connecting to encrypted cluster
func SecureDialContext(endpoint string, skipHostnameVerification bool) (func(ctx context.Context, network string, address string) (net.Conn, error), error) {
dialer := &proxy.Dialer{}
var cfg tls.Config
if skipHostnameVerification {
cfg = tls.Config{InsecureSkipVerify: true}
} else {
u, err := url.ParseRequestURI(endpoint)
if err != nil {
return nil, err
}
cfg = tls.Config{ServerName: u.Hostname()}
}
dialer.Config = &cfg
return dialer.DialContext, nil
}
// NewWithSession creates a new instance of the DAX client with a session.
//
// Only configurations relevent to DAX will be used, others will be ignored.
//
// Example:
// mySession := session.Must(session.NewSession(
// &aws.Config{
// Region: aws.String("us-east-1"),
// Endpoint: aws.String("dax://mycluster.frfx8h.clustercfg.dax.usw2.amazonaws.com:8111"),
// }))
//
// // Create a DAX client from just a session.
// svc := dax.NewWithSession(mySession)
func NewWithSession(session session.Session) (*Dax, error) {
dc := DefaultConfig()
if session.Config != nil {
dc.mergeFrom(*session.Config)
}
return New(dc)
}
func (c *Config) mergeFrom(ac aws.Config) {
if r := ac.MaxRetries; r != nil && *r != aws.UseServiceDefaultRetries {
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 {
if c.Logger != nil && c.LogLevel.AtLeast(aws.LogDebug) {
c.Logger.Log(fmt.Sprintf("DEBUG: Error in merging from Request Options : %s", err))
}
return client.RequestOptions{}, nil, err
}
return opt, cfn, nil
}
func buildHandlersForUnimplementedOperations() *request.Handlers {
h := &request.Handlers{}
h.Build.PushFrontNamed(request.NamedHandler{
Name: "dax.BuildHandler",
Fn: func(r *request.Request) {
r.Error = errors.New(client.ErrCodeNotImplemented)
return
}})
return h
}
var handlersForUnimplementedOperations = buildHandlersForUnimplementedOperations()
func newRequestForUnimplementedOperation() *request.Request {
op := &request.Operation{Name: "Unimplemented"}
clientInfo := metadata.ClientInfo{ServiceName: "dax"}
req := request.New(aws.Config{}, clientInfo, *handlersForUnimplementedOperations, nil, op, nil, nil)
return req
}