session

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package session 提供基于 WebSocket 的有状态会话高阶封装。

在 pkg/ws 的薄封装之上,补齐长连接生产级所需的:

  • 双 goroutine 读写模型:读循环(主)与写循环(子)分离,所有写串行化, 避免对 conn 的并发写(coder/websocket 允许并发读写,但同方向需串行);
  • 心跳:写循环按周期发 Ping(带超时),CloseRead 在后台处理 pong/close 控制帧; 读循环的 ctx 在连接断开时自动取消,用于检测半开;
  • 关闭握手:Close 只发一次 close 帧,CloseRead 返回的 ctx 取消即代表对端断开;
  • 写超时保护:每条写用独立带超时的 ctx,慢客户端不拖垮会话。

Consume/processOutgoing/pingNow, 适配 coder/websocket 的 context-based API。

使用:

mux.Handle("/ws", ws.Handler(session.Accept(myHandler, opts...)))

其中 myHandler 实现 session.Handler,在 OnMessage 里读消息、用 Send 投递写。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Accept

func Accept(h Handler, opts ...Option) func(*http.Request, *ws.Conn) error

Accept 返回一个 ws.Handler,把每条连接升级为有状态会话并交给 h 处理。

mux.Handle("/ws", ws.Handler(session.Accept(myHandler,
    session.WithPingPeriod(30*time.Second),
), ws.WithSubprotocols("v1")))

Types

type Envelope

type Envelope struct {
	Type MessageType
	Data []byte
}

Envelope 是一条待发送的消息:类型 + 负载。

type Handler

type Handler interface {
	// OnOpen 在握手成功、会话就绪后调用一次。返回 error 立即关闭会话。
	OnOpen(s *Session) error
	// OnMessage 在收到一条客户端消息时调用。返回 error 关闭会话。
	OnMessage(s *Session, typ MessageType, data []byte) error
	// OnClose 在会话结束(任何原因)时调用一次,用于清理。
	OnClose(s *Session, reason string)
}

Handler 由业务实现,定义会话生命周期。 OnOpen/OnMessage/OnClose 都在读循环 goroutine 内串行调用,故业务状态可无锁。

type MessageType

type MessageType = ws.MessageType

MessageType 区分文本/二进制消息。

type Option

type Option func(*config)

Option 配置 Accept 行为。

func WithMaxMessageSize

func WithMaxMessageSize(n int64) Option

WithMaxMessageSize 设置单条消息读取上限(字节),默认 0=不限。

func WithPingBackoff

func WithPingBackoff(n int) Option

WithPingBackoff 设置"每收 N 条消息才 ping 一次"的阈值,默认 20。 业务流量大时少 ping,省带宽。

func WithPingPeriod

func WithPingPeriod(d time.Duration) Option

WithPingPeriod 设置主动 ping 周期。默认 54s(小于多数网关 60s 空闲)。 <=0 不主动 ping(此时也不检测半开)。

func WithPingTimeout

func WithPingTimeout(d time.Duration) Option

WithPingTimeout 设置单次 ping 的等待超时(等 pong),默认 5s。 ping 超时即视为半开,关闭会话。

func WithSendQueue

func WithSendQueue(n int) Option

WithSendQueue 设置发送队列容量,默认 256。队列满(慢客户端)时关闭会话。

func WithWriteTimeout

func WithWriteTimeout(d time.Duration) Option

WithWriteTimeout 设置每条业务写的超时,默认 10s。

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session 是一个有状态 WebSocket 会话。零值不可用,由 Accept 创建。

func (*Session) Close

func (s *Session) Close(reason string)

Close 主动关闭会话。幂等。reason 会传给 OnClose。

func (*Session) Context

func (s *Session) Context() context.Context

Context 返回会话生命周期 context,在会话关闭时取消。

func (*Session) ID

func (s *Session) ID() uint64

ID 返回会话的唯一自增 ID(进程内唯一)。

func (*Session) Send

func (s *Session) Send(typ MessageType, data []byte) bool

Send 投递一条消息到写循环队列,异步发送。会话已停止或队列满时返回 false。 队列满(慢客户端)会触发关闭会话,避免内存堆积。

func (*Session) SendJSON

func (s *Session) SendJSON(v any) bool

SendJSON 把 v 序列化为 JSON 文本消息发送。

func (*Session) SendText

func (s *Session) SendText(b []byte) bool

SendText 是 Send(Text, ...) 的便捷封装。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL