Documentation ¶
Index ¶
- Constants
- Variables
- func MarshalGatewayInPacket(addr string, currentTime int64, packet []byte) ([]byte, error)
- func MarshalGatewayOutPacket(addr string, packet []byte) ([]byte, error)
- func UnmarshalGatewayInPacket(data []byte) (addr string, sendTime int64, packet []byte, err error)
- func UnmarshalGatewayOutPacket(data []byte) (addr string, packet []byte, err error)
- type ConnectionClosedEventHandle
- type ConnectionOpenedEventHandle
- type ConnectionReceivePacketEventHandle
- type Endpoint
- type EndpointConnectClosedEventHandle
- type EndpointConnectOpenedEventHandle
- type EndpointConnectReceivePacketEventHandle
- type EndpointOption
- type EndpointSelector
- type Gateway
- func (slf *Gateway) GetConnEndpoint(name string, conn *server.Conn) (*Endpoint, error)
- func (slf *Gateway) GetEndpoint(name string) (*Endpoint, error)
- func (slf Gateway) OnConnectionClosedEvent(gateway *Gateway, conn *server.Conn)
- func (slf Gateway) OnConnectionOpenedEvent(gateway *Gateway, conn *server.Conn)
- func (slf Gateway) OnConnectionReceivePacketEvent(gateway *Gateway, conn *server.Conn, packet []byte)
- func (slf Gateway) OnEndpointConnectClosedEvent(gateway *Gateway, endpoint *Endpoint)
- func (slf Gateway) OnEndpointConnectOpenedEvent(gateway *Gateway, endpoint *Endpoint)
- func (slf Gateway) OnEndpointConnectReceivePacketEvent(gateway *Gateway, endpoint *Endpoint, conn *server.Conn, packet []byte)
- func (slf Gateway) RegConnectionClosedEventHandle(handle ConnectionClosedEventHandle, priority ...int)
- func (slf Gateway) RegConnectionOpenedEventHandle(handle ConnectionOpenedEventHandle, priority ...int)
- func (slf Gateway) RegConnectionReceivePacketEventHandle(handle ConnectionReceivePacketEventHandle, priority ...int)
- func (slf Gateway) RegEndpointConnectClosedEventHandle(handle EndpointConnectClosedEventHandle, priority ...int)
- func (slf Gateway) RegEndpointConnectOpenedEventHandle(handle EndpointConnectOpenedEventHandle, priority ...int)
- func (slf Gateway) RegEndpointConnectReceivePacketEventHandle(handle EndpointConnectReceivePacketEventHandle, priority ...int)
- func (slf *Gateway) Run(addr string) error
- func (slf *Gateway) Server() *server.Server
- func (slf *Gateway) Shutdown()
- func (slf *Gateway) SwitchEndpoint(source, dest *Endpoint)
- type Option
- type Scanner
Constants ¶
const ( DefaultEndpointReconnectInterval = time.Second DefaultEndpointConnectionPoolSize = 1 )
Variables ¶
var ( // ErrEndpointNotExists 该名称下不存在任何端点 ErrEndpointNotExists = errors.New("gateway: endpoint not exists") // ErrGatewayClosed 网关已关闭 ErrGatewayClosed = errors.New("gateway: gateway closed") // ErrGatewayRunning 网关正在运行 ErrGatewayRunning = errors.New("gateway: gateway running") // ErrConnectionNotFount 该端点下不存在该连接 ErrConnectionNotFount = errors.New("gateway: connection not found") )
Functions ¶
func MarshalGatewayInPacket ¶ added in v0.1.0
MarshalGatewayInPacket 将数据包转换为网关入网数据包
- | ipv4(4) | port(2) | cost(4) | packet |
func MarshalGatewayOutPacket ¶ added in v0.1.0
MarshalGatewayOutPacket 将数据包转换为网关出网数据包
- | identifier(4) | ipv4(4) | port(2) | packet |
func UnmarshalGatewayInPacket ¶ added in v0.1.0
UnmarshalGatewayInPacket 将网关入网数据包转换为数据包
- | ipv4(4) | port(2) | cost(4) | packet |
Types ¶
type ConnectionClosedEventHandle ¶ added in v0.1.1
type ConnectionOpenedEventHandle ¶ added in v0.1.1
type ConnectionReceivePacketEventHandle ¶ added in v0.1.1
type Endpoint ¶
type Endpoint struct {
// contains filtered or unexported fields
}
Endpoint 网关端点
func NewEndpoint ¶
func NewEndpoint(name string, cli *client.Client, options ...EndpointOption) *Endpoint
NewEndpoint 创建网关端点
func (*Endpoint) Forward ¶ added in v0.1.1
Forward 转发数据包到该端点
- 端点在处理数据包时,应区分数据包为普通直连数据包还是网关数据包。可通过 UnmarshalGatewayOutPacket 进行数据包解析,当解析失败且无其他数据包协议时,可认为该数据包为普通直连数据包。
func (*Endpoint) GetAddress ¶ added in v0.1.1
GetAddress 获取端点地址
type EndpointConnectClosedEventHandle ¶ added in v0.1.1
type EndpointConnectOpenedEventHandle ¶ added in v0.1.1
type EndpointConnectReceivePacketEventHandle ¶ added in v0.1.1
type EndpointOption ¶ added in v0.0.30
type EndpointOption func(endpoint *Endpoint)
EndpointOption 网关端点选项
func WithEndpointConnectionPoolSize ¶ added in v0.1.2
func WithEndpointConnectionPoolSize(size int) EndpointOption
WithEndpointConnectionPoolSize 设置端点连接池大小
- 默认为 DefaultEndpointConnectionPoolSize
- 端点连接池大小决定了网关服务器与端点服务器建立的连接数,如果 <= 0 则会使用默认值
- 在网关服务器中,多个客户端在发送消息到端点服务器时,会共用一个连接,适当的增大连接池大小可以提高网关服务器的承载能力
func WithEndpointReconnectInterval ¶ added in v0.1.2
func WithEndpointReconnectInterval(interval time.Duration) EndpointOption
WithEndpointReconnectInterval 设置端点重连间隔
- 默认为 DefaultEndpointReconnectInterval
- 端点在连接失败后会在该间隔后重连,如果 <= 0 则不会重连
func WithEndpointStateEvaluator ¶ added in v0.0.30
func WithEndpointStateEvaluator(evaluator func(costUnixNano float64) float64) EndpointOption
WithEndpointStateEvaluator 设置端点健康值评估函数
type EndpointSelector ¶ added in v0.1.1
EndpointSelector 端点选择器,用于从多个端点中选择一个可用的端点,如果没有可用的端点则返回 nil
type Gateway ¶
type Gateway struct {
// contains filtered or unexported fields
}
Gateway 基于 server.Server 实现的网关服务器
- 网关服务器是一个特殊的服务器,它会通过扫描器扫描端点列表,然后连接到端点列表中的所有端点,当端点连接成功后,网关服务器会将客户端的连接数据转发到端点服务器
- 由于该网关为多个客户端共享一个端点的连接,所以不会受限于单机 65535 个端口的限制
- 需要注意的是,网关可以通过扫描器得到每个端点需要建立多少个连接,该连接总数将受限与 65535 个端口的限制
一些特性:
- 支持 server.Server 所支持的所有 Socket 网络类型
- 支持将客户端网络类型进行不同的转换,例如:客户端使用 Websocket 连接,但是网关服务器可以将其转换为 TCP 端点的连接
- 支持客户端消息绑定,在客户端未断开连接的情况下,可以将客户端的连接绑定到某个端点,这样该客户端的所有消息都会转发到该端点
- 根据端点延迟实时调整端点状态评分,根据评分选择最优的端点,默认评分算法为:1 / (1 + 1.5 * ${DelaySeconds})
func NewGateway ¶
NewGateway 基于 server.Server 创建 Gateway 网关服务器
func (*Gateway) GetConnEndpoint ¶ added in v0.1.2
GetConnEndpoint 获取一个可用的端点,如果客户端已经连接到了某个端点,将优先返回该端点
- 当连接到的端点不可用或没有连接记录时,效果同 GetEndpoint 相同
- 当连接行为为有状态时,推荐使用该方法
func (Gateway) OnConnectionClosedEvent ¶ added in v0.1.1
func (Gateway) OnConnectionOpenedEvent ¶ added in v0.1.1
func (Gateway) OnConnectionReceivePacketEvent ¶ added in v0.1.1
func (Gateway) OnEndpointConnectClosedEvent ¶ added in v0.1.1
func (Gateway) OnEndpointConnectOpenedEvent ¶ added in v0.1.1
func (Gateway) OnEndpointConnectReceivePacketEvent ¶ added in v0.1.1
func (Gateway) RegConnectionClosedEventHandle ¶ added in v0.1.1
func (slf Gateway) RegConnectionClosedEventHandle(handle ConnectionClosedEventHandle, priority ...int)
RegConnectionClosedEventHandle 注册客户端连接关闭事件处理函数
func (Gateway) RegConnectionOpenedEventHandle ¶ added in v0.1.1
func (slf Gateway) RegConnectionOpenedEventHandle(handle ConnectionOpenedEventHandle, priority ...int)
RegConnectionOpenedEventHandle 注册客户端连接打开事件处理函数
func (Gateway) RegConnectionReceivePacketEventHandle ¶ added in v0.1.1
func (slf Gateway) RegConnectionReceivePacketEventHandle(handle ConnectionReceivePacketEventHandle, priority ...int)
RegConnectionReceivePacketEventHandle 注册客户端连接接收数据包事件处理函数
func (Gateway) RegEndpointConnectClosedEventHandle ¶ added in v0.1.1
func (slf Gateway) RegEndpointConnectClosedEventHandle(handle EndpointConnectClosedEventHandle, priority ...int)
RegEndpointConnectClosedEventHandle 注册端点连接关闭事件处理函数
func (Gateway) RegEndpointConnectOpenedEventHandle ¶ added in v0.1.1
func (slf Gateway) RegEndpointConnectOpenedEventHandle(handle EndpointConnectOpenedEventHandle, priority ...int)
RegEndpointConnectOpenedEventHandle 注册端点连接打开事件处理函数
func (Gateway) RegEndpointConnectReceivePacketEventHandle ¶ added in v0.1.1
func (slf Gateway) RegEndpointConnectReceivePacketEventHandle(handle EndpointConnectReceivePacketEventHandle, priority ...int)
RegEndpointConnectReceivePacketEventHandle 注册端点连接接收数据包事件处理函数
func (*Gateway) SwitchEndpoint ¶ added in v0.1.2
SwitchEndpoint 将端点端点的所有连接切换到另一个端点
type Option ¶
type Option func(gateway *Gateway)
Option 网关选项
func WithEndpointSelector ¶
func WithEndpointSelector(selector EndpointSelector) Option
WithEndpointSelector 设置端点选择器
- 默认情况下,网关会随机选择一个端点作为目标,如果需要自定义端点选择器,可以通过该选项设置