idgenerator

package module
v0.0.0-...-7bfebd0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2025 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrIDPoolExhausted 表示ID池已经耗尽 (计数器超过了最大值)
	ErrIDPoolExhausted = errors.New("room ID pool is exhausted")
)

Functions

This section is empty.

Types

type GenerationStrategy

type GenerationStrategy int

GenerationStrategy 定义了ID的生成策略

const (
	// PreferReleased (默认策略): 优先从释放列表中获取ID,如果列表为空,再尝试生成新的ID。
	PreferReleased GenerationStrategy = iota
	// PreferNew: 优先尝试生成新的ID,只有在生成失败(例如,多次尝试后都冲突)时,才尝试从释放列表中获取ID作为备用方案。
	PreferNew
)

type IDGenerator

type IDGenerator interface {
	GetNextID() (int64, error)
	GetCurrentNextID() (int64, error)
}

IDGenerator 定义了 ID 生成器的接口

func NewRedisIDGenerator

func NewRedisIDGenerator(client redis.Cmdable, key string, initialValue int64) (IDGenerator, error)

NewRedisIDGenerator creates and returns a new RedisIDGenerator instance. It preloads the Lua script required for GetNextID operation. client: An initialized Redis commandable client (e.g., *redis.Client). key: The Redis key to be used for this specific counter. initialValue: The value from which the ID should start if the key is new.

type Option

type Option func(*RoomIDManager)

Option 是用于配置 RoomIDManager 的函数类型

func WithGenerationStrategy

func WithGenerationStrategy(strategy GenerationStrategy) Option

WithGenerationStrategy 是一个选项,用于设置ID生成策略。

func WithMaxReleasedIDsToStore

func WithMaxReleasedIDsToStore(max int64) Option

WithMaxReleasedIDsToStore 是一个选项,用于设置释放队列的最大长度。

func WithRandomGenerationAttempts

func WithRandomGenerationAttempts(attempts int) Option

WithRandomGenerationAttempts 是一个选项,用于设置随机生成ID时的尝试次数。

type RedisIDGenerator

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

RedisIDGenerator implements the IDGenerator interface using Redis as a backend.

func (*RedisIDGenerator) GetCurrentNextID

func (g *RedisIDGenerator) GetCurrentNextID() (int64, error)

GetCurrentNextID queries and returns the next ID that would be assigned by GetNextID. If the counter key doesn't exist in Redis, it returns the configured initialValue. Otherwise, it returns the current counter value incremented by one.

func (*RedisIDGenerator) GetNextID

func (g *RedisIDGenerator) GetNextID() (int64, error)

GetNextID retrieves the next unique ID from Redis. It uses a Lua script to ensure atomicity of initializing the counter (if new) and incrementing it.

type RoomIDManager

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

RoomIDManager 负责管理房间ID的分配和释放

func NewRoomIDManager

func NewRoomIDManager(client redis.Cmdable, baseKey string, opts ...Option) (*RoomIDManager, error)

NewRoomIDManager 创建一个新的 RoomIDManager 实例 client: Redis 客户端 baseKey: 用于区分不同类型房间号池的键 opts: 一系列可选配置,例如 WithGenerationStrategy(PreferNew)

func (*RoomIDManager) CountReleasedRoomIDs

func (m *RoomIDManager) CountReleasedRoomIDs(ctx context.Context) (int64, error)

CountReleasedRoomIDs returns the current number of IDs in the released queue. Useful for monitoring.

func (*RoomIDManager) CountUsedRoomIDs

func (m *RoomIDManager) CountUsedRoomIDs(ctx context.Context) (int64, error)

CountUsedRoomIDs returns the current number of used room IDs. Useful for monitoring.

func (*RoomIDManager) GetNextAvailableRoomID

func (m *RoomIDManager) GetNextAvailableRoomID(ctx context.Context) (int64, error)

GetNextAvailableRoomID 根据配置的策略获取下一个可用的房间号。

func (*RoomIDManager) LoadScripts

func (m *RoomIDManager) LoadScripts(ctx context.Context) error

LoadScripts loads Lua scripts into Redis.

func (*RoomIDManager) ReleaseRoomID

func (m *RoomIDManager) ReleaseRoomID(ctx context.Context, roomID int64) error

ReleaseRoomID 将一个房间号标记为未使用,并将其添加到待重用列表

type SequentialIDManager

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

SequentialIDManager 负责管理连续房间ID的分配和释放

func NewSequentialIDManager

func NewSequentialIDManager(client redis.Cmdable, baseKey string) (*SequentialIDManager, error)

NewSequentialIDManager 创建一个新的 SequentialIDManager 实例

func (*SequentialIDManager) GetNextID

func (m *SequentialIDManager) GetNextID(ctx context.Context) (int64, error)

GetNextID 获取下一个可用的房间号 (优先重用,否则连续递增)

func (*SequentialIDManager) ReleaseID

func (m *SequentialIDManager) ReleaseID(ctx context.Context, roomID int64) error

ReleaseID 释放一个房间号,使其可被重用

Jump to

Keyboard shortcuts

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