diff --git a/pkg/serverconfigs/reverse_proxy_config.go b/pkg/serverconfigs/reverse_proxy_config.go index ae47443..0da3387 100644 --- a/pkg/serverconfigs/reverse_proxy_config.go +++ b/pkg/serverconfigs/reverse_proxy_config.go @@ -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) diff --git a/pkg/serverconfigs/schedulingconfigs/scheduling_hash.go b/pkg/serverconfigs/schedulingconfigs/scheduling_hash.go index c357836..ec628b0 100644 --- a/pkg/serverconfigs/schedulingconfigs/scheduling_hash.go +++ b/pkg/serverconfigs/schedulingconfigs/scheduling_hash.go @@ -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 获取简要信息 diff --git a/pkg/serverconfigs/schedulingconfigs/scheduling_sticky.go b/pkg/serverconfigs/schedulingconfigs/scheduling_sticky.go index 8469612..9516dde 100644 --- a/pkg/serverconfigs/schedulingconfigs/scheduling_sticky.go +++ b/pkg/serverconfigs/schedulingconfigs/scheduling_sticky.go @@ -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 }