Archived
优化源站调度
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// 源站服务配置
|
||||
// OriginConfig 源站服务配置
|
||||
type OriginConfig struct {
|
||||
Id int64 `yaml:"id" json:"id"` // ID
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
|
||||
@@ -48,6 +48,8 @@ type OriginConfig struct {
|
||||
FTPServerRef *FTPServerRef `yaml:"ftpServerRef" json:"ftpServerRef"` // TODO
|
||||
FTPServer *FTPServerConfig `yaml:"ftpServer" json:"ftpServer"` // TODO
|
||||
|
||||
IsOk bool `yaml:"isOk" json:"isOk"` // 是否可以正常访问
|
||||
|
||||
connTimeoutDuration time.Duration
|
||||
readTimeoutDuration time.Duration
|
||||
idleTimeoutDuration time.Duration
|
||||
@@ -65,8 +67,10 @@ type OriginConfig struct {
|
||||
requestURIHasVariables bool
|
||||
}
|
||||
|
||||
// 校验
|
||||
// Init 校验
|
||||
func (this *OriginConfig) Init() error {
|
||||
this.IsOk = true
|
||||
|
||||
// URL
|
||||
this.requestHostHasVariables = configutils.HasVariables(this.RequestHost)
|
||||
this.requestURIHasVariables = configutils.HasVariables(this.RequestURI)
|
||||
@@ -157,7 +161,7 @@ func (this *OriginConfig) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 候选对象代号
|
||||
// CandidateCodes 候选对象代号
|
||||
func (this *OriginConfig) CandidateCodes() []string {
|
||||
codes := []string{strconv.FormatInt(this.Id, 10)}
|
||||
if len(this.Code) > 0 {
|
||||
@@ -166,37 +170,37 @@ func (this *OriginConfig) CandidateCodes() []string {
|
||||
return codes
|
||||
}
|
||||
|
||||
// 候选对象权重
|
||||
// CandidateWeight 候选对象权重
|
||||
func (this *OriginConfig) CandidateWeight() uint {
|
||||
return this.Weight
|
||||
}
|
||||
|
||||
// 连接超时时间
|
||||
// ConnTimeoutDuration 连接超时时间
|
||||
func (this *OriginConfig) ConnTimeoutDuration() time.Duration {
|
||||
return this.connTimeoutDuration
|
||||
}
|
||||
|
||||
// 读取超时时间
|
||||
// ReadTimeoutDuration 读取超时时间
|
||||
func (this *OriginConfig) ReadTimeoutDuration() time.Duration {
|
||||
return this.readTimeoutDuration
|
||||
}
|
||||
|
||||
// 休眠超时时间
|
||||
// IdleTimeoutDuration 休眠超时时间
|
||||
func (this *OriginConfig) IdleTimeoutDuration() time.Duration {
|
||||
return this.idleTimeoutDuration
|
||||
}
|
||||
|
||||
// 判断RequestHost是否有变量
|
||||
// RequestHostHasVariables 判断RequestHost是否有变量
|
||||
func (this *OriginConfig) RequestHostHasVariables() bool {
|
||||
return this.requestHostHasVariables
|
||||
}
|
||||
|
||||
// 判断RequestURI是否有变量
|
||||
// RequestURIHasVariables 判断RequestURI是否有变量
|
||||
func (this *OriginConfig) RequestURIHasVariables() bool {
|
||||
return this.requestURIHasVariables
|
||||
}
|
||||
|
||||
// 唯一Key
|
||||
// UniqueKey 唯一Key
|
||||
func (this *OriginConfig) UniqueKey() string {
|
||||
return this.uniqueKey
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ const (
|
||||
RequestHostTypeCustomized RequestHostType = 2
|
||||
)
|
||||
|
||||
// 反向代理设置
|
||||
// ReverseProxyConfig 反向代理设置
|
||||
type ReverseProxyConfig struct {
|
||||
Id int64 `yaml:"id" json:"id"` // ID
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
|
||||
@@ -59,7 +59,7 @@ type ReverseProxyConfig struct {
|
||||
addXForwardedProtoHeader bool
|
||||
}
|
||||
|
||||
// 初始化
|
||||
// Init 初始化
|
||||
func (this *ReverseProxyConfig) Init() error {
|
||||
this.requestHostHasVariables = configutils.HasVariables(this.RequestHost)
|
||||
this.requestURIHasVariables = configutils.HasVariables(this.RequestURI)
|
||||
@@ -98,7 +98,7 @@ func (this *ReverseProxyConfig) Init() error {
|
||||
}
|
||||
|
||||
// scheduling
|
||||
this.SetupScheduling(false)
|
||||
this.SetupScheduling(false, false, true)
|
||||
|
||||
// Header
|
||||
if len(this.AddHeaders) == 0 {
|
||||
@@ -119,17 +119,17 @@ func (this *ReverseProxyConfig) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 添加主源站配置
|
||||
// AddPrimaryOrigin 添加主源站配置
|
||||
func (this *ReverseProxyConfig) AddPrimaryOrigin(origin *OriginConfig) {
|
||||
this.PrimaryOrigins = append(this.PrimaryOrigins, origin)
|
||||
}
|
||||
|
||||
// 添加备用源站配置
|
||||
// AddBackupOrigin 添加备用源站配置
|
||||
func (this *ReverseProxyConfig) AddBackupOrigin(origin *OriginConfig) {
|
||||
this.BackupOrigins = append(this.BackupOrigins, origin)
|
||||
}
|
||||
|
||||
// 取得下一个可用的后端服务
|
||||
// NextOrigin 取得下一个可用的后端服务
|
||||
func (this *ReverseProxyConfig) NextOrigin(call *shared.RequestCall) *OriginConfig {
|
||||
this.schedulingLocker.Lock()
|
||||
defer this.schedulingLocker.Unlock()
|
||||
@@ -145,14 +145,31 @@ func (this *ReverseProxyConfig) NextOrigin(call *shared.RequestCall) *OriginConf
|
||||
}
|
||||
|
||||
candidate := this.schedulingObject.Next(call)
|
||||
|
||||
// 末了重置状态
|
||||
defer func() {
|
||||
if candidate == nil {
|
||||
this.schedulingIsBackup = false
|
||||
}
|
||||
}()
|
||||
|
||||
if candidate == nil {
|
||||
// 启用备用服务器
|
||||
if !this.schedulingIsBackup {
|
||||
this.SetupScheduling(true)
|
||||
|
||||
this.SetupScheduling(true, true, false)
|
||||
candidate = this.schedulingObject.Next(call)
|
||||
if candidate == nil {
|
||||
return nil
|
||||
// 不检查主要源站
|
||||
this.SetupScheduling(false, false, false)
|
||||
candidate = this.schedulingObject.Next(call)
|
||||
if candidate == nil {
|
||||
// 不检查备用源站
|
||||
this.SetupScheduling(true, false, false)
|
||||
candidate = this.schedulingObject.Next(call)
|
||||
if candidate == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,12 +181,13 @@ func (this *ReverseProxyConfig) NextOrigin(call *shared.RequestCall) *OriginConf
|
||||
return candidate.(*OriginConfig)
|
||||
}
|
||||
|
||||
// 设置调度算法
|
||||
func (this *ReverseProxyConfig) SetupScheduling(isBackup bool) {
|
||||
if !isBackup {
|
||||
// SetupScheduling 设置调度算法
|
||||
func (this *ReverseProxyConfig) SetupScheduling(isBackup bool, checkOk bool, lock bool) {
|
||||
if lock {
|
||||
this.schedulingLocker.Lock()
|
||||
defer this.schedulingLocker.Unlock()
|
||||
}
|
||||
|
||||
this.schedulingIsBackup = isBackup
|
||||
|
||||
if this.Scheduling == nil {
|
||||
@@ -187,22 +205,26 @@ func (this *ReverseProxyConfig) SetupScheduling(isBackup bool) {
|
||||
|
||||
if !isBackup {
|
||||
for _, origin := range this.PrimaryOrigins {
|
||||
if origin.IsOn {
|
||||
if origin.IsOn && (origin.IsOk || !checkOk) {
|
||||
this.schedulingObject.Add(origin)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for _, origin := range this.BackupOrigins {
|
||||
if origin.IsOn {
|
||||
if origin.IsOn && (origin.IsOk || !checkOk) {
|
||||
this.schedulingObject.Add(origin)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !this.schedulingObject.HasCandidates() {
|
||||
return
|
||||
}
|
||||
|
||||
this.schedulingObject.Start()
|
||||
}
|
||||
|
||||
// 获取调度配置对象
|
||||
// FindSchedulingConfig 获取调度配置对象
|
||||
func (this *ReverseProxyConfig) FindSchedulingConfig() *SchedulingConfig {
|
||||
if this.Scheduling == nil {
|
||||
this.Scheduling = &SchedulingConfig{Code: "random"}
|
||||
@@ -210,37 +232,42 @@ func (this *ReverseProxyConfig) FindSchedulingConfig() *SchedulingConfig {
|
||||
return this.Scheduling
|
||||
}
|
||||
|
||||
// 判断RequestHost是否有变量
|
||||
// RequestHostHasVariables 判断RequestHost是否有变量
|
||||
func (this *ReverseProxyConfig) RequestHostHasVariables() bool {
|
||||
return this.requestHostHasVariables
|
||||
}
|
||||
|
||||
// 判断RequestURI是否有变量
|
||||
// RequestURIHasVariables 判断RequestURI是否有变量
|
||||
func (this *ReverseProxyConfig) RequestURIHasVariables() bool {
|
||||
return this.requestURIHasVariables
|
||||
}
|
||||
|
||||
// 是否添加X-Real-IP
|
||||
// ShouldAddXRealIPHeader 是否添加X-Real-IP
|
||||
func (this *ReverseProxyConfig) ShouldAddXRealIPHeader() bool {
|
||||
return this.addXRealIPHeader
|
||||
}
|
||||
|
||||
// 是否添加X-Forwarded-For
|
||||
// ShouldAddXForwardedForHeader 是否添加X-Forwarded-For
|
||||
func (this *ReverseProxyConfig) ShouldAddXForwardedForHeader() bool {
|
||||
return this.addXForwardedForHeader
|
||||
}
|
||||
|
||||
// 是否添加X-Forwarded-By
|
||||
// ShouldAddXForwardedByHeader 是否添加X-Forwarded-By
|
||||
func (this *ReverseProxyConfig) ShouldAddXForwardedByHeader() bool {
|
||||
return this.addXForwardedByHeader
|
||||
}
|
||||
|
||||
// 是否添加X-Forwarded-Host
|
||||
// ShouldAddXForwardedHostHeader 是否添加X-Forwarded-Host
|
||||
func (this *ReverseProxyConfig) ShouldAddXForwardedHostHeader() bool {
|
||||
return this.addXForwardedHostHeader
|
||||
}
|
||||
|
||||
// 是否添加X-Forwarded-Proto
|
||||
// ShouldAddXForwardedProtoHeader 是否添加X-Forwarded-Proto
|
||||
func (this *ReverseProxyConfig) ShouldAddXForwardedProtoHeader() bool {
|
||||
return this.addXForwardedProtoHeader
|
||||
}
|
||||
|
||||
// ResetScheduling 重置调度算法
|
||||
func (this *ReverseProxyConfig) ResetScheduling() {
|
||||
this.SetupScheduling(false, true, true)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package schedulingconfigs
|
||||
|
||||
import "github.com/iwind/TeaGo/maps"
|
||||
|
||||
// 所有请求类型
|
||||
// AllSchedulingTypes 所有请求类型
|
||||
func AllSchedulingTypes() []maps.Map {
|
||||
types := []maps.Map{}
|
||||
for _, s := range []SchedulingInterface{
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package schedulingconfigs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestFindSchedulingType(t *testing.T) {
|
||||
t.Logf("%p", FindSchedulingType("roundRobin"))
|
||||
t.Logf("%p", FindSchedulingType("roundRobin"))
|
||||
t.Logf("%p", FindSchedulingType("roundRobin"))
|
||||
}
|
||||
Reference in New Issue
Block a user