修复负载均衡算法中可能因参数缺失而崩溃的Bug

This commit is contained in:
刘祥超
2021-09-23 11:43:21 +08:00
parent 96ddf1ded8
commit 6b33fb52e0
3 changed files with 20 additions and 9 deletions

View File

@@ -194,7 +194,7 @@ func (this *ReverseProxyConfig) NextOrigin(call *shared.RequestCall) *OriginConf
}
// 空域名
if len(call.Domain) == 0 {
if call == nil || len(call.Domain) == 0 {
group, ok := this.schedulingGroupMap[""]
if ok {
return group.NextOrigin(call)

View File

@@ -3,6 +3,7 @@ package schedulingconfigs
import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/rands"
"hash/crc32"
)
@@ -10,12 +11,12 @@ import (
type HashScheduling struct {
Scheduling
count uint32
count int
}
// Start 启动
func (this *HashScheduling) Start() {
this.count = uint32(len(this.Candidates))
this.count = len(this.Candidates)
}
// Next 获取下一个候选对象
@@ -24,6 +25,10 @@ func (this *HashScheduling) Next(call *shared.RequestCall) CandidateInterface {
return nil
}
if call == nil || call.Options == nil {
return this.Candidates[rands.Int(0, this.count-1)]
}
key := call.Options.GetString("key")
if call.Formatter != nil {
@@ -31,7 +36,7 @@ func (this *HashScheduling) Next(call *shared.RequestCall) CandidateInterface {
}
sum := crc32.ChecksumIEEE([]byte(key))
return this.Candidates[sum%this.count]
return this.Candidates[sum%uint32(this.count)]
}
// Summary 获取简要信息

View File

@@ -3,6 +3,7 @@ package schedulingconfigs
import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/rands"
"math/rand"
"net/http"
"time"
@@ -12,7 +13,7 @@ import (
type StickyScheduling struct {
Scheduling
count uint32
count int
mapping map[string]CandidateInterface // code => candidate
}
@@ -25,7 +26,7 @@ func (this *StickyScheduling) Start() {
}
}
this.count = uint32(len(this.Candidates))
this.count = len(this.Candidates)
rand.Seed(time.Now().UnixNano())
}
@@ -34,11 +35,16 @@ func (this *StickyScheduling) Next(call *shared.RequestCall) CandidateInterface
if this.count == 0 {
return nil
}
if call == nil || call.Options == nil {
return this.Candidates[rands.Int(0, this.count-1)]
}
typeCode := call.Options.GetString("type")
param := call.Options.GetString("param")
if call.Request == nil {
return this.Candidates[uint32(rand.Int())%this.count]
return this.Candidates[rand.Int()%this.count]
}
code := ""
@@ -80,14 +86,14 @@ func (this *StickyScheduling) Next(call *shared.RequestCall) CandidateInterface
}()
if len(code) == 0 {
c = this.Candidates[uint32(rand.Int())%this.count]
c = this.Candidates[rand.Int()%this.count]
return c
}
found := false
c, found = this.mapping[code]
if !found {
c = this.Candidates[uint32(rand.Int())%this.count]
c = this.Candidates[rand.Int()%this.count]
return c
}