-
Notifications
You must be signed in to change notification settings - Fork 1
/
backend.go
244 lines (207 loc) · 4.59 KB
/
backend.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
package balancer
import (
"sync"
"sync/atomic"
"time"
"github.com/bytom/vapor/common"
)
// Backends backend node list
type Backends struct {
sync.RWMutex
nodes []*Backend
nodesMap map[string]*Backend
}
// NewBackends create a list of backend nodes
func NewBackends() *Backends {
return &Backends{
nodes: make([]*Backend, 0),
nodesMap: make(map[string]*Backend),
}
}
// Add add backend node
func (b *Backends) Add(newnode *Backend) bool {
b.Lock()
defer b.Unlock()
return b.add(newnode)
}
// Delete delete backend node
func (b *Backends) Delete(newnode *Backend) bool {
b.Lock()
defer b.Unlock()
return b.delete(newnode)
}
// Get get the backend node by index and name
func (b *Backends) Get(key interface{}) (*Backend, bool) {
b.RLock()
defer b.RUnlock()
return b.get(key)
}
// Len get the length of the backend node
func (b *Backends) Len() int {
b.RLock()
defer b.RUnlock()
return len(b.nodes)
}
// Range traverse back-end nodes
func (b *Backends) Range(f func(index int, backend *Backend) bool) {
b.RLock()
defer b.RUnlock()
for i, node := range b.nodes {
if !f(i, node) {
break
}
}
}
func (b *Backends) add(newnode *Backend) bool {
if newnode == nil {
return false
}
if _, ok := b.nodesMap[newnode.URL]; ok {
return false
}
b.nodesMap[newnode.URL] = newnode
b.nodes = append(b.nodes, newnode)
return true
}
func (b *Backends) delete(newnode *Backend) bool {
if newnode == nil {
return false
}
if _, ok := b.nodesMap[newnode.URL]; !ok {
return false
}
delete(b.nodesMap, newnode.URL)
for i, node := range b.nodes {
if node.URL == newnode.URL {
b.nodes = append(b.nodes[:i], b.nodes[i+1:]...)
return true
}
}
return false
}
func (b *Backends) get(key interface{}) (*Backend, bool) {
switch k := key.(type) {
case int:
if k >= 0 && k < len(b.nodes) {
return b.nodes[k], true
}
case string:
if node, ok := b.nodesMap[k]; ok {
return node, true
}
}
return nil, false
}
// Backend node specific information
type Backend struct {
URL string
State *State
Statistic *Statistic
Cache *common.Cache
}
// NewBackend creates a Backend.
func NewBackend(url string, cacheSize int) *Backend {
return &Backend{
URL: url,
State: &State{
alive: true,
},
Statistic: &Statistic{},
Cache: common.NewCache(cacheSize),
}
}
// State describes the status information of the node
type State struct {
alive bool
aliveMux sync.RWMutex
failures []*FailureError
failuresMux sync.RWMutex
}
// SetAlive set whether the node is available
func (s *State) SetAlive(alive bool) {
s.aliveMux.Lock()
defer s.aliveMux.Unlock()
s.alive = alive
}
// Alive if the backend is still alive, Alive returns true
func (s *State) Alive() bool {
s.aliveMux.RLock()
defer s.aliveMux.RUnlock()
return s.alive
}
// AddFail add failed error
func (s *State) AddFail(err error) {
s.failuresMux.Lock()
defer s.failuresMux.Unlock()
n := len(s.failures)
if n == 100 {
s.failures = s.failures[1:]
}
s.failures = append(s.failures, NewFailureError(err))
}
// CleanFail clear outdated errors
func (s *State) CleanFail(interval int64) {
s.failuresMux.Lock()
defer s.failuresMux.Unlock()
now := time.Now().Unix()
for _, v := range s.failures {
if now-v.timestamp >= interval {
s.failures = s.failures[1:]
} else {
break
}
}
}
// LenFail return number of failures
func (s *State) LenFail() int {
s.failuresMux.RLock()
defer s.failuresMux.RUnlock()
return len(s.failures)
}
// HealthCheck determine whether the node is available
func (s *State) HealthCheck(interval int64, max int) bool {
s.CleanFail(interval)
length := s.LenFail()
if length >= max {
s.SetAlive(false)
return false
}
return true
}
// FailureError describe the failed error
type FailureError struct {
timestamp int64
err error
}
// NewFailureError creates a fail error.
func NewFailureError(err error) *FailureError {
return &FailureError{
timestamp: time.Now().Unix(),
err: err,
}
}
// Error return error message
func (f *FailureError) Error() string {
return f.err.Error()
}
// Statistic describe statistics
type Statistic struct {
success uint64
failure uint64
}
// Success return number of success
func (s *Statistic) Success() uint64 {
return atomic.LoadUint64(&s.success)
}
// IncSuccess auto-increment success times
func (s *Statistic) IncSuccess() uint64 {
return atomic.AddUint64(&s.success, 1)
}
// Failure return number of failures
func (s *Statistic) Failure() uint64 {
return atomic.LoadUint64(&s.failure)
}
// IncFailure auto-increment failure times
func (s *Statistic) IncFailure() uint64 {
return atomic.AddUint64(&s.failure, 1)
}