connection

package
v0.0.0-...-23f8949 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package connection 提供连接分桶管理 通过分桶减少锁竞争,提升并发性能

Package connection 提供连接管理功能 包括连接封装、连接池和分桶管理

Package connection 提供连接管理器 负责连接的生命周期管理和查询

Index

Constants

This section is empty.

Variables

View Source
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

func GenerateConnID

func GenerateConnID() string

GenerateConnID 生成连接ID

Types

type Bucket

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

Bucket 连接桶 将连接分散到多个桶中,减少锁竞争

func NewBucket

func NewBucket() *Bucket

NewBucket 创建新的连接桶

func (*Bucket) Add

func (b *Bucket) Add(conn *Connection)

Add 添加连接

func (*Bucket) Count

func (b *Bucket) Count() int

Count 返回连接数量

func (*Bucket) Get

func (b *Bucket) Get(connID string) *Connection

Get 获取连接

func (*Bucket) Range

func (b *Bucket) Range(fn func(*Connection) bool)

Range 遍历所有连接

func (*Bucket) Remove

func (b *Bucket) Remove(connID string) *Connection

Remove 移除连接

type BucketPool

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

BucketPool 连接桶池

func NewBucketPool

func NewBucketPool(bucketNum int) *BucketPool

NewBucketPool 创建连接桶池

func (*BucketPool) Add

func (p *BucketPool) Add(conn *Connection)

Add 添加连接

func (*BucketPool) Count

func (p *BucketPool) Count() int

Count 返回总连接数

func (*BucketPool) Get

func (p *BucketPool) Get(connID string) *Connection

Get 获取连接

func (*BucketPool) GetBucket

func (p *BucketPool) GetBucket(connID string) *Bucket

GetBucket 获取连接所属的桶

func (*BucketPool) Range

func (p *BucketPool) Range(fn func(*Connection) bool)

Range 遍历所有连接

func (*BucketPool) RangeParallel

func (p *BucketPool) RangeParallel(fn func(*Connection))

RangeParallel 并行遍历所有连接

func (*BucketPool) Remove

func (p *BucketPool) Remove(connID string) *Connection

Remove 移除连接

type Connection

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

Connection 连接封装

func NewConnection

func NewConnection(id string, conn gnet.Conn) *Connection

NewConnection 创建新连接

func (*Connection) AuthenticatedAt

func (c *Connection) AuthenticatedAt() time.Time

AuthenticatedAt 返回认证时间

func (*Connection) Close

func (c *Connection) Close() error

Close 关闭连接

func (*Connection) CloseWithReason

func (c *Connection) CloseWithReason(ctx context.Context, reason string) error

CloseWithReason 带原因关闭连接

func (*Connection) Codec

func (c *Connection) Codec() *protocol.Codec

Codec 返回编解码器

func (*Connection) ConnectedAt

func (c *Connection) ConnectedAt() time.Time

ConnectedAt 返回连接建立时间

func (*Connection) DeviceID

func (c *Connection) DeviceID() string

DeviceID 返回设备ID

func (*Connection) GetMetadata

func (c *Connection) GetMetadata(key string) (interface{}, bool)

GetMetadata 获取元数据

func (*Connection) GnetConn

func (c *Connection) GnetConn() gnet.Conn

GnetConn 返回 gnet 连接

func (*Connection) ID

func (c *Connection) ID() string

ID 返回连接ID

func (*Connection) IdleDuration

func (c *Connection) IdleDuration() time.Duration

IdleDuration 返回空闲时长

func (*Connection) IsAuthenticated

func (c *Connection) IsAuthenticated() bool

IsAuthenticated 判断是否已认证

func (*Connection) IsConnected

func (c *Connection) IsConnected() bool

IsConnected 判断是否已连接

func (*Connection) IsWSUpgraded

func (c *Connection) IsWSUpgraded() bool

IsWSUpgraded 判断是否已完成 WebSocket 升级

