-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.go
131 lines (115 loc) · 3.36 KB
/
factory.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
package goloadbalancer
import "fmt"
type lcEndpointImpl struct {
Endpoint
}
func (l *lcEndpointImpl) ActivateConnections() int {
return l.Stats().GetActivateConns()
}
func (l *lcEndpointImpl) InactiveConnections() int {
return l.Stats().GetIdleConns()
}
func NewLCEndpointImpl(endpoint Endpoint) LeastConnectionsEndpoint {
return &lcEndpointImpl{Endpoint: endpoint}
}
type WLCEndpointImpl struct {
*lcEndpointImpl
weight int
}
func (w *WLCEndpointImpl) Weight() int {
return w.weight
}
func NewWLCEndpointImpl(endpoint Endpoint, weight int) WeightLeastConnectionsEndpoint {
return &WLCEndpointImpl{
lcEndpointImpl: &lcEndpointImpl{Endpoint: endpoint},
weight: weight,
}
}
type wrrEndpointImpl struct {
Endpoint
weight int
currentWeight int
}
func (w *wrrEndpointImpl) Weight() int {
return w.weight
}
func (w *wrrEndpointImpl) CurrentWeight() int {
return w.currentWeight
}
func (w *wrrEndpointImpl) SetWeight(weight int) {
w.currentWeight = weight
}
func NewWRREndpointImpl(endpoint Endpoint, weight int) WeightEndpoint {
return &wrrEndpointImpl{
Endpoint: endpoint,
weight: weight,
currentWeight: weight,
}
}
type sedEndpointImpl struct {
Endpoint
weight int
}
func (s *sedEndpointImpl) ActivateConnections() int {
return s.Stats().GetActivateConns()
}
func (s *sedEndpointImpl) Weight() int {
return s.weight
}
func NewSedEndpointImpl(endpoint Endpoint, weight int) ShortestExpectedDelayEndpoint {
return &sedEndpointImpl{
Endpoint: endpoint,
weight: weight,
}
}
// const (
//
// RRBalanceType BalanceType = "RoundRobin"
// WRRBalanceType BalanceType = "WeightedRoundRobin"
// ConsistentHashBalanceType BalanceType = "ConsistentHash"
// LCBalanceType BalanceType = "LeastConnection"
// SEDBalanceType BalanceType = "ShortestExpectedDelay"
// WLCBalanceType BalanceType = "WeightedLeastConnection"
// NQBalanceType BalanceType = "NeverQueue"
//
// )
func New(lbType BalanceType, endpoints []Endpoint) (LoadBalance, error) {
switch lbType {
case RRBalanceType:
return NewRoundRobinBalance(endpoints), nil
case WRRBalanceType:
return NewWeightedRoundRobinBalance(endpoints), nil
case ConsistentHashBalanceType:
return NewConsistentHashBalancer(endpoints, 10), nil
case LCBalanceType:
return NewLeastConnectionsBalance(endpoints), nil
case SEDBalanceType:
return NewShortestExpectedDelayBalance(endpoints), nil
case WLCBalanceType:
return NewWeightLeastConnectionsBalance(endpoints), nil
case NQBalanceType:
return NewNeverQueueBalance(endpoints), nil
default:
return nil, fmt.Errorf("Unknown load balance type")
}
}
// func NewEndpoints(lbType BalanceType,endpoints []EndpointMeta)([]Endpoint,error){
// switch lbType{
// case RRBalanceType:
// return NewRoundRobinBalance(endpoints),nil
// case WRRBalanceType:
// return NewWeightedRoundRobinBalance(endpoints),nil
// case ConsistentHashBalanceType:
// return NewConsistentHashBalancer(endpoints,10),nil
// case LCBalanceType:
// return NewLeastConnectionsBalance(endpoints),nil
// case SEDBalanceType:
// return NewShortestExpectedDelayBalance(endpoints),nil
// case WLCBalanceType:
// return NewWeightLeastConnectionsBalance(endpoints),nil
// case NQBalanceType:
// return NewNeverQueueBalance(endpoints),nil
// default:
// return nil,fmt.Errorf("Unknown load balance type")
// }
// }