feat: gateway.Endpoint 支持设置重连间隔
This commit is contained in:
parent
30e7894a37
commit
cdfecb41e8
|
@ -8,6 +8,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var DefaultEndpointReconnectInterval = time.Second
|
||||||
|
|
||||||
// NewEndpoint 创建网关端点
|
// NewEndpoint 创建网关端点
|
||||||
func NewEndpoint(name string, cli *client.Client, options ...EndpointOption) *Endpoint {
|
func NewEndpoint(name string, cli *client.Client, options ...EndpointOption) *Endpoint {
|
||||||
endpoint := &Endpoint{
|
endpoint := &Endpoint{
|
||||||
|
@ -15,6 +17,7 @@ func NewEndpoint(name string, cli *client.Client, options ...EndpointOption) *En
|
||||||
name: name,
|
name: name,
|
||||||
address: cli.GetServerAddr(),
|
address: cli.GetServerAddr(),
|
||||||
connections: haxmap.New[string, *server.Conn](),
|
connections: haxmap.New[string, *server.Conn](),
|
||||||
|
rci: DefaultEndpointReconnectInterval,
|
||||||
}
|
}
|
||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
option(endpoint)
|
option(endpoint)
|
||||||
|
@ -36,6 +39,7 @@ type Endpoint struct {
|
||||||
state float64 // 端点健康值(0为不可用,越高越优)
|
state float64 // 端点健康值(0为不可用,越高越优)
|
||||||
evaluator func(costUnixNano float64) float64 // 端点健康值评估函数
|
evaluator func(costUnixNano float64) float64 // 端点健康值评估函数
|
||||||
connections *haxmap.Map[string, *server.Conn] // 被该端点转发的连接列表
|
connections *haxmap.Map[string, *server.Conn] // 被该端点转发的连接列表
|
||||||
|
rci time.Duration // 端点重连间隔
|
||||||
}
|
}
|
||||||
|
|
||||||
// connect 连接端点
|
// connect 连接端点
|
||||||
|
@ -52,7 +56,12 @@ func (slf *Endpoint) connect(gateway *Gateway) {
|
||||||
slf.state = slf.evaluator(float64(time.Now().UnixNano() - cur))
|
slf.state = slf.evaluator(float64(time.Now().UnixNano() - cur))
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
time.Sleep(1000 * time.Millisecond)
|
if slf.rci > 0 {
|
||||||
|
time.Sleep(slf.rci)
|
||||||
|
} else {
|
||||||
|
slf.state = 0
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
slf.client.RegConnectionReceivePacketEvent(func(conn *client.Client, wst int, packet []byte) {
|
slf.client.RegConnectionReceivePacketEvent(func(conn *client.Client, wst int, packet []byte) {
|
||||||
|
@ -76,7 +85,12 @@ func (slf *Endpoint) connect(gateway *Gateway) {
|
||||||
slf.state = slf.evaluator(float64(time.Now().UnixNano() - cur))
|
slf.state = slf.evaluator(float64(time.Now().UnixNano() - cur))
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
time.Sleep(1000 * time.Millisecond)
|
if slf.rci > 0 {
|
||||||
|
time.Sleep(slf.rci)
|
||||||
|
} else {
|
||||||
|
slf.state = 0
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package gateway
|
package gateway
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
// EndpointOption 网关端点选项
|
// EndpointOption 网关端点选项
|
||||||
type EndpointOption func(endpoint *Endpoint)
|
type EndpointOption func(endpoint *Endpoint)
|
||||||
|
|
||||||
|
@ -9,3 +11,12 @@ func WithEndpointStateEvaluator(evaluator func(costUnixNano float64) float64) En
|
||||||
endpoint.evaluator = evaluator
|
endpoint.evaluator = evaluator
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithReconnectInterval 设置端点重连间隔
|
||||||
|
// - 默认为 DefaultEndpointReconnectInterval
|
||||||
|
// - 端点在连接失败后会在该间隔后重连,如果 <= 0 则不会重连
|
||||||
|
func WithReconnectInterval(interval time.Duration) EndpointOption {
|
||||||
|
return func(endpoint *Endpoint) {
|
||||||
|
endpoint.rci = interval
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue