Documentation ¶
Overview ¶
Package olaf provides methods to generate unique ID using Twitter Snowflake algorithm. #author Thanh Nguyen <btnguyen2k@gmail.com> #version 0.1.0
Index ¶
- Constants
- func UnixMilliseconds() int64
- func WaitTillNextMillisec(currentMillisec int64) int64
- type Olaf
- func (o *Olaf) ExtractTime128(id128 *big.Int) time.Time
- func (o *Olaf) ExtractTime128Ascii(id128Ascii string) time.Time
- func (o *Olaf) ExtractTime128Hex(id128Hex string) time.Time
- func (o *Olaf) ExtractTime64(id64 uint64) time.Time
- func (o *Olaf) ExtractTime64Ascii(id64Ascii string) time.Time
- func (o *Olaf) ExtractTime64Hex(id64Hex string) time.Time
- func (o *Olaf) Id128() *big.Int
- func (o *Olaf) Id128Ascii() string
- func (o *Olaf) Id128Hex() string
- func (o *Olaf) Id64() uint64
- func (o *Olaf) Id64Ascii() string
- func (o *Olaf) Id64Hex() string
Constants ¶
const Epoch int64 = 1546300800000
Epoch is set to 2019-01-01 00:00:00 UTC. You may customize this to set a different epoch for your application.
Variables ¶
This section is empty.
Functions ¶
func UnixMilliseconds ¶
func UnixMilliseconds() int64
UnixMilliseconds returns current UNIX timestamp in milliseconds.
func WaitTillNextMillisec ¶
WaitTillNextMillisec waits till clock moves to the next millisecond. Returns the "next" millisecond.
Types ¶
type Olaf ¶
type Olaf struct { NodeID int64 // original node-id Epoch int64 // Twitter snowflake's epoch SequenceId int64 // Twitter snowflake's sequence-id Timestamp int64 // last 'touch' UNIX timestamp in milliseconds // contains filtered or unexported fields }
Olaf wraps configurations for Twitter Snowflake IDs.
func NewOlafWithEpoch ¶
NewOlafWithEpoch creates a new Olaf with custom epoch.
func (*Olaf) ExtractTime128 ¶
ExtractTime128 extracts time metadata from a 128-bit id.
func (*Olaf) ExtractTime128Ascii ¶
ExtractTime128Ascii extracts time metadata from a 128-bit id in ascii (base 36) format.
func (*Olaf) ExtractTime128Hex ¶
ExtractTime128Hex extracts time metadata from a 128-bit id in hex (base 16) format.
func (*Olaf) ExtractTime64 ¶
ExtractTime64 extracts time metadata from a 64-bit id.
func (*Olaf) ExtractTime64Ascii ¶
ExtractTime64Ascii extracts time metadata from a 64-bit id in ascii (base 36) format.
func (*Olaf) ExtractTime64Hex ¶
ExtractTime64Hex extracts time metadata from a 64-bit id in hex (base 16) format.
func (*Olaf) Id128Ascii ¶
Id128Ascii generates a 128-bit id as an ascii (base 36) string.