session

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Kind

type Kind int
const (
	Conn Kind = iota + 1 // 连接SESSION
	User                 // 用户SESSION
)

func (Kind) IsValid added in v1.0.10

func (k Kind) IsValid() bool

IsValid 检查 Kind 是否有效

func (Kind) String

func (k Kind) String() string

type Option added in v1.0.9

type Option func(*Session)

Option 配置选项

func WithShardCount added in v1.0.9

func WithShardCount(count int) Option

WithShardCount 设置分片数量

type Session

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

Session 会话管理器(泛型 ShardMap 分片锁设计) 使用 ShardMap[K,V] 统一分片存储,减少锁竞争

func NewSession

func NewSession(opts ...Option) *Session

NewSession 创建会话管理器(内部使用泛型 ShardMap)

func (*Session) AddConn

func (s *Session) AddConn(conn network.Conn)

AddConn 添加连接

func (*Session) Bind

func (s *Session) Bind(cid, uid int64) error

Bind 绑定用户ID

func (*Session) Broadcast

func (s *Session) Broadcast(kind Kind, message []byte) (n int64, err error)

Broadcast 推送广播消息(异步)

func (*Session) Close

func (s *Session) Close(kind Kind, target int64, force ...bool) error

Close 关闭会话

func (*Session) Has

func (s *Session) Has(kind Kind, target int64) (ok bool, err error)

Has 是否存在会话

func (*Session) LocalAddr

func (s *Session) LocalAddr(kind Kind, target int64) (net.Addr, error)

LocalAddr 获取本地地址

func (*Session) LocalIP

func (s *Session) LocalIP(kind Kind, target int64) (string, error)

LocalIP 获取本地IP

func (*Session) Multicast

func (s *Session) Multicast(kind Kind, targets []int64, message []byte) (n int64, err error)

Multicast 推送组播消息(异步)

func (*Session) Publish

func (s *Session) Publish(channel string, message []byte) (n int64)

Publish 发布频道消息(异步)

func (*Session) Push

func (s *Session) Push(kind Kind, target int64, message []byte) error

Push 推送消息(异步)

func (*Session) RemConn

func (s *Session) RemConn(conn network.Conn)

RemConn 移除连接

func (*Session) RemoteAddr

func (s *Session) RemoteAddr(kind Kind, target int64) (net.Addr, error)

RemoteAddr 获取远端地址

func (*Session) RemoteIP

func (s *Session) RemoteIP(kind Kind, target int64) (string, error)

RemoteIP 获取远端IP

func (*Session) Send

func (s *Session) Send(kind Kind, target int64, message []byte) error

Send 发送消息(同步)

func (*Session) Stat

func (s *Session) Stat(kind Kind) (int64, error)

Stat 统计会话总数

func (*Session) Subscribe

func (s *Session) Subscribe(kind Kind, targets []int64, channel string) (err error)

Subscribe 订阅频道

func (*Session) Unbind

func (s *Session) Unbind(uid int64) (int64, error)

Unbind 解绑用户ID

func (*Session) Unsubscribe

func (s *Session) Unsubscribe(kind Kind, targets []int64, channel string) (err error)

Unsubscribe 取消订阅频道

type ShardMap added in v1.0.10

type ShardMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

ShardMap 通用分片锁 Map,减少锁竞争 使用泛型替代重复的 connShard/userShard/channelShard 实现

func NewInt64ShardMap added in v1.0.10

func NewInt64ShardMap[V any](shardCount int) *ShardMap[int64, V]

NewInt64ShardMap 创建 int64 键的分片锁 Map(常用场景的便捷构造函数)

func NewShardMap added in v1.0.10

func NewShardMap[K comparable, V any](shardCount int, hasher func(K) uint32) *ShardMap[K, V]

NewShardMap 创建分片锁 Map

func NewStringShardMap added in v1.0.10

func NewStringShardMap[V any](shardCount int) *ShardMap[string, V]

NewStringShardMap 创建 string 键的分片锁 Map(常用场景的便捷构造函数)

func (*ShardMap[K, V]) Delete added in v1.0.10

func (m *ShardMap[K, V]) Delete(key K)

Delete 删除值

func (*ShardMap[K, V]) Get added in v1.0.10

func (m *ShardMap[K, V]) Get(key K) (V, bool)

Get 获取值

func (*ShardMap[K, V]) GetAndDelete added in v1.0.10

func (m *ShardMap[K, V]) GetAndDelete(key K) (V, bool)

GetAndDelete 获取并删除值(原子操作)

func (*ShardMap[K, V]) GetOrSet added in v1.0.10

func (m *ShardMap[K, V]) GetOrSet(key K, value V) (V, bool)

GetOrSet 获取或设置值(如果不存在则设置,返回最终值和是否已存在)

func (*ShardMap[K, V]) Has added in v1.0.10

func (m *ShardMap[K, V]) Has(key K) bool

Has 检查键是否存在

func (*ShardMap[K, V]) Len added in v1.0.10

func (m *ShardMap[K, V]) Len() int64

Len 获取所有分片的总元素数

func (*ShardMap[K, V]) RangeAll added in v1.0.10

func (m *ShardMap[K, V]) RangeAll(fn func(key K, value V) bool)

RangeAll 遍历所有分片中的所有元素 fn 返回 false 时停止遍历

func (*ShardMap[K, V]) Set added in v1.0.10

func (m *ShardMap[K, V]) Set(key K, value V)

Set 设置值

func (*ShardMap[K, V]) WithShard added in v1.0.10

func (m *ShardMap[K, V]) WithShard(key K, fn func(items map[K]V))

WithShard 在指定键的分片锁保护下执行操作(用于复杂的原子操作)

func (*ShardMap[K, V]) WithShardRLock added in v1.0.10

func (m *ShardMap[K, V]) WithShardRLock(key K, fn func(items map[K]V))

WithShardRLock 在指定键的分片读锁保护下执行操作

Jump to

Keyboard shortcuts

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