Documentation
¶
Overview ¶
Package connection 提供连接分桶管理 通过分桶减少锁竞争,提升并发性能
Package connection 提供连接管理功能 包括连接封装、连接池和分桶管理
Package connection 提供连接管理器 负责连接的生命周期管理和查询
Index ¶
- Variables
- func GenerateConnID() string
- type Bucket
- type BucketPool
- func (p *BucketPool) Add(conn *Connection)
- func (p *BucketPool) Count() int
- func (p *BucketPool) Get(connID string) *Connection
- func (p *BucketPool) GetBucket(connID string) *Bucket
- func (p *BucketPool) Range(fn func(*Connection) bool)
- func (p *BucketPool) RangeParallel(fn func(*Connection))
- func (p *BucketPool) Remove(connID string) *Connection
- type Connection
- func (c *Connection) AuthenticatedAt() time.Time
- func (c *Connection) Close() error
- func (c *Connection) CloseWithReason(ctx context.Context, reason string) error
- func (c *Connection) Codec() *protocol.Codec
- func (c *Connection) ConnectedAt() time.Time
- func (c *Connection) DeviceID() string
- func (c *Connection) GetMetadata(key string) (interface{}, bool)
- func (c *Connection) GnetConn() gnet.Conn
- func (c *Connection) ID() string
- func (c *Connection) IdleDuration() time.Duration
- func (c *Connection) IsAuthenticated() bool
- func (c *Connection) IsConnected() bool
- func (c *Connection) IsWSUpgraded() bool
- func (c *Connection) LastActiveAt() time.Time
- func (c *Connection) LocalAddr() string
- func (c *Connection) Platform() string
- func (c *Connection) RemoteAddr() string
- func (c *Connection) SetAuthenticatedAt(t time.Time)
- func (c *Connection) SetDeviceID(deviceID string)
- func (c *Connection) SetMetadata(key string, value interface{})
- func (c *Connection) SetOnClose(fn func(*Connection))
- func (c *Connection) SetPlatform(platform string)
- func (c *Connection) SetState(state State)
- func (c *Connection) SetUserID(userID int64)
- func (c *Connection) SetWSOpCode(opCode ws.OpCode)
- func (c *Connection) SetWSUpgraded(upgraded bool)
- func (c *Connection) State() State
- func (c *Connection) UpdateLastActive()
- func (c *Connection) UserID() int64
- func (c *Connection) WSOpCode() ws.OpCode
- func (c *Connection) Write(data []byte) error
- func (c *Connection) WritePacket(packet *protocol.Packet) error
- type Manager
- func (m *Manager) Add(conn *Connection) error
- func (m *Manager) BindUser(connID string, userID int64) error
- func (m *Manager) Broadcast(data []byte)
- func (m *Manager) BroadcastToUsers(userIDs []int64, data []byte)
- func (m *Manager) Close(ctx context.Context) error
- func (m *Manager) Count() int64
- func (m *Manager) Get(connID string) *Connection
- func (m *Manager) GetAllOnlineUserIDs() []int64
- func (m *Manager) GetByGnetConn(gnetConn gnet.Conn) *Connection
- func (m *Manager) GetByUserID(userID int64) *Connection
- func (m *Manager) GetOnlineUserCount() int
- func (m *Manager) GetUserConnCount(userID int64) int
- func (m *Manager) GetUserConns(userID int64) []*Connection
- func (m *Manager) IsClosed() bool
- func (m *Manager) IsUserOnline(userID int64) bool
- func (m *Manager) Range(fn func(*Connection) bool)
- func (m *Manager) Remove(connID string) *Connection
- func (m *Manager) Stats() map[string]int64
- func (m *Manager) UnbindUser(connID string)
- type State
Constants ¶
This section is empty.
Variables ¶
var ( ErrConnectionClosed = errors.New("connection closed") ErrConnectionNotFound = errors.New("connection not found") ErrUserNotOnline = errors.New("user not online") ErrMaxConnectionsReached = errors.New("max connections reached") )
错误定义
Functions ¶
Types ¶
type Bucket ¶
type Bucket struct {
// contains filtered or unexported fields
}
Bucket 连接桶 将连接分散到多个桶中,减少锁竞争
type BucketPool ¶
type BucketPool struct {
// contains filtered or unexported fields
}
BucketPool 连接桶池
func (*BucketPool) GetBucket ¶
func (p *BucketPool) GetBucket(connID string) *Bucket
GetBucket 获取连接所属的桶
func (*BucketPool) RangeParallel ¶
func (p *BucketPool) RangeParallel(fn func(*Connection))
RangeParallel 并行遍历所有连接
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
Connection 连接封装
func (*Connection) AuthenticatedAt ¶
func (c *Connection) AuthenticatedAt() time.Time
AuthenticatedAt 返回认证时间
func (*Connection) CloseWithReason ¶
func (c *Connection) CloseWithReason(ctx context.Context, reason string) error
CloseWithReason 带原因关闭连接
func (*Connection) GetMetadata ¶
func (c *Connection) GetMetadata(key string) (interface{}, bool)
GetMetadata 获取元数据
func (*Connection) IdleDuration ¶
func (c *Connection) IdleDuration() time.Duration
IdleDuration 返回空闲时长
func (*Connection) IsAuthenticated ¶
func (c *Connection) IsAuthenticated() bool
IsAuthenticated 判断是否已认证
func (*Connection) IsWSUpgraded ¶
func (c *Connection) IsWSUpgraded() bool
IsWSUpgraded 判断是否已完成 WebSocket 升级
func (*Connection) LastActiveAt ¶
func (c *Connection) LastActiveAt() time.Time
LastActiveAt 返回最后活跃时间
func (*Connection) SetAuthenticatedAt ¶
func (c *Connection) SetAuthenticatedAt(t time.Time)
SetAuthenticatedAt 设置认证时间
func (*Connection) SetDeviceID ¶
func (c *Connection) SetDeviceID(deviceID string)
SetDeviceID 设置设备ID
func (*Connection) SetMetadata ¶
func (c *Connection) SetMetadata(key string, value interface{})
SetMetadata 设置元数据
func (*Connection) SetOnClose ¶
func (c *Connection) SetOnClose(fn func(*Connection))
SetOnClose 设置关闭回调
func (*Connection) SetWSOpCode ¶
func (c *Connection) SetWSOpCode(opCode ws.OpCode)
SetWSOpCode 设置 WebSocket 操作码
func (*Connection) SetWSUpgraded ¶
func (c *Connection) SetWSUpgraded(upgraded bool)
SetWSUpgraded 设置 WebSocket 升级状态
func (*Connection) UpdateLastActive ¶
func (c *Connection) UpdateLastActive()
UpdateLastActive 更新最后活跃时间
func (*Connection) WritePacket ¶
func (c *Connection) WritePacket(packet *protocol.Packet) error
WritePacket 写入数据包
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager 连接管理器
func (*Manager) BroadcastToUsers ¶
BroadcastToUsers 广播消息给指定用户
func (*Manager) GetAllOnlineUserIDs ¶
GetAllOnlineUserIDs 获取所有在线用户ID
func (*Manager) GetByGnetConn ¶
func (m *Manager) GetByGnetConn(gnetConn gnet.Conn) *Connection
GetByGnetConn 通过 gnet.Conn 获取连接
func (*Manager) GetByUserID ¶
func (m *Manager) GetByUserID(userID int64) *Connection
GetByUserID 获取用户的第一个在线连接(单设备简化场景)
func (*Manager) GetOnlineUserCount ¶
GetOnlineUserCount 获取在线用户数
func (*Manager) GetUserConnCount ¶
GetUserConnCount 获取用户连接数
func (*Manager) GetUserConns ¶
func (m *Manager) GetUserConns(userID int64) []*Connection
GetUserConns 获取用户的所有连接
func (*Manager) IsUserOnline ¶
IsUserOnline 判断用户是否在线