forked from couchbase/gocbcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemdclientdialer_component.go
More file actions
208 lines (177 loc) · 5.81 KB
/
memdclientdialer_component.go
File metadata and controls
208 lines (177 loc) · 5.81 KB
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
package gocbcore
import (
"context"
"crypto/tls"
"errors"
"sync"
"time"
)
type memdClientDialerComponent struct {
kvConnectTimeout time.Duration
serverWaitTimeout time.Duration
clientID string
breakerCfg CircuitBreakerConfig
dcpQueueSize int
compressionMinSize int
compressionMinRatio float64
disableDecompression bool
connBufSize uint
serverFailuresLock sync.Mutex
serverFailures map[string]time.Time
tracer *tracerComponent
zombieLogger *zombieLoggerComponent
bootstrapProps bootstrapProps
bootstrapInitFunc memdInitFunc
bootstrapFailHandlersLock sync.Mutex
bootstrapFailHandlers []memdBoostrapFailHandler
noTLSSeedNode bool
}
type memdClientDialerProps struct {
KVConnectTimeout time.Duration
ServerWaitTimeout time.Duration
ClientID string
DCPQueueSize int
CompressionMinSize int
CompressionMinRatio float64
DisableDecompression bool
NoTLSSeedNode bool
ConnBufSize uint
}
type memdBoostrapFailHandler interface {
onBootstrapFail(error)
}
func newMemdClientDialerComponent(props memdClientDialerProps, bSettings bootstrapProps, breakerCfg CircuitBreakerConfig,
zLogger *zombieLoggerComponent, tracer *tracerComponent, bootstrapInitFunc memdInitFunc) *memdClientDialerComponent {
return &memdClientDialerComponent{
kvConnectTimeout: props.KVConnectTimeout,
serverWaitTimeout: props.ServerWaitTimeout,
clientID: props.ClientID,
breakerCfg: breakerCfg,
zombieLogger: zLogger,
tracer: tracer,
serverFailures: make(map[string]time.Time),
bootstrapProps: bSettings,
bootstrapInitFunc: bootstrapInitFunc,
dcpQueueSize: props.DCPQueueSize,
compressionMinSize: props.CompressionMinSize,
compressionMinRatio: props.CompressionMinRatio,
disableDecompression: props.DisableDecompression,
noTLSSeedNode: props.NoTLSSeedNode,
connBufSize: props.ConnBufSize,
}
}
func (mcc *memdClientDialerComponent) AddBootstrapFailHandler(handler memdBoostrapFailHandler) {
mcc.bootstrapFailHandlersLock.Lock()
mcc.bootstrapFailHandlers = append(mcc.bootstrapFailHandlers, handler)
mcc.bootstrapFailHandlersLock.Unlock()
}
func (mcc *memdClientDialerComponent) RemoveBootstrapFailHandler(handler memdBoostrapFailHandler) {
var idx int
mcc.bootstrapFailHandlersLock.Lock()
for i, w := range mcc.bootstrapFailHandlers {
if w == handler {
idx = i
}
}
if idx == len(mcc.bootstrapFailHandlers) {
mcc.bootstrapFailHandlers = mcc.bootstrapFailHandlers[:idx]
} else {
mcc.bootstrapFailHandlers = append(mcc.bootstrapFailHandlers[:idx], mcc.bootstrapFailHandlers[idx+1:]...)
}
mcc.bootstrapFailHandlersLock.Unlock()
}
func (mcc *memdClientDialerComponent) SlowDialMemdClient(cancelSig <-chan struct{}, address routeEndpoint, tlsConfig *dynTLSConfig,
auth authFuncHandler, authMechanisms []AuthMechanism, postCompleteHandler postCompleteErrorHandler) (*memdClient, error) {
mcc.serverFailuresLock.Lock()
failureTime := mcc.serverFailures[address.Address]
mcc.serverFailuresLock.Unlock()
if !failureTime.IsZero() {
waitedTime := time.Since(failureTime)
if waitedTime < mcc.serverWaitTimeout {
select {
case <-cancelSig:
return nil, errRequestCanceled
case <-time.After(mcc.serverWaitTimeout - waitedTime):
}
}
}
deadline := time.Now().Add(mcc.kvConnectTimeout)
client, err := mcc.dialMemdClient(cancelSig, address, deadline, postCompleteHandler, tlsConfig)
if err != nil {
if !errors.Is(err, ErrRequestCanceled) {
mcc.serverFailuresLock.Lock()
mcc.serverFailures[address.Address] = time.Now()
mcc.serverFailuresLock.Unlock()
}
return nil, err
}
err = client.Bootstrap(cancelSig, mcc.bootstrapProps, deadline, authMechanisms, auth, mcc.bootstrapInitFunc)
if err != nil {
closeErr := client.Close()
if closeErr != nil {
logWarnf("Failed to close authentication client (%s)", closeErr)
}
if !errors.Is(err, ErrForcedReconnect) {
mcc.serverFailuresLock.Lock()
mcc.serverFailures[address.Address] = time.Now()
mcc.serverFailuresLock.Unlock()
}
mcc.bootstrapFailHandlersLock.Lock()
handlers := make([]memdBoostrapFailHandler, len(mcc.bootstrapFailHandlers))
copy(handlers, mcc.bootstrapFailHandlers)
mcc.bootstrapFailHandlersLock.Unlock()
for _, handler := range handlers {
handler.onBootstrapFail(err)
}
return nil, err
}
return client, nil
}
func (mcc *memdClientDialerComponent) dialMemdClient(cancelSig <-chan struct{}, address routeEndpoint, deadline time.Time,
postCompleteHandler postCompleteErrorHandler, dynTls *dynTLSConfig) (*memdClient, error) {
// Copy the tls configuration since we need to provide the hostname for each
// server that we connect to so that the certificate can be validated properly.
var tlsConfig *tls.Config
if dynTls != nil && !(mcc.noTLSSeedNode && address.IsSeedNode) {
srvTLSConfig, err := dynTls.MakeForAddr(address.Address)
if err != nil {
return nil, err
}
tlsConfig = srvTLSConfig
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
select {
case <-ctx.Done():
return
case <-cancelSig:
cancel()
}
}()
conn, err := dialMemdConn(ctx, address.Address, tlsConfig, deadline, mcc.connBufSize)
cancel()
if err != nil {
if errors.Is(err, context.Canceled) {
err = errRequestCanceled
} else {
err = wrapError(err, "check server ports and cluster encryption setting")
}
logDebugf("Failed to connect. %v", err)
return nil, err
}
client := newMemdClient(
memdClientProps{
ClientID: mcc.clientID,
DCPQueueSize: mcc.dcpQueueSize,
DisableDecompression: mcc.disableDecompression,
CompressionMinRatio: mcc.compressionMinRatio,
CompressionMinSize: mcc.compressionMinSize,
},
conn,
mcc.breakerCfg,
postCompleteHandler,
mcc.tracer,
mcc.zombieLogger,
)
return client, err
}