-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwr.go
147 lines (123 loc) · 2.09 KB
/
wr.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
package balancer
import (
"sort"
"sync"
"github.com/fufuok/balancer/utils"
)
// WeightedRand
type wr struct {
items []*wrItem
weights []int
count int
max uint32
all map[string]int
sync.RWMutex
}
type wrItem struct {
item string
weight int
}
func NewWeightedRand(items ...map[string]int) (lb *wr) {
if len(items) > 0 && len(items[0]) > 0 {
lb = &wr{}
lb.Update(items[0])
return
}
return &wr{
all: make(map[string]int),
}
}
func (b *wr) Add(item string, weight ...int) {
w := 1
if len(weight) > 0 {
w = weight[0]
}
all := b.All().(map[string]int)
all[item] = w
b.Update(all)
}
func (b *wr) All() interface{} {
all := make(map[string]int)
b.RLock()
for k, v := range b.all {
all[k] = v
}
b.RUnlock()
return all
}
func (b *wr) Name() string {
return "WeightedRand"
}
func (b *wr) Select(_ ...string) (item string) {
b.RLock()
switch b.count {
case 0:
item = ""
case 1:
item = b.items[0].item
default:
r := utils.FastRandn(b.max) + 1
i := utils.SearchInts(b.weights, int(r))
item = b.items[i].item
}
b.RUnlock()
return
}
func (b *wr) Remove(item string, _ ...bool) (ok bool) {
b.RLock()
_, ok = b.all[item]
b.RUnlock()
if ok {
all := b.All().(map[string]int)
delete(all, item)
b.Update(all)
}
return
}
func (b *wr) RemoveAll() {
b.Lock()
b.items = b.items[:0]
b.weights = b.weights[:0]
b.count = 0
b.max = 0
b.all = make(map[string]int)
b.Unlock()
}
func (b *wr) Reset() {}
func (b *wr) Update(items interface{}) bool {
v, ok := items.(map[string]int)
if !ok {
return false
}
var (
count int
data []*wrItem
)
for item, weight := range v {
// Discard items with a weight less than 1
if weight > 0 {
data = append(data, &wrItem{
item: item,
weight: weight,
})
count++
}
}
sort.Slice(data, func(i, j int) bool {
return data[i].weight < data[j].weight
})
max := 0
weights := make([]int, count)
for i := range data {
max += data[i].weight
weights[i] = max
}
b.Lock()
b.items = data
b.weights = weights
b.count = count
b.max = uint32(max)
b.all = v
b.Unlock()
return true
}