Documentation
¶
Overview ¶
Package cli 提供面向 gate 服务端的客户端传输层。
可通过 Connect 建立使用框架 GTP 与 GAP 协议的 TCP 或 WebSocket 会话, 也可在 rpcli 之类的上层工具中复用 Client。
Index ¶
- Variables
- func Reconnect(client *Client) error
- type Client
- func (c *Client) Close(err error) async.Signal
- func (c *Client) Closed() async.Signal
- func (c *Client) DataIO() IDataIO
- func (c *Client) Endpoint() string
- func (c *Client) EventIO() IEventIO
- func (c *Client) Extensions() []byte
- func (c *Client) FutureController() *concurrent.FutureController
- func (c *Client) L() *zap.Logger
- func (c *Client) Migrations() int64
- func (c *Client) NetAddr() NetAddr
- func (c *Client) ProbeTime() async.Future
- func (c *Client) S() *zap.SugaredLogger
- func (c *Client) SessionId() uid.Id
- func (c *Client) String() string
- func (c *Client) Token() string
- func (c *Client) UserId() string
- type ClientOptions
- type DataHandler
- type EventHandler
- type IDataIO
- type IEventIO
- type NetAddr
- type NetProtocol
- type TimeSample
Constants ¶
This section is empty.
Variables ¶
var ( // ErrAutoReconnectRetriesExhausted 表示自动重连已用尽配置的尝试次数。 ErrAutoReconnectRetriesExhausted = errors.New("cli: auto reconnect retries exhausted") // ErrInactiveTimeout 表示未启用自动重连的连接持续失活并超过等待时间。 ErrInactiveTimeout = errors.New("cli: inactive timeout") )
var With _ClientOption
With 提供 gate 客户端的 Option 构造方法。
Functions ¶
Types ¶
type Client ¶
Client 是支持 GTP 握手、心跳和连接迁移的并发安全客户端。 其上下文在父上下文取消或 Close 被调用时取消。
func Connect ¶
func Connect(ctx context.Context, endpoint string, settings ...option.Setting[ClientOptions]) (*Client, error)
Connect 使用 settings 连接 endpoint,完成 GTP 握手后启动客户端循环。 TCP 模式下 endpoint 为 host:port;WebSocket 模式下为 ws/wss URL。 ctx 为 nil 时使用 context.Background,取消 ctx 会关闭返回的客户端。
func (*Client) Extensions ¶ added in v0.3.68
Extensions 返回握手使用的扩展数据;调用方不得修改。
func (*Client) FutureController ¶ added in v0.3.68
func (c *Client) FutureController() *concurrent.FutureController
FutureController 返回用于关联请求与响应的 Future 控制器。
func (*Client) Migrations ¶ added in v0.3.68
Migrations 返回连接成功迁移的累计次数。
type ClientOptions ¶
type ClientOptions struct {
NetProtocol NetProtocol // NetProtocol 选择 TCP 或 WebSocket。
TCPNoDelay *bool // TCPNoDelay 为 nil 时沿用系统默认值。
TCPQuickAck *bool // TCPQuickAck 为 nil 时沿用系统默认值。
TCPRecvBuf *int // TCPRecvBuf 是接收缓冲区字节数;nil 沿用系统默认值。
TCPSendBuf *int // TCPSendBuf 是发送缓冲区字节数;nil 沿用系统默认值。
TCPLinger *int // TCPLinger 是关闭等待秒数;nil 沿用系统默认值。
WebSocketOrigin string // WebSocketOrigin 为空时根据 endpoint 和用户 ID 生成。
TLSConfig *tls.Config // TLSConfig 非 nil 时为 TCP 或安全 WebSocket 启用 TLS。
IOTimeout time.Duration // IOTimeout 是单次网络 I/O 的超时。
IORetryTimes int // IORetryTimes 是 I/O 超时后的重试次数。
IOBufferCap int // IOBufferCap 是断线重连时保留的发送数据字节上限。
MsgCreator gtp.IMsgCreator // MsgCreator 用于按消息 ID 创建 GTP 解码目标。
EncCipherSuite gtp.CipherSuite // EncCipherSuite 是客户端提出的密码套件。
EncSignatureAlgorithm gtp.SignatureAlgorithm // EncSignatureAlgorithm 是客户端握手签名算法。
EncSignaturePrivateKey crypto.PrivateKey // EncSignaturePrivateKey 是客户端握手签名私钥。
EncVerifyServerSignature bool // EncVerifyServerSignature 要求验证服务端握手签名。
EncVerifySignaturePublicKey crypto.PublicKey // EncVerifySignaturePublicKey 是服务端签名验证公钥。
Compression gtp.Compression // Compression 是客户端提出的压缩算法。
CompressionThreshold int // CompressionThreshold 是启用压缩的字节阈值;小于等于 0 时禁用。
MaxUncompressedSize int // MaxUncompressedSize 限制解压后负载,防御压缩炸弹。
MaxPacketSize int // MaxPacketSize 限制单个 GTP 包大小。
AutoReconnect bool // AutoReconnect 在连接失活后自动迁移会话连接。
AutoReconnectInterval time.Duration // AutoReconnectInterval 是相邻重连尝试的间隔。
AutoReconnectRetryTimes int // AutoReconnectRetryTimes 小于等于 0 时无限重试。
InactiveTimeout time.Duration // InactiveTimeout 是未启用自动重连时的失活等待时间。
FutureTimeout time.Duration // FutureTimeout 是时间探测等关联请求的默认超时。
AuthUserId string // AuthUserId 是握手提交的用户 ID。
AuthToken string // AuthToken 是握手提交的鉴权令牌。
AuthExtensions []byte // AuthExtensions 是握手提交的扩展数据。
AutoRecover bool // AutoRecover 控制监听器 panic 是否自动恢复。
ReportError chan error // ReportError 接收自动恢复的 panic 错误。
DataListenerInboxSize int // DataListenerInboxSize 是每个数据监听器的收件箱容量。
EventListenerInboxSize int // EventListenerInboxSize 是每个事件监听器的收件箱容量。
Logger *zap.Logger // Logger 是客户端日志器;nil 时不输出日志。
}
ClientOptions 配置连接协议、GTP 协商、重连、监听器和日志行为。
type DataHandler ¶ added in v0.3.68
type DataHandler = generic.DelegateVoid1[[]byte]
DataHandler 处理客户端收到的一段原始负载。
type EventHandler ¶ added in v0.3.68
type EventHandler = transport.EventHandler
EventHandler 处理客户端收到的一个 GTP 事件。
type IDataIO ¶ added in v0.3.68
type IDataIO interface {
// Send 复制 data 并将其加入发送队列。
Send(data []byte) error
// Listen 注册监听器,直到 ctx 取消或客户端关闭。
Listen(ctx context.Context, handler DataHandler) error
}
IDataIO 提供客户端原始负载的异步发送与监听。
type IEventIO ¶ added in v0.3.68
type IEventIO interface {
// Send 将 event 加入发送队列;调用方须保证事件在处理完成前保持有效。
Send(event transport.IEvent) error
// Listen 注册监听器,直到 ctx 取消或客户端关闭。
Listen(ctx context.Context, handler EventHandler) error
}
IEventIO 提供客户端 GTP 事件的异步发送与监听。
type NetProtocol ¶
type NetProtocol int32
NetProtocol 选择客户端建立底层连接的协议。
const ( // TCP 使用原生 TCP 连接。 TCP NetProtocol = iota // WebSocket 使用二进制 WebSocket 连接。 WebSocket )
type TimeSample ¶ added in v0.3.68
type TimeSample struct {
OriginTime time.Time // OriginTime 是请求方发送请求的 t1。
ReceiveTime time.Time // ReceiveTime 是响应方收到请求的 t2。
TransmitTime time.Time // TransmitTime 是响应方发送响应的 t3。
DestinationTime time.Time // DestinationTime 是请求方收到响应的 t4。
RemoteZoneOffset int // RemoteZoneOffset 是响应方相对 UTC 的偏移秒数。
}
TimeSample 保存一次类 NTP 时钟探测的四个时间点,时间值以对端时区显示。
func (TimeSample) Offset ¶ added in v0.3.68
func (ts TimeSample) Offset() time.Duration
Offset 估算对端时钟相对于本地时钟的偏移量。
func (TimeSample) RTT ¶ added in v0.3.68
func (ts TimeSample) RTT() time.Duration
RTT 根据四个采样点估算扣除服务端处理时间后的网络往返时延。
func (TimeSample) RemoteLocation ¶ added in v0.3.68
func (ts TimeSample) RemoteLocation() *time.Location
RemoteLocation 返回由采样时区偏移构造的固定时区。
func (TimeSample) RemoteNow ¶ added in v0.3.68
func (ts TimeSample) RemoteNow() time.Time
RemoteNow 使用本次采样的固定偏移估算当前对端时间。
func (TimeSample) RemoteTime ¶ added in v0.3.68
func (ts TimeSample) RemoteTime() time.Time
RemoteTime 估算请求方收到响应时刻的对端时间。