random

package
v0.21.6 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2021 License: MIT Imports: 11 Imported by: 2

Documentation

Overview

Package random 雪花算法修改版 0 - 毫秒时间戳(41 bit) - 序列号(12 bit) - 机器 id(10 bit) 0 - 69年 - 4000(每秒 1000 * 4000 个 uuid) - 1000

时间回拨处理: 1 当相差 15 ms 之内时,等待时间追上 2 当超过 15 ms 时,直接替换机器 id

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSnowflakeWorkerID 无效的 workerID,取值范围 [0, defaultMaxWorkerID]
	ErrSnowflakeWorkerID = errors.New("bad worker id")

	// ErrSnowflakeTimeBackward 时间倒退,当前时间比上一次记录的时间还要小
	ErrSnowflakeTimeBackward = errors.New("time backward")
)

Functions

func Int

func Int(min, max int64) int64

Int 获取指定范围内的整数

func Lower added in v0.6.2

func Lower(length int) string

Lower 获取指定长度的字符串,仅包含小写字母

func NewUUID

func NewUUID() string

NewUUID 全局唯一标识符,12byte

基于时间戳,主机信息,进程 ID 生成,每次调用值加 1,具有原子性

eg: NewUUID() -> 5cb840f90a5dcd71e779ba64

eg: NewUUID() -> 5cb840f90a5dcd71e779ba65

func SetSnowflakeTestTimebackward

func SetSnowflakeTestTimebackward(able bool)

SetSnowflakeTestTimebackward 测试时间回退

func String

func String(length int) string

String 获取指定长度的字符串,包括大小写字母和数字

func Uint32

func Uint32() uint32

Uint32 获取随机数,类型为 uint32

func Upper added in v0.6.2

func Upper(length int) string

Upper 获取指定长度的字符串,仅包含大写字母

Types

type Snowflake

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

Snowflake uuid 生成器

func NewBit46Snowflake added in v0.19.6

func NewBit46Snowflake(workerID int) (*Snowflake, error)

NewBit46Snowflake 生成46 bit 的 uint64 workerId: 1-8

func NewSnowflake

func NewSnowflake(workerID int) (*Snowflake, error)

NewSnowflake 创建生成器

func NewSnowflakeBy

func NewSnowflakeBy(workerID int, originTime int64, workerIDBits int, sequenceBits int, nextWorkIDFunc SnowflakeNextWorkIDFunc, backWorkIDFunc SnowflakeBackWorkIDFunc) (*Snowflake, error)

NewSnowflakeBy 创建生成器

func (*Snowflake) SetOriginTime

func (snowflake *Snowflake) SetOriginTime(originTime int64)

SetOriginTime 设置起始时间

func (*Snowflake) SetWorkIDFunc

func (snowflake *Snowflake) SetWorkIDFunc(nextWorkIDFunc SnowflakeNextWorkIDFunc, backWorkIDFunc SnowflakeBackWorkIDFunc)

SetWorkIDFunc 设置时间回拨超过 15 ms 后,用于处理 workID 的函数

func (*Snowflake) UUID

func (snowflake *Snowflake) UUID() (uint64, error)

UUID 获取 uuid,线程安全

func (*Snowflake) UnsafeUUID

func (snowflake *Snowflake) UnsafeUUID() (uint64, error)

UnsafeUUID 获取 uuid,非线程安全

type SnowflakeBackWorkIDFunc added in v0.19.6

type SnowflakeBackWorkIDFunc func(int) error

SnowflakeBackWorkIDFunc ..

type SnowflakeNextWorkIDFunc added in v0.19.6

type SnowflakeNextWorkIDFunc func() (int, error)

SnowflakeNextWorkIDFunc ..

Jump to

Keyboard shortcuts

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