func (*Connection) LastActiveAt

func (c *Connection) LastActiveAt() time.Time

LastActiveAt 返回最后活跃时间

func (*Connection) LocalAddr

func (c *Connection) LocalAddr() string

LocalAddr 返回本地地址

func (*Connection) Platform

func (c *Connection) Platform() string

Platform 返回平台

func (*Connection) RemoteAddr

func (c *Connection) RemoteAddr() string

RemoteAddr 返回远程地址

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) SetPlatform

func (c *Connection) SetPlatform(platform string)

SetPlatform 设置平台

func (*Connection) SetState

func (c *Connection) SetState(state State)

SetState 设置连接状态

func (*Connection) SetUserID

func (c *Connection) SetUserID(userID int64)

SetUserID 设置用户ID

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) State

func (c *Connection) State() State

State 返回当前状态

func (*Connection) UpdateLastActive

func (c *Connection) UpdateLastActive()

UpdateLastActive 更新最后活跃时间

func (*Connection) UserID

func (c *Connection) UserID() int64

UserID 返回用户ID

func (*Connection) WSOpCode

func (c *Connection) WSOpCode() ws.OpCode

WSOpCode 返回 WebSocket 操作码

func (*Connection) Write

func (c *Connection) Write(data []byte) error

Write 写入数据

func (*Connection) WritePacket

func (c *Connection) WritePacket(packet *protocol.Packet) error

WritePacket 写入数据包

type Manager

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

Manager 连接管理器

func NewManager

func NewManager(bucketNum int, maxConns int64) *Manager

NewManager 创建连接管理器

func (*Manager) Add

func (m *Manager) Add(conn *Connection) error

Add 添加新连接

func (*Manager) BindUser

func (m *Manager) BindUser(connID string, userID int64) error

BindUser 绑定用户到连接(认证成功后调用)

func (*Manager) Broadcast

func (m *Manager) Broadcast(data []byte)

Broadcast 广播消息给所有连接

func (*Manager) BroadcastToUsers

func (m *Manager) BroadcastToUsers(userIDs []int64, data []byte)

BroadcastToUsers 广播消息给指定用户

func (*Manager) Close

func (m *Manager) Close(ctx context.Context) error

Close 关闭管理器,断开所有连接

func (*Manager) Count

func (m *Manager) Count() int64

Count 返回当前连接数

func (*Manager) Get

func (m *Manager) Get(connID string) *Connection

Get 获取连接

func (*Manager) GetAllOnlineUserIDs

func (m *Manager) GetAllOnlineUserIDs() []int64

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

func (m *Manager) GetOnlineUserCount() int

GetOnlineUserCount 获取在线用户数

func (*Manager) GetUserConnCount

func (m *Manager) GetUserConnCount(userID int64) int

GetUserConnCount 获取用户连接数

func (*Manager) GetUserConns

func (m *Manager) GetUserConns(userID int64) []*Connection

GetUserConns 获取用户的所有连接

func (*Manager) IsClosed

func (m *Manager) IsClosed() bool

IsClosed 判断管理器是否已关闭

func (*Manager) IsUserOnline

func (m *Manager) IsUserOnline(userID int64) bool

IsUserOnline 判断用户是否在线

func (*Manager) Range

func (m *Manager) Range(fn func(*Connection) bool)

Range 遍历所有连接

func (*Manager) Remove

func (m *Manager) Remove(connID string) *Connection

Remove 移除连接

func (*Manager) Stats

func (m *Manager) Stats() map[string]int64

Stats 返回统计信息

func (*Manager) UnbindUser

func (m *Manager) UnbindUser(connID string)

UnbindUser 解绑用户(连接关闭时调用)

type State

type State int32

State 连接状态

const (
	StateConnecting State = iota // 连接中(握手前)
	StateConnected               // 已连接(握手成功)
	StateClosing                 // 关闭中
	StateClosed                  // 已关闭
)

Jump to

Keyboard shortcuts

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