Documentation
ΒΆ
Index ΒΆ
- Constants
- func Define(scene string, options Options) bool
- func Play(count int)
- func Rand(w byte) int64
- func Register(name string, d DataProvider)
- func Simple(server int64) (func() int64, error)
- type Base64
- type Bits
- func Arg(width byte, index int, fallback int64) Bits
- func Data(width byte, source string, fallback int64, query ...interface{}) Bits
- func Env(width byte, name string, fallback int64) Bits
- func Fixed(width byte, value int64) Bits
- func Host(width byte, fallback int64) Bits
- func Node(width byte, fallback int64) Bits
- func Option(width byte, key string, fallback int64) Bits
- func Random(width byte) Bits
- func Sequence(width byte) Bits
- func Timestamp(width byte, t DateTimeType) Bits
- type Builder
- type DataProvider
- type DataSourceType
- type DateTimeType
- type DebugInfo
- type DecodeError
- type Encoder
- type ID
- type Options
- type OptionsError
Constants ΒΆ
const ( DecodeErrorEmpty decodeErrorType = iota DecodeErrorInvalidDigit DecodeErrorOverflow DecodeErrorOutOfRange )
const ( // SegmentsLimit is the maximum number of segments SegmentsLimit = 63 // EpochReservedDays indicates the minimum days approaching the end EpochReservedDays = 7 // EpochMS is the default start timestamp, // measured in milliseconds starting // at midnight on December 12, 2022 EpochMS = 1_670_774_400_000 )
const ( // HostWidth is the default width of the bit-segment, // value range [0, 63] HostWidth = 6 // NodeWidth is the default width of the bit-segment, // value range [0, 15] NodeWidth = 4 // TimestampWidth is the default width of the bit-segment. // It measures time by the number of seconds that have // elapsed since EpochMS, value range [0, 68 years after] TimestampWidth = 41 // SequenceWidth is the default width of the bit-segment, // value range [0, 4095] SequenceWidth = 12 )
const ( // EnvServerHost is data center id, type: byte, value range [0, 31], 6 bits EnvServerHost = "SERVER_HOST_ID" // EnvServerNode is server node id, type: byte, value range [0, 15], 4 bits EnvServerNode = "SERVER_NODE_ID" // EnvDomainId is geo region id, type: int32, value range [0, 65535], 16 bits EnvDomainId = "SERVER_DOMAIN_ID" // EnvTimeEpoch is server epoch timestamp, type: int64, [0, 9_223_372_036_854_775_807] EnvTimeEpoch = "SERVER_EPOCH_TIMESTAMP" )
Variables ΒΆ
This section is empty.
Functions ΒΆ
func Rand ΒΆ
Rand generates a secure random number with a width specified by w, which is the expected bit width, value range is [1, 63].
Types ΒΆ
type Bits ΒΆ
type Bits struct { // Source indicates that bit-segment data source Source DataSourceType Width byte Value int64 // Key indicates the data source key Key string // Index indicates the data source index Index int // contains filtered or unexported fields }
func Timestamp ΒΆ
func Timestamp(width byte, t DateTimeType) Bits
Timestamp to make a bit-segment, which value from system unix timestamp
type Builder ΒΆ
type Builder struct { sync.Mutex Encoder Encoder Debug bool // contains filtered or unexported fields }
func Snowflake ΒΆ
Snowflake implements the common snowflake algorithm. The value range of host is [0, 63]. The value range of node is [0, 15].
func (*Builder) NextString ΒΆ
NextString returns the next ID as a string.
func (*Builder) ResetEpoch ΒΆ
ResetEpoch resets the epoch.
type DataProvider ΒΆ
type DataSourceType ΒΆ
type DataSourceType int
const ( // Static indicates that the value is from default Static DataSourceType = iota // Args indicates that the value is from arguments of caller Args // OS indicates that the value is from OS environment OS // Settings indicates that the value is from options Settings // SequenceID indicates that the value is from sequence value SequenceID // DateTime indicates that the value is from system unix timestamp in nanoseconds DateTime // RandomID indicates that the value is from a random number RandomID // Provider indicates that the value is from data provider Provider )
func (DataSourceType) String ΒΆ
func (d DataSourceType) String() string
type DateTimeType ΒΆ
type DateTimeType int
const ( TimestampMilliseconds DateTimeType = iota TimestampNanoseconds TimestampMicroseconds TimestampSeconds TimeNanosecond TimeMicrosecond TimeMillisecond TimeSecond TimeMinute TimeHour TimeDay TimeMonth TimeYear TimeYearDay TimeWeekday TimeWeekNumber )
func (DateTimeType) String ΒΆ
func (d DateTimeType) String() string
type DecodeError ΒΆ
func (*DecodeError) Error ΒΆ
func (e *DecodeError) Error() string
func (*DecodeError) Unwrap ΒΆ
func (e *DecodeError) Unwrap() error
type Options ΒΆ
type Options struct { // ReservedDays indicates the minimum days approaching the end ReservedDays, EpochMS int64 // Signed is used to on/off the sign bit Signed bool // contains filtered or unexported fields }
Options MUST include DateTime segment AND SequenceID segment
func Config ΒΆ
Config is a shortcut for make Options, segments MUST include DateTime segment AND SequenceID segment.
func Default ΒΆ
func Default() Options
Default is a shortcut for make Options, which is the classic snowflake algorithm
func Predefined ΒΆ
Predefined obtains the predefined options specified by scope(case-insensitive), which includes "Default"(aliases: classic, snowflake), "Random"(aliases: shuffle), "OpenId", "SequenceId"(aliases: seq, seqid, increment, auto-increment), "Test"(aliases: testing) ... etc
func Shuffle ΒΆ
func Shuffle() Options
Shuffle return predefined options "shuffle"(alias: random), 126 bits
type OptionsError ΒΆ
func (*OptionsError) Error ΒΆ
func (e *OptionsError) Error() string
func (*OptionsError) SameAs ΒΆ
func (e *OptionsError) SameAs(err error) bool
func (*OptionsError) Unwrap ΒΆ
func (e *OptionsError) Unwrap() error