Documentation ¶
Overview ¶
Package monoton is a highly scalable, single/multi node, human-readable, predictable and incremental unique id generator
Time Ordered ¶
The monoton package provides sequences based on the monotonic time which represents the absolute elapsed wall-clock time since some arbitrary, fixed point in the past. It isn't affected by changes in the system time-of-day clock.
Initial Time ¶
Initial time value opens space for the time value by subtracting the given value from the time sequence.
Readable ¶
The monoton package converts all sequences into Base62 format. And Base62 only uses ASCII alpha-numeric chars to represent data which makes it easy to read, predict the order by a human eye.
The total byte size is fixed to 16 bytes for all sequencers. And at least one byte is reserved to nodes.
Multi Node Support ¶
The monoton package can be used on single/multiple nodes without the need for machine coordination. It uses configured node identifier to generate ids by attaching the node identifier to the end of the sequences.
Extendable ¶
The package comes with three pre-configured sequencers and Sequencer interface to allow new sequencers.
Included Sequencers and Byte Orderings ¶
The monoton package currently comes with Nanosecond, Millisecond and Second sequencers. And it uses Millisecond sequencer by default. For each sequencer, the byte orders are as following:
Second: 16 B => 6 B (seconds) + 6 B (counter) + 4 B (node) Millisecond: 16 B => 8 B (milliseconds) + 4 B (counter) + 4 B (node) Nanosecond: 16 B => 11 B (nanoseconds) + 2 B (counter) + 3 B (node)
New Sequencers ¶
The sequencers can be extended for any other time format, sequence format by implementing the monoton/sequncer.Sequencer interface.
Example using Singleton
package uniqid // Import packages import ( "fmt" "github.com/mustafaturan/monoton/v3" "github.com/mustafaturan/monoton/v3/sequencer" ) const year2020asMillisecondPST = 1577865600000 var m monoton.Monoton // On init configure the monoton func init() { m = newIDGenerator() } func newIDGenerator() monoton.Monoton { // Fetch your node id from a config server or generate from MAC/IP // address node := uint64(1) // A unix time value which will be subtracted from the time sequence // value. The initialTime value type corresponds to the sequencer type's // time representation. If you are using Millisecond sequencer then it // must be considered as Millisecond initialTime := uint64(year2020asMillisecondPST) // Configure monoton with a sequencer and the node m, err = monoton.New(sequencer.NewMillisecond(), node, initialTime) if err != nil{ panic(err) } return m } func Generate() string { m.Next() } // In any other package unique ids can be generated like below: package main import ( "fmt" "uniqid" // your local `uniqid` package inside your project ) func main() { for i := 0; i < 100; i++ { fmt.Println(uniqid.Generate()) } }
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MaxByteSizeError ¶
MaxByteSizeError is an error type with sequence & time byte sizes
func (*MaxByteSizeError) Error ¶
func (e *MaxByteSizeError) Error() string
type MaxNodeCapacityExceededError ¶
MaxNodeCapacityExceededError is an error type with node information
func (*MaxNodeCapacityExceededError) Error ¶
func (e *MaxNodeCapacityExceededError) Error() string
type Monoton ¶
type Monoton struct {
// contains filtered or unexported fields
}
Monoton is a sequential id generator
func New ¶
New inits a new monoton ID generator with the given generator and node.
Example ¶
s := sequencer.NewMillisecond() // has 4 bytes free space for a node n := uint64(19) // Base62 => J t := uint64(0) // initial time (start from unix time in ms) m, err := monoton.New(s, n, t) if err != nil { panic(err) } fmt.Println(m.Next()[12:])
Output: 000J
func (Monoton) Next ¶
Next generates next incremental unique identifier as Base62 The execution returns the following Bytes (B) for the known sequencer types:
Second: 16 B => 6 B (seconds) + 6 B (counter) + 4 B (node) Millisecond: 16 B => 8 B (milliseconds) + 4 B (counter) + 4 B (node) Nanosecond: 16 B => 11 B (nanoseconds) + 2 B (counter) + 3 B (node)
For byte size decisions please refer to docs/adrs/byte-sizes.md
Example ¶
s := sequencer.NewSecond() // sequencer.Second n := uint64(19) // Base62 => J t := uint64(time.Now().Unix()) // initial time (start from unix time in s) m, err := monoton.New(s, n, t) if err != nil { panic(err) } fmt.Println(len(m.Next()))
Output: 16
func (Monoton) NextBytes ¶
NextBytes generates next incremental unique identifier as Base62 16 bytes array The execution returns the following Bytes (B) for the known sequencer types:
Second: 16 B => 6 B (seconds) + 6 B (counter) + 4 B (node) Millisecond: 16 B => 8 B (milliseconds) + 4 B (counter) + 4 B (node) Nanosecond: 16 B => 11 B (nanoseconds) + 2 B (counter) + 3 B (node)
For byte size decisions please refer to docs/adrs/byte-sizes.md
Directories ¶
Path | Synopsis |
---|---|
Package encoder provides encoding functionality for Base10 to Base62 conversion with/without paddings
|
Package encoder provides encoding functionality for Base10 to Base62 conversion with/without paddings |
Package mtimer returns the current monotonic time in nanoseconds
|
Package mtimer returns the current monotonic time in nanoseconds |
Package sequencer provides sequences based on monotonic time Time The sequncer package provides sequences based on the monotonic time which represents the absolute elapsed wall-clock time since some arbitrary, fixed point in the past.
|
Package sequencer provides sequences based on monotonic time Time The sequncer package provides sequences based on the monotonic time which represents the absolute elapsed wall-clock time since some arbitrary, fixed point in the past. |