-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
203 lines (157 loc) · 4.88 KB
/
server.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
/*
Copyright 2019 Jan Berktold <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License 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 memcachey
import (
"errors"
"net"
"sync/atomic"
"time"
"github.com/fatih/pool"
"github.com/serialx/hashring"
)
type connectionProvider interface {
ForKey(key string) (net.Conn, error)
ForKeys(keys []string) (map[net.Conn][]string, error)
ForAddress(address string) (net.Conn, error)
ForEach() ([]net.Conn, error)
}
type roundRobinConnectionProvider struct {
pools []pool.Pool
poolsByAddress map[string]pool.Pool
roundRobinIndex uint64
}
func newRoundRobinConnectionProvider(addresses []string, minCons, maxCons int, connectTimeout time.Duration) (*roundRobinConnectionProvider, error) {
pools, poolsByAddress, err := buildPools(addresses, minCons, maxCons, connectTimeout)
if err != nil {
return nil, err
}
return &roundRobinConnectionProvider{
pools: pools,
poolsByAddress: poolsByAddress,
}, nil
}
func (p *roundRobinConnectionProvider) ForKey(key string) (net.Conn, error) {
currentIndex := atomic.AddUint64(&p.roundRobinIndex, 1) % uint64(len(p.pools))
pool := p.pools[currentIndex]
return pool.Get()
}
func (p *roundRobinConnectionProvider) ForKeys(keys []string) (map[net.Conn][]string, error) {
result := make(map[net.Conn][]string, 1)
if conn, err := p.ForKey(""); err == nil {
result[conn] = keys
} else {
return nil, err
}
return result, nil
}
func (p *roundRobinConnectionProvider) ForAddress(address string) (net.Conn, error) {
if pool, ok := p.poolsByAddress[address]; ok {
return pool.Get()
}
return nil, ErrNoSuchAddress
}
func (p *roundRobinConnectionProvider) ForEach() ([]net.Conn, error) {
result := make([]net.Conn, len(p.pools))
for i, pool := range p.pools {
conn, err := pool.Get()
if err != nil {
return nil, err
}
result[i] = conn
}
return result, nil
}
type consistentHashConnnectionProvider struct {
ring *hashring.HashRing
pools []pool.Pool
poolsByAddress map[string]pool.Pool
}
func newConsistentHashConnectionProvider(addresses []string, minCons, maxCons int, connectTimeout time.Duration) (*consistentHashConnnectionProvider, error) {
ring := hashring.New(addresses)
pools, poolsByAddress, err := buildPools(addresses, minCons, maxCons, connectTimeout)
if err != nil {
return nil, err
}
return &consistentHashConnnectionProvider{
ring: ring,
pools: pools,
poolsByAddress: poolsByAddress,
}, nil
}
func (p *consistentHashConnnectionProvider) ForKey(key string) (net.Conn, error) {
address, ok := p.ring.GetNode(key)
if !ok {
return nil, errors.New("failed to get slot")
}
return p.poolsByAddress[address].Get()
}
func (p *consistentHashConnnectionProvider) ForKeys(keys []string) (map[net.Conn][]string, error) {
addressToKeys := map[string][]string{}
for _, key := range keys {
address, ok := p.ring.GetNode(key)
if !ok {
return nil, errors.New("failed to get slot")
}
if array, ok := addressToKeys[address]; ok {
addressToKeys[address] = append(array, key)
} else {
addressToKeys[address] = []string{key}
}
}
connectionsToKeys := map[net.Conn][]string{}
for address, keys := range addressToKeys {
conn, err := p.poolsByAddress[address].Get()
if err != nil {
return nil, err
}
connectionsToKeys[conn] = keys
}
return connectionsToKeys, nil
}
func (p *consistentHashConnnectionProvider) ForAddress(address string) (net.Conn, error) {
if pool, ok := p.poolsByAddress[address]; ok {
return pool.Get()
}
return nil, ErrNoSuchAddress
}
func (p *consistentHashConnnectionProvider) ForEach() ([]net.Conn, error) {
result := make([]net.Conn, len(p.pools))
for i, pool := range p.pools {
conn, err := pool.Get()
if err != nil {
return nil, err
}
result[i] = conn
}
return result, nil
}
func buildPools(addresses []string, minCons, maxCons int, connectTimeout time.Duration) ([]pool.Pool, map[string]pool.Pool, error) {
pools := make([]pool.Pool, len(addresses))
poolsByAddress := make(map[string]pool.Pool, len(addresses))
for i, address := range addresses {
address := address
p, err := pool.NewChannelPool(minCons, maxCons, func() (net.Conn, error) {
conn, err := net.DialTimeout("tcp", address, connectTimeout)
if err != nil {
return nil, err
}
return conn, nil
})
if err != nil {
return nil, nil, err
}
pools[i] = p
poolsByAddress[address] = p
}
return pools, poolsByAddress, nil
}