-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsets.go
335 lines (269 loc) · 6.51 KB
/
sets.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package actorkit
import (
"math/rand"
"sync"
"sync/atomic"
"github.com/serialx/hashring"
)
//**************************************
// RandomSet
//**************************************
// RandomSet implements a element set which returns
// a random item on every call to it's Get() method.
// It uses the internal random package, which is not
// truly random.
type RandomSet struct {
procs []string
set map[string]int
}
// NewRandomSet returns a new instance of RandomSet.
func NewRandomSet() *RandomSet {
return &RandomSet{
set: map[string]int{},
}
}
// Get will return the next Process in a round-robin
// random fashion, allowing some form of distributed calls
// for different process to handle messages.
func (p *RandomSet) Get() string {
total := len(p.procs)
target := rand.Intn(total)
return p.procs[target]
}
// Total returns current total of items in round robin.
func (p *RandomSet) Total() int {
return len(p.procs)
}
// Remove removes giving item from set.
func (p *RandomSet) Remove(proc string) {
if !p.Has(proc) {
return
}
index := p.set[proc]
delete(p.set, proc)
last := len(p.procs) - 1
if last == 1 {
p.procs = nil
return
}
lastItem := p.procs[last]
p.procs[index] = lastItem
p.procs = p.procs[:last]
}
// Add adds giving item into set.
func (p *RandomSet) Add(proc string) {
if p.Has(proc) {
return
}
pIndex := len(p.procs)
p.procs = append(p.procs, proc)
p.set[proc] = pIndex
}
// Has returns true/false if giving item is in set.
func (p *RandomSet) Has(s string) bool {
_, ok := p.set[s]
return ok
}
//**************************************
// HashedSet
//**************************************
// HashedSet implements a giving set which is unique in that
// it has a hash ring underline which is encoded to return specific
// keys for specific hash strings. It allows consistently retrieving
// same key for same hash.
type HashedSet struct {
set map[string]struct{}
hashing *hashring.HashRing
}
// NewHashedSet returns a new instance of HashedSet.
func NewHashedSet(set []string) *HashedSet {
var hashed HashedSet
hashed.set = map[string]struct{}{}
hashed.hashing = hashring.New(set)
for _, k := range set {
hashed.set[k] = struct{}{}
}
return &hashed
}
// Get returns a giving item for provided hash value.
func (hs *HashedSet) Get(hashed string) (string, bool) {
if content, ok := hs.hashing.GetNode(hashed); ok {
return content, ok
}
return "", false
}
// Add adds giving item into set.
func (hs *HashedSet) Add(n string) {
hs.hashing = hs.hashing.AddNode(n)
hs.set[n] = struct{}{}
}
// Remove removes giving item from set.
func (hs *HashedSet) Remove(n string) {
hs.hashing = hs.hashing.RemoveNode(n)
delete(hs.set, n)
}
// Has returns true/false if giving item is in set.
func (hs *HashedSet) Has(n string) bool {
_, ok := hs.set[n]
return ok
}
//**************************************
// RoundRobinSet
//**************************************
// RoundRobinSet defines a process set/group which
// are processes offering the same service contract and
// will randomly based on index be provided when a process
// is needed for communication.
type RoundRobinSet struct {
lastIndex int32
procs []string
set map[string]int
}
// NewRoundRobinSet returns a new instance of RoundRobinSet.
func NewRoundRobinSet() *RoundRobinSet {
return &RoundRobinSet{
set: map[string]int{},
}
}
// Get will return the next Process in a round-robin
// random fashion, allowing some form of distributed calls
// for different process to handle messages.
func (p *RoundRobinSet) Get() string {
var lastIndex int32
total := int32(len(p.procs))
if atomic.LoadInt32(&p.lastIndex) >= total {
atomic.StoreInt32(&p.lastIndex, -1)
}
lastIndex = atomic.AddInt32(&p.lastIndex, 1)
target := int(lastIndex % total)
return p.procs[target]
}
// Total returns current total of items in round robin.
func (p *RoundRobinSet) Total() int {
return len(p.procs)
}
// Remove removes giving item from set.
func (p *RoundRobinSet) Remove(proc string) {
if !p.Has(proc) {
return
}
index := p.set[proc]
delete(p.set, proc)
last := len(p.procs) - 1
if last == 1 {
p.procs = nil
return
}
lastItem := p.procs[last]
p.procs[index] = lastItem
p.procs = p.procs[:last]
}
// Add adds giving item into set.
func (p *RoundRobinSet) Add(proc string) {
if p.Has(proc) {
return
}
pIndex := len(p.procs)
p.procs = append(p.procs, proc)
p.set[proc] = pIndex
}
// Has returns true/false if giving item is in set.
func (p *RoundRobinSet) Has(s string) bool {
_, ok := p.set[s]
return ok
}
//**************************************
// locality
//**************************************
type localitySrc interface {
Remove(*localityIndex)
Get(*localityIndex) interface{}
}
type localityIndex struct {
index int
src localitySrc
}
func (l *localityIndex) Remove() {
l.src.Remove(l)
}
func (l *localityIndex) Get() interface{} {
return l.src.Get(l)
}
//**************************************
// LocalityMap
//**************************************
type localityMap struct {
rl sync.RWMutex
items map[int]interface{}
}
func (l *localityMap) Get(index *localityIndex) interface{} {
if index.index == -1 {
return nil
}
l.rl.RLock()
defer l.rl.RUnlock()
return l.items[index.index]
}
func (l *localityMap) Remove(index *localityIndex) {
if index.index == -1 {
return
}
l.rl.Lock()
defer l.rl.Unlock()
total := len(l.items)
if total == 1 {
l.items = nil
index.index = 1
return
}
l.items[index.index] = l.items[total-1]
delete(l.items, total-1)
}
func (l *localityMap) Add(item interface{}) *localityIndex {
index := new(localityIndex)
index.src = l
l.rl.Lock()
defer l.rl.Unlock()
index.index = len(l.items)
l.items[index.index] = item
return index
}
//**************************************
// localityList
//**************************************
type localityList struct {
rl sync.RWMutex
items []interface{}
}
func (l *localityList) Get(index *localityIndex) interface{} {
if index.index == -1 {
return nil
}
l.rl.RLock()
defer l.rl.RUnlock()
return l.items[index.index]
}
func (l *localityList) Remove(index *localityIndex) {
if index.index == -1 {
return
}
l.rl.Lock()
defer l.rl.Unlock()
total := len(l.items)
if total == 1 {
l.items = nil
index.index = 1
return
}
l.items[index.index] = l.items[total-1]
l.items = l.items[:total-1]
}
func (l *localityList) Add(item interface{}) *localityIndex {
index := new(localityIndex)
index.src = l
l.rl.Lock()
defer l.rl.Unlock()
index.index = len(l.items)
l.items = append(l.items, item)
return index
}