Archived
反向代理源站实现使用域名分组
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
package schedulingconfigs
|
||||
|
||||
// 候选对象接口
|
||||
// CandidateInterface 候选对象接口
|
||||
type CandidateInterface interface {
|
||||
// 权重
|
||||
// CandidateWeight 权重
|
||||
CandidateWeight() uint
|
||||
|
||||
// 代号
|
||||
// CandidateCodes 代号
|
||||
CandidateCodes() []string
|
||||
}
|
||||
|
||||
@@ -5,35 +5,35 @@ import (
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
// 调度算法接口
|
||||
// SchedulingInterface 调度算法接口
|
||||
type SchedulingInterface interface {
|
||||
// 是否有候选对象
|
||||
// HasCandidates 是否有候选对象
|
||||
HasCandidates() bool
|
||||
|
||||
// 添加候选对象
|
||||
// Add 添加候选对象
|
||||
Add(candidate ...CandidateInterface)
|
||||
|
||||
// 启动
|
||||
// Start 启动
|
||||
Start()
|
||||
|
||||
// 查找下一个候选对象
|
||||
// Next 查找下一个候选对象
|
||||
Next(call *shared.RequestCall) CandidateInterface
|
||||
|
||||
// 获取简要信息
|
||||
// Summary 获取简要信息
|
||||
Summary() maps.Map
|
||||
}
|
||||
|
||||
// 调度算法基础类
|
||||
// Scheduling 调度算法基础类
|
||||
type Scheduling struct {
|
||||
Candidates []CandidateInterface
|
||||
}
|
||||
|
||||
// 判断是否有候选对象
|
||||
// HasCandidates 判断是否有候选对象
|
||||
func (this *Scheduling) HasCandidates() bool {
|
||||
return len(this.Candidates) > 0
|
||||
}
|
||||
|
||||
// 添加候选对象
|
||||
// Add 添加候选对象
|
||||
func (this *Scheduling) Add(candidate ...CandidateInterface) {
|
||||
this.Candidates = append(this.Candidates, candidate...)
|
||||
}
|
||||
|
||||
@@ -6,19 +6,19 @@ import (
|
||||
"hash/crc32"
|
||||
)
|
||||
|
||||
// Hash调度算法
|
||||
// HashScheduling Hash调度算法
|
||||
type HashScheduling struct {
|
||||
Scheduling
|
||||
|
||||
count uint32
|
||||
}
|
||||
|
||||
// 启动
|
||||
// Start 启动
|
||||
func (this *HashScheduling) Start() {
|
||||
this.count = uint32(len(this.Candidates))
|
||||
}
|
||||
|
||||
// 获取下一个候选对象
|
||||
// Next 获取下一个候选对象
|
||||
func (this *HashScheduling) Next(call *shared.RequestCall) CandidateInterface {
|
||||
if this.count == 0 {
|
||||
return nil
|
||||
@@ -34,7 +34,7 @@ func (this *HashScheduling) Next(call *shared.RequestCall) CandidateInterface {
|
||||
return this.Candidates[sum%this.count]
|
||||
}
|
||||
|
||||
// 获取简要信息
|
||||
// Summary 获取简要信息
|
||||
func (this *HashScheduling) Summary() maps.Map {
|
||||
return maps.Map{
|
||||
"code": "hash",
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// 随机调度算法
|
||||
// RandomScheduling 随机调度算法
|
||||
type RandomScheduling struct {
|
||||
Scheduling
|
||||
|
||||
@@ -16,7 +16,7 @@ type RandomScheduling struct {
|
||||
count uint // 实际总的服务器数
|
||||
}
|
||||
|
||||
// 启动
|
||||
// Start 启动
|
||||
func (this *RandomScheduling) Start() {
|
||||
sumWeight := uint(0)
|
||||
for _, c := range this.Candidates {
|
||||
@@ -55,7 +55,7 @@ func (this *RandomScheduling) Start() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
// 获取下一个候选对象
|
||||
// Next 获取下一个候选对象
|
||||
func (this *RandomScheduling) Next(call *shared.RequestCall) CandidateInterface {
|
||||
if this.count == 0 {
|
||||
return nil
|
||||
@@ -67,7 +67,7 @@ func (this *RandomScheduling) Next(call *shared.RequestCall) CandidateInterface
|
||||
return this.array[index]
|
||||
}
|
||||
|
||||
// 获取简要信息
|
||||
// Summary 获取简要信息
|
||||
func (this *RandomScheduling) Summary() maps.Map {
|
||||
return maps.Map{
|
||||
"code": "random",
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// 轮询调度算法
|
||||
// RoundRobinScheduling 轮询调度算法
|
||||
type RoundRobinScheduling struct {
|
||||
Scheduling
|
||||
|
||||
@@ -19,7 +19,7 @@ type RoundRobinScheduling struct {
|
||||
locker sync.Mutex
|
||||
}
|
||||
|
||||
// 启动
|
||||
// Start 启动
|
||||
func (this *RoundRobinScheduling) Start() {
|
||||
lists.Sort(this.Candidates, func(i int, j int) bool {
|
||||
c1 := this.Candidates[i]
|
||||
@@ -41,7 +41,7 @@ func (this *RoundRobinScheduling) Start() {
|
||||
this.count = uint(len(this.Candidates))
|
||||
}
|
||||
|
||||
// 获取下一个候选对象
|
||||
// Next 获取下一个候选对象
|
||||
func (this *RoundRobinScheduling) Next(call *shared.RequestCall) CandidateInterface {
|
||||
if this.count == 0 {
|
||||
return nil
|
||||
@@ -69,7 +69,7 @@ func (this *RoundRobinScheduling) Next(call *shared.RequestCall) CandidateInterf
|
||||
return c
|
||||
}
|
||||
|
||||
// 获取简要信息
|
||||
// Summary 获取简要信息
|
||||
func (this *RoundRobinScheduling) Summary() maps.Map {
|
||||
return maps.Map{
|
||||
"code": "roundRobin",
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Sticky调度算法
|
||||
// StickyScheduling Sticky调度算法
|
||||
type StickyScheduling struct {
|
||||
Scheduling
|
||||
|
||||
@@ -16,7 +16,7 @@ type StickyScheduling struct {
|
||||
mapping map[string]CandidateInterface // code => candidate
|
||||
}
|
||||
|
||||
// 启动
|
||||
// Start 启动
|
||||
func (this *StickyScheduling) Start() {
|
||||
this.mapping = map[string]CandidateInterface{}
|
||||
for _, c := range this.Candidates {
|
||||
@@ -29,7 +29,7 @@ func (this *StickyScheduling) Start() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
// 获取下一个候选对象
|
||||
// Next 获取下一个候选对象
|
||||
func (this *StickyScheduling) Next(call *shared.RequestCall) CandidateInterface {
|
||||
if this.count == 0 {
|
||||
return nil
|
||||
@@ -95,7 +95,7 @@ func (this *StickyScheduling) Next(call *shared.RequestCall) CandidateInterface
|
||||
return c
|
||||
}
|
||||
|
||||
// 获取简要信息
|
||||
// Summary 获取简要信息
|
||||
func (this *StickyScheduling) Summary() maps.Map {
|
||||
return maps.Map{
|
||||
"code": "sticky",
|
||||
|
||||
Reference in New Issue
Block a user