From a737e5cb20902f574c49aff6ccb1afbee699c2e9 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Sun, 27 Sep 2020 15:25:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=9F=BA=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E5=8F=8D=E5=90=91=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/serverconfigs/health_check_config.go | 14 +++- pkg/serverconfigs/network_address_config.go | 23 +++++- .../network_address_config_test.go | 48 +++++++++++ pkg/serverconfigs/origin_config.go | 81 ++++++++++++------- pkg/serverconfigs/origin_config_test.go | 15 ++++ pkg/serverconfigs/protocol.go | 5 ++ 6 files changed, 153 insertions(+), 33 deletions(-) create mode 100644 pkg/serverconfigs/origin_config_test.go diff --git a/pkg/serverconfigs/health_check_config.go b/pkg/serverconfigs/health_check_config.go index 39e20cd..a08f125 100644 --- a/pkg/serverconfigs/health_check_config.go +++ b/pkg/serverconfigs/health_check_config.go @@ -2,10 +2,16 @@ package serverconfigs import "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" +// 健康检查设置 type HealthCheckConfig struct { - IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启 TODO - URL string `yaml:"url" json:"url"` // TODO - Interval int `yaml:"interval" json:"interval"` // TODO - StatusCodes []int `yaml:"statusCodes" json:"statusCodes"` // TODO + IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启 TODO + URL string `yaml:"url" json:"url"` // TODO + Interval int `yaml:"interval" json:"interval"` // TODO + StatusCodes []int `yaml:"statusCodes" json:"statusCodes"` // TODO Timeout *shared.TimeDuration `yaml:"timeout" json:"timeout"` // 超时时间 TODO } + +// 初始化 +func (this *HealthCheckConfig) Init() error { + return nil +} diff --git a/pkg/serverconfigs/network_address_config.go b/pkg/serverconfigs/network_address_config.go index f164456..0d51d73 100644 --- a/pkg/serverconfigs/network_address_config.go +++ b/pkg/serverconfigs/network_address_config.go @@ -1,6 +1,8 @@ package serverconfigs import ( + "github.com/TeaOSLab/EdgeCommon/pkg/configutils" + "github.com/iwind/TeaGo/rands" "github.com/iwind/TeaGo/types" "regexp" "strconv" @@ -12,14 +14,19 @@ var regexpSinglePort = regexp.MustCompile(`^\d+$`) // 网络地址配置 type NetworkAddressConfig struct { Protocol Protocol `yaml:"protocol" json:"protocol"` // 协议,http、tcp、tcp4、tcp6、unix、udp等 - Host string `yaml:"host" json:"host"` // 主机地址或主机名 + Host string `yaml:"host" json:"host"` // 主机地址或主机名,支持变量 PortRange string `yaml:"portRange" json:"portRange"` // 端口范围,支持 8080、8080-8090、8080:8090 minPort int maxPort int + + hostHasVariables bool } +// 初始化 func (this *NetworkAddressConfig) Init() error { + this.hostHasVariables = configutils.HasVariables(this.Host) + // 8080 if regexpSinglePort.MatchString(this.PortRange) { this.minPort = types.Int(this.PortRange) @@ -56,6 +63,7 @@ func (this *NetworkAddressConfig) Init() error { return nil } +// 所有的地址列表,包含scheme func (this *NetworkAddressConfig) FullAddresses() []string { if this.Protocol == ProtocolUnix { return []string{this.Protocol.String() + ":" + this.Host} @@ -68,3 +76,16 @@ func (this *NetworkAddressConfig) FullAddresses() []string { } return result } + +// 选择其中一个地址 +func (this *NetworkAddressConfig) PickAddress() string { + if this.maxPort > this.minPort { + return this.Host + ":" + strconv.Itoa(rands.Int(this.minPort, this.maxPort)) + } + return this.Host + ":" + strconv.Itoa(this.minPort) +} + +// 判断Host是否包含变量 +func (this *NetworkAddressConfig) HostHasVariables() bool { + return this.hostHasVariables +} diff --git a/pkg/serverconfigs/network_address_config_test.go b/pkg/serverconfigs/network_address_config_test.go index bb14dba..02fc18b 100644 --- a/pkg/serverconfigs/network_address_config_test.go +++ b/pkg/serverconfigs/network_address_config_test.go @@ -55,3 +55,51 @@ func TestNetworkAddressConfig_FullAddresses(t *testing.T) { t.Log(addr.FullAddresses()) } } + +func TestNetworkAddressConfig_PickAddress(t *testing.T) { + { + addr := &NetworkAddressConfig{ + Host: "127.0.0.1", + PortRange: "1234", + } + err := addr.Init() + if err != nil { + t.Fatal(err) + } + t.Log(addr.PickAddress()) + } + + { + addr := &NetworkAddressConfig{ + Host: "127.0.0.1", + PortRange: "8000-9000", + } + err := addr.Init() + if err != nil { + t.Fatal(err) + } + t.Log(addr.PickAddress()) + } + { + addr := &NetworkAddressConfig{ + Host: "127.0.0.1", + PortRange: "8000-8001", + } + err := addr.Init() + if err != nil { + t.Fatal(err) + } + t.Log(addr.PickAddress()) + } + { + addr := &NetworkAddressConfig{ + Host: "127.0.0.1", + PortRange: "9000-8000", + } + err := addr.Init() + if err != nil { + t.Fatal(err) + } + t.Log(addr.PickAddress()) + } +} diff --git a/pkg/serverconfigs/origin_config.go b/pkg/serverconfigs/origin_config.go index a7f3fdd..544fc1e 100644 --- a/pkg/serverconfigs/origin_config.go +++ b/pkg/serverconfigs/origin_config.go @@ -2,6 +2,7 @@ package serverconfigs import ( "fmt" + "github.com/TeaOSLab/EdgeCommon/pkg/configutils" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs" "strconv" @@ -27,8 +28,9 @@ type OriginConfig struct { MaxConns int `yaml:"maxConns" json:"maxConns"` // 最大并发连接数 TODO MaxIdleConns int `yaml:"idleConns" json:"idleConns"` // 最大空闲连接数 TODO - RequestURI string `yaml:"requestURI" json:"requestURI"` // 转发后的请求URI TODO - Host string `yaml:"host" json:"host"` // 自定义主机名 TODO + StripPrefix string `yaml:"stripPrefix" json:"stripPrefix"` // 去除URL前缀 + RequestURI string `yaml:"requestURI" json:"requestURI"` // 转发后的请求URI TODO + RequestHost string `yaml:"requestHost" json:"requestHost"` // 自定义主机名 TODO RequestHeaderPolicyRef *shared.HTTPHeaderPolicyRef `yaml:"requestHeaderPolicyRef" json:"requestHeaderPolicyRef"` // 请求Header RequestHeaderPolicy *shared.HTTPHeaderPolicy `yaml:"requestHeaderPolicy" json:"requestHeaderPolicy"` // 请求Header策略 @@ -55,17 +57,29 @@ type OriginConfig struct { hasRequestHeaders bool hasResponseHeaders bool - hasHost bool - uniqueKey string - hasAddrVariables bool // 地址中是否含有变量 - - realAddr string // 最终的Addr TODO + requestHostHasVariables bool + requestURIHasVariables bool } // 校验 func (this *OriginConfig) Init() error { + // URL + this.requestHostHasVariables = configutils.HasVariables(this.RequestHost) + this.requestURIHasVariables = configutils.HasVariables(this.RequestURI) + + // unique key + this.uniqueKey = strconv.FormatInt(this.Id, 10) + "@" + strconv.Itoa(this.Version) + "@" + fmt.Sprintf("%p", this) + + // addr + if this.Addr != nil { + err := this.Addr.Init() + if err != nil { + return err + } + } + // 证书 if this.Cert != nil { err := this.Cert.Init() @@ -74,9 +88,6 @@ func (this *OriginConfig) Init() error { } } - // unique key - this.uniqueKey = strconv.FormatInt(this.Id, 10) + "@" + fmt.Sprintf("%d", this.Version) - // failTimeout if this.ConnTimeout != nil { this.connTimeoutDuration = this.ConnTimeout.Duration() @@ -133,14 +144,13 @@ func (this *OriginConfig) Init() error { } } - // TODO init health check - - // host - this.hasHost = len(this.Host) > 0 - - // variables - // TODO 在host和port中支持变量 - this.hasAddrVariables = false + // health check + if this.HealthCheck != nil { + err := this.HealthCheck.Init() + if err != nil { + return err + } + } return nil } @@ -159,17 +169,32 @@ func (this *OriginConfig) CandidateWeight() uint { return this.Weight } -// 获取最终请求的地址 -func (this *OriginConfig) RealAddr() string { - return this.realAddr -} - -// 设置最终请求的地址 TODO 需要实现 -func (this *OriginConfig) SetRealAddr(realAddr string) { - this.realAddr = realAddr -} - // 连接超时时间 func (this *OriginConfig) ConnTimeoutDuration() time.Duration { return this.connTimeoutDuration } + +// 读取超时时间 +func (this *OriginConfig) ReadTimeoutDuration() time.Duration { + return this.readTimeoutDuration +} + +// 休眠超时时间 +func (this *OriginConfig) IdleTimeoutDuration() time.Duration { + return this.idleTimeoutDuration +} + +// 判断RequestHost是否有变量 +func (this *OriginConfig) RequestHostHasVariables() bool { + return this.requestHostHasVariables +} + +// 判断RequestURI是否有变量 +func (this *OriginConfig) RequestURIHasVariables() bool { + return this.requestURIHasVariables +} + +// 唯一Key +func (this *OriginConfig) UniqueKey() string { + return this.uniqueKey +} diff --git a/pkg/serverconfigs/origin_config_test.go b/pkg/serverconfigs/origin_config_test.go new file mode 100644 index 0000000..ece9a01 --- /dev/null +++ b/pkg/serverconfigs/origin_config_test.go @@ -0,0 +1,15 @@ +package serverconfigs + +import "testing" + +func TestOriginConfig_UniqueKey(t *testing.T) { + origin := &OriginConfig{ + Id: 1, + Version: 101, + } + err := origin.Init() + if err != nil { + t.Fatal(err) + } + t.Log(origin.UniqueKey()) +} diff --git a/pkg/serverconfigs/protocol.go b/pkg/serverconfigs/protocol.go index b5be234..266b1b1 100644 --- a/pkg/serverconfigs/protocol.go +++ b/pkg/serverconfigs/protocol.go @@ -72,6 +72,11 @@ func (this Protocol) Primary() Protocol { } } +// Scheme +func (this Protocol) Scheme() string { + return string(this) +} + // 转换为字符串 func (this Protocol) String() string { return string(this)