-
Notifications
You must be signed in to change notification settings - Fork 34
/
backend_list.go
204 lines (169 loc) · 4.49 KB
/
backend_list.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
package teaconfigs
import (
"github.com/TeaWeb/code/teaconfigs/scheduling"
"github.com/TeaWeb/code/teaconfigs/shared"
"github.com/iwind/TeaGo/lists"
"sync"
)
// BackendList接口定义
type BackendListInterface interface {
// 校验
ValidateBackends() error
// 添加Backend
AddBackend(backend *BackendConfig)
// 所有的Backends
AllBackends() []*BackendConfig
// 删除后端服务器
DeleteBackend(backendId string)
// 查找后端服务器
FindBackend(backendId string) *BackendConfig
// 调度算法
SchedulingConfig() *SchedulingConfig
// 设置调度算法
SetSchedulingConfig(scheduling *SchedulingConfig)
// 重新建立调度
SetupScheduling(isBackup bool)
}
// BackendList定义
type BackendList struct {
Backends []*BackendConfig `yaml:"backends" json:"backends"`
Scheduling *SchedulingConfig `yaml:"scheduling" json:"scheduling"` // 调度算法选项
hasBackends bool
schedulingIsBackup bool
schedulingObject scheduling.SchedulingInterface
schedulingLocker sync.Mutex
}
// 校验
func (this *BackendList) ValidateBackends() error {
this.hasBackends = len(this.Backends) > 0
for _, backend := range this.Backends {
err := backend.Validate()
if err != nil {
return err
}
}
// scheduling
this.SetupScheduling(false)
return nil
}
// 添加Backend
func (this *BackendList) AddBackend(backend *BackendConfig) {
this.Backends = append(this.Backends, backend)
}
// 所有的Backends
func (this *BackendList) AllBackends() []*BackendConfig {
return this.Backends
}
// 删除后端服务器
func (this *BackendList) DeleteBackend(backendId string) {
result := []*BackendConfig{}
for _, backend := range this.Backends {
if backend.Id == backendId {
continue
}
result = append(result, backend)
}
this.Backends = result
}
// 删除一组后端服务器
func (this *BackendList) DeleteBackends(backendIds []string) {
if len(backendIds) == 0 {
return
}
result := []*BackendConfig{}
for _, backend := range this.Backends {
if lists.ContainsString(backendIds, backend.Id) {
continue
}
result = append(result, backend)
}
this.Backends = result
}
// 根据ID查找后端服务器
func (this *BackendList) FindBackend(backendId string) *BackendConfig {
for _, backend := range this.Backends {
if backend.Id == backendId {
return backend
}
}
return nil
}
// 取得下一个可用的后端服务
func (this *BackendList) NextBackend(call *shared.RequestCall) *BackendConfig {
this.schedulingLocker.Lock()
defer this.schedulingLocker.Unlock()
if this.schedulingObject == nil {
return nil
}
if this.Scheduling != nil && call != nil && call.Options != nil {
for k, v := range this.Scheduling.Options {
call.Options[k] = v
}
}
candidate := this.schedulingObject.Next(call)
if candidate == nil {
// 启用备用服务器
if !this.schedulingIsBackup {
this.SetupScheduling(true)
candidate = this.schedulingObject.Next(call)
if candidate == nil {
return nil
}
}
if candidate == nil {
return nil
}
}
return candidate.(*BackendConfig)
}
// 设置调度算法
func (this *BackendList) SetupScheduling(isBackup bool) {
if !isBackup {
this.schedulingLocker.Lock()
defer this.schedulingLocker.Unlock()
}
this.schedulingIsBackup = isBackup
if this.Scheduling == nil {
this.schedulingObject = &scheduling.RandomScheduling{}
} else {
typeCode := this.Scheduling.Code
s := scheduling.FindSchedulingType(typeCode)
if s == nil {
this.Scheduling = nil
this.schedulingObject = &scheduling.RandomScheduling{}
} else {
this.schedulingObject = s["instance"].(scheduling.SchedulingInterface)
}
}
for _, backend := range this.Backends {
if backend.On && !backend.IsDown {
if isBackup && backend.IsBackup {
this.schedulingObject.Add(backend)
} else if !isBackup && !backend.IsBackup {
this.schedulingObject.Add(backend)
}
}
}
this.schedulingObject.Start()
}
// 调度算法
func (this *BackendList) SchedulingConfig() *SchedulingConfig {
return this.Scheduling
}
// 设置调度算法
func (this *BackendList) SetSchedulingConfig(scheduling *SchedulingConfig) {
this.Scheduling = scheduling
}
// 判断是否有后端服务器
func (this *BackendList) HasBackends() bool {
return this.hasBackends
}
// 克隆
func (this *BackendList) CloneBackendList() *BackendList {
newBackendList := new(BackendList)
newBackendList.Backends = this.Backends
newBackendList.Scheduling = this.Scheduling
newBackendList.hasBackends = this.hasBackends
newBackendList.SetupScheduling(false)
return newBackendList
}