-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp.go
315 lines (258 loc) · 7.23 KB
/
tcp.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
// Copyright 2020 Ye Zi Jie. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
//
// Author: FishGoddess
// Email: [email protected]
// Created at 2020/11/17 22:34:49
package kafo
import (
"encoding/binary"
"encoding/json"
"fmt"
"strings"
"sync"
"time"
"stathat.com/c/consistent"
)
const (
// getCommand is the command of get operation.
getCommand = byte(1)
// setCommand is the command of set operation.
setCommand = byte(2)
// deleteCommand is the command of delete operation.
deleteCommand = byte(3)
// statusCommand is the command of status operation.
statusCommand = byte(4)
// nodesCommand is the command of nodes operation.
nodesCommand = byte(5)
// redirectPrefix is the prefix of redirect error.
redirectPrefix = "redirect to node "
)
// TCPClient is the client of tcp Network.
type TCPClient struct {
// config stores all configurations of connections.
config *Config
// data stores all connections.
data map[string]*connection
// deadlines stores all deadline of connections.
deadlines map[string]time.Time
// circle stores the relation of data and node.
circle *consistent.Consistent
// lock is for safe-concurrency.
lock *sync.Mutex
}
// NewTCPClient returns a new tcp client with given config and an error if failed.
func NewTCPClient(addresses []string, config Config) (*TCPClient, error) {
client := &TCPClient{
config: &config,
data: map[string]*connection{},
deadlines: map[string]time.Time{},
lock: &sync.Mutex{},
}
errorCount := 0
for _, address := range addresses {
err := client.addConnection(address)
if err != nil {
errorCount++
}
}
if errorCount == len(addresses) {
return client, fmt.Errorf("failed after trying to add %d connections", errorCount)
}
client.circle = consistent.New()
client.circle.NumberOfReplicas = config.NumberOfReplicas
client.circle.Set(addresses)
client.updateCircle()
go client.autoGc()
go client.autoUpdateCircle()
return client, nil
}
// autoGc clears all dead connections at fixed duration.
func (tc *TCPClient) autoGc() {
ticker := time.NewTicker(tc.config.GcDuration)
for {
select {
case <-ticker.C:
tc.lock.Lock()
for address, conn := range tc.data {
if deadline, ok := tc.deadlines[address]; !ok || deadline.Before(time.Now()) {
conn.close()
delete(tc.data, address)
delete(tc.deadlines, address)
}
}
tc.lock.Unlock()
}
}
}
// getConnection returns connection of address or not.
func (tc *TCPClient) getConnection(address string) (*connection, bool) {
conn, ok := tc.data[address]
if !ok {
return nil, false
}
if deadline, ok := tc.deadlines[address]; !ok || deadline.Before(time.Now()) {
conn.close()
delete(tc.data, address)
delete(tc.deadlines, address)
return nil, false
}
return conn, true
}
// addConnection adds a new connection of address to tc.
func (tc *TCPClient) addConnection(address string) error {
conn, err := newConnection(address, tc.config)
if err != nil {
return err
}
tc.data[address] = conn
tc.deadlines[address] = time.Now().Add(tc.config.Ttl)
return nil
}
// nodes returns nodes of cluster and an error if failed.
func (tc *TCPClient) nodes() ([]string, error) {
addresses := tc.circle.Members()
for _, address := range addresses {
conn, ok := tc.getConnection(address)
if !ok {
continue
}
body, err := conn.do(nodesCommand, nil)
if err != nil {
continue
}
var nodes []string
return nodes, json.Unmarshal(body, &nodes)
}
return nil, fmt.Errorf("failed to get nodes after connecting %d addresses", len(addresses))
}
// updateCircle will update hash circle to cluster.
func (tc *TCPClient) updateCircle() {
for i := 0; i < tc.config.MaxRetryTimes; i++ {
nodes, err := tc.nodes()
if err == nil {
tc.circle.Set(nodes)
}
}
}
// autoUpdateCircle updates circle at fixed duration.
func (tc *TCPClient) autoUpdateCircle() {
ticker := time.NewTicker(tc.config.UpdateCircleDuration)
for {
select {
case <-ticker.C:
tc.lock.Lock()
tc.updateCircle()
tc.lock.Unlock()
}
}
}
// do will use key to get a address and try to connect it.
// If connection is ok, then command and args will be sent through this connection.
func (tc *TCPClient) do(key string, command byte, args [][]byte) (body []byte, err error) {
tc.lock.Lock()
defer tc.lock.Unlock()
// Guess why we do this? Because we think the first time adding connection is not counted as retry
maxRetryTimes := 3
if tc.config.MaxRetryTimes > 3 {
maxRetryTimes = tc.config.MaxRetryTimes
}
var address string
for i := 0; i < maxRetryTimes; i++ {
// Select address of key
address, err = tc.circle.Get(key)
if err != nil {
continue
}
// Get connection of address and add a new one if not found
conn, ok := tc.getConnection(address)
if !ok {
err = tc.addConnection(address)
continue
}
// Use connection to do something
tc.lock.Unlock()
body, err = conn.do(command, args)
tc.lock.Lock()
if err != nil {
errMsg := err.Error()
if strings.HasPrefix(errMsg, redirectPrefix) {
address = strings.TrimPrefix(err.Error(), redirectPrefix)
continue
}
// An existing connection was forcibly closed by the remote host
if strings.Contains(errMsg, "closed by the remote host") {
if nodes, err := tc.nodes(); err == nil {
tc.circle.Set(nodes)
continue
}
}
}
return body, nil
}
return nil, fmt.Errorf("failed after trying %d times due to %v", maxRetryTimes, err)
}
// Get returns the value of key and an error if failed.
func (tc *TCPClient) Get(key string) ([]byte, error) {
return tc.do(key, getCommand, [][]byte{[]byte(key)})
}
// Set adds the key and value with given ttl to cache.
// Returns an error if failed.
func (tc *TCPClient) Set(key string, value []byte, ttl int64) error {
ttlBytes := make([]byte, 8)
binary.BigEndian.PutUint64(ttlBytes, uint64(ttl))
_, err := tc.do(key, setCommand, [][]byte{ttlBytes, []byte(key), value})
return err
}
// Delete deletes the value of key and returns an error if failed.
func (tc *TCPClient) Delete(key string) error {
_, err := tc.do(key, deleteCommand, [][]byte{[]byte(key)})
return err
}
// Status returns the status of kafo and an error if failed.
func (tc *TCPClient) Status() (*Status, error) {
tc.lock.Lock()
defer tc.lock.Unlock()
// Fetch from all nodes
result := &Status{}
addresses := tc.circle.Members()
for _, address := range addresses {
conn, ok := tc.getConnection(address)
if !ok {
if err := tc.addConnection(address); err != nil {
continue
}
if conn, ok = tc.getConnection(address); !ok {
continue
}
}
body, err := conn.do(statusCommand, nil)
status := &Status{}
err = json.Unmarshal(body, status)
if err != nil {
continue
}
result.Count += status.Count
result.KeySize += status.KeySize
result.ValueSize += status.ValueSize
}
return result, nil
}
// Nodes returns the nodes of cluster and an error if failed.
func (tc *TCPClient) Nodes() ([]string, error) {
tc.lock.Lock()
defer tc.lock.Unlock()
return tc.nodes()
}
// Close closes all connections.
func (tc *TCPClient) Close() {
tc.lock.Lock()
defer tc.lock.Unlock()
for _, conn := range tc.data {
conn.close()
}
for address, _ := range tc.deadlines {
delete(tc.deadlines, address)
}
}