-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtagged.go
85 lines (72 loc) · 1.58 KB
/
tagged.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
package rpcng
import (
"math/rand"
"sync/atomic"
"github.com/iqoption/rpcng/consistenthash"
"github.com/iqoption/rpcng/registry"
)
// Tagged entry
type tagged struct {
tag string
index int64
services registry.Services
clients []*Client
hash *consistenthash.Map
idc map[string]*Client
}
func newTagged(tag string, services registry.Services, clients []*Client, chash bool) *tagged {
t := &tagged{
tag: tag,
services: services,
clients: clients,
}
if chash {
t.idc = make(map[string]*Client)
s := make([]string, len(services))
for i := range services {
s[i] = services[i].Id
t.idc[s[i]] = clients[i]
}
t.hash = consistenthash.New(200, s...)
}
return t
}
func (t *tagged) Tag() string {
return t.tag
}
func (t *tagged) Random() (*Client, error) {
n := len(t.clients)
switch {
case n <= 0:
return nil, ErrNotFounded
case n == 1:
return t.client(0)
default:
return t.client(int64(rand.Intn(n)))
}
}
func (t *tagged) Hashed(key int) (*Client, error) {
if t.hash == nil || t.hash.IsEmpty() {
return nil, ErrNotFounded
}
return t.idc[t.hash.GetUint64(uint64(key))], nil
}
func (t *tagged) RoundRobin() (*Client, error) {
var index = atomic.AddInt64(&t.index, 1)
return t.client(index)
}
func (t *tagged) client(i int64) (*Client, error) {
if len(t.clients) == 0 {
return nil, ErrNotFounded
}
return t.clients[int(i)%len(t.clients)], nil
}
func (t *tagged) All() []*Client {
return t.clients
}
func (t *tagged) HasCHash() bool {
return t.hash != nil
}
func (t *tagged) Services() registry.Services {
return t.services
}