-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
143 lines (122 loc) · 3.54 KB
/
api.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
// Copyright © 2021 Hedzr Yeh.
package lbapi
import "reflect"
// Peer is a backend object, such as a host+port, a
// http/https url, or a constraint expression, and so on.
type Peer interface {
// String will return the main identity string of a peer.
//
// You would have to implement only this one func to plug
// your object into the architecture created by
// lbapi.Balancer
String() string
}
// Weighted object
type Weighted interface {
Weight() int
}
// WeightedPeer is a Peer with Weight property.
type WeightedPeer interface {
Peer
Weighted
}
// Factor is a factor parameter for BalancerLite.Next.
//
// If you won't known what should be passed into
// BalancerLite.Next, send DummyFactor as it.
//
// But when you are constructing a complex lb system
// or a multiple-level one such as a weighted
// versioning load balancer. Take a look at our
// version.New and version.VersioningBackendFactor.
type Factor interface {
Factor() string
}
// BalancerLite represents a generic load balancer.
type BalancerLite interface {
// Next will return the next backend.
//
// For some ones like consistent-hashed balancer, Next needs
// a factor as its param. It might be the requesting URL
// typically.
// For else, you can pass a lbapi.DummyFactor or just an
// empty string as factor.
Next(factor Factor) (next Peer, c Constrainable)
}
// Balancer represents a generic load balancer.
// For the real world, Balancer is a useful interface rather
// than BalancerLite.
type Balancer interface {
BalancerLite
Count() int
Add(peers ...Peer)
Remove(peer Peer)
Clear()
}
// Opt is a type prototype for New Balancer
type Opt func(balancer Balancer)
// FactorComparable is a composite interface which assembly Factor and constraint comparing.
type FactorComparable interface {
Factor
ConstrainedBy(constraints interface{}) (peer Peer, c Constrainable, satisfied bool)
}
// FactorHashable is a composite interface which assembly Factor and hashing computer.
type FactorHashable interface {
Factor
HashCode() uint32
}
// FactorString is a string type, it implements Factor interface.
type FactorString string
// Factor function impl Factor interface
func (s FactorString) Factor() string { return string(s) }
// DummyFactor will be used in someone like you does not known
// what on earth should be passed into BalancerLite.Next(factor).
const DummyFactor FactorString = ""
// Constrainable is an object who can be applied onto a factor ( BalancerLite.Next(factor) )
type Constrainable interface {
CanConstrain(o interface{}) (yes bool)
Check(o interface{}) (satisfied bool)
Peer
}
// WeightedConstrainable is an object who can be applied onto a factor ( BalancerLite.Next(factor) )
type WeightedConstrainable interface {
Constrainable
Weighted
}
// DeepEqualAware could be concreted by a Peer so you could
// customize how to compare two peers, avoid reflect.DeepEqual
// bypass.
type DeepEqualAware interface {
DeepEqual(b Peer) bool
}
// DeepEqual will be used in Balancer, and a Peer can bypass
// reflect.DeepEqual by implementing DeepEqualAware interface.
func DeepEqual(a, b Peer) (yes bool) {
if a == b {
return true
}
if e, ok := a.(DeepEqualAware); ok {
return e.DeepEqual(b)
}
return reflect.DeepEqual(a, b)
}
//// Sum sum
//func Sum(a []int, fn func(it int)) {
// for _, it := range a {
// fn(it)
// }
//}
//
//// SumMapKeys sum
//func SumMapKeys(a map[int]string, fn func(it int)) {
// for it := range a {
// fn(it)
// }
//}
//
//// SumMapValues sum
//func SumMapValues(a map[string]int, fn func(it int)) {
// for _, it := range a {
// fn(it)
// }
//}