This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
roundrobin.go
153 lines (121 loc) · 2.64 KB
/
roundrobin.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
/*
Copyright © 2017 Henry Huang <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package node
import (
"fmt"
"sort"
"strings"
"sync"
"sync/atomic"
"github.com/iTrellis/common/formats"
)
type roundrobin struct {
Name string
nodes map[string]*Node
indexes formats.Strings
count int64
robinIndex int64
sync.RWMutex
}
// NewRoundRobin get roundrobin node manager
func NewRoundRobin(name string) (Manager, error) {
if name = strings.TrimSpace(name); name == "" {
return nil, fmt.Errorf("name should not be nil")
}
return &roundrobin{Name: name}, nil
}
func (p *roundrobin) IsEmpty() bool {
return atomic.LoadInt64(&p.count) == 0
}
func (p *roundrobin) Add(node *Node) {
if node == nil {
return
}
if node.Weight != 0 {
node.Weight = 0
}
p.Lock()
defer p.Unlock()
p.add(node)
}
func (p *roundrobin) add(pNode *Node) {
if p.nodes == nil {
p.nodes = make(map[string]*Node)
}
_, ok := p.nodes[pNode.ID]
if !ok {
p.count++
p.indexes = append(p.indexes, pNode.ID)
sort.Sort(p.indexes)
}
p.nodes[pNode.ID] = pNode
}
func (p *roundrobin) NodeFor(...string) (*Node, bool) {
if p.IsEmpty() {
return nil, false
}
p.RLock()
defer p.RUnlock()
if p.robinIndex >= p.count {
p.robinIndex = 0
}
node := p.nodes[p.indexes[int(p.robinIndex%p.count)]]
p.robinIndex++
return node, true
}
func (p *roundrobin) Remove() {
p.Lock()
defer p.Unlock()
p.remove()
}
func (p *roundrobin) remove() {
p.nodes = nil
p.indexes = nil
p.count = 0
p.robinIndex = 0
}
func (p *roundrobin) RemoveByID(id string) {
p.Lock()
defer p.Unlock()
p.removeByID(id)
}
func (p *roundrobin) removeByID(id string) {
if p.IsEmpty() {
p.remove()
return
}
_, ok := p.nodes[id]
if !ok {
return
}
delete(p.nodes, id)
p.removeKey(id)
p.count--
sort.Sort(p.indexes)
}
func (p *roundrobin) removeKey(key string) {
for i, v := range p.indexes {
if v == key {
p.indexes = append(p.indexes[:i], p.indexes[i+1:]...)
break
}
}
}
func (p *roundrobin) PrintNodes() {
p.RLock()
defer p.RUnlock()
for i, v := range p.nodes {
fmt.Println("nodes:", i, *v)
}
}