monoton

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2019 License: Apache-2.0 Imports: 5 Imported by: 6

README

Monoton

Build Status Coverage Status Go Report Card

Highly scalable, single/multi node, predictable and incremental unique id generator.

Installation

Via go packages: go get github.com/mustafaturan/monoton

Usage

package main

// Import packages
import (
	"fmt"
	"github.com/mustafaturan/monoton"
	"github.com/mustafaturan/monoton/sequencer"
)

// On init configure the monoton
func init() {
	// Fetch your node id from a config server or generate from MAC/IP address
	node := uint(1)
	monoton.Configure(sequencer.NewMillisecond(), node)
}

func main() {
	for i := 0; i < 100; i++ {
		fmt.Println(monoton.Next())
	}
}
New Sequencers

The monoton package currently comes with Nanosecond, Millisecond and Second sequencers. And it uses Millisecond sequencer by default. But, for sure depending on needs, it can be extended for Microsecond easily by implementing the monoton.Sequencer interface.

Contributing

All contributors should follow Contributing Guidelines and ADR docs before creating pull requests.

Credits

Mustafa Turan

License

Apache License 2.0

Copyright (c) 2019 Mustafa Turan

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package monoton is a highly scalable, single/multi node, human-readable, predictable and incremental unique id generator

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Configure

func Configure(s sequencer.Sequencer, node uint) error

Configure configures the monoton with the given generator and node. If you need to reset the node, then you have to reconfigure. If you do not configure the node then the node will be set to zero value.

Example
package main

import (
	"fmt"

	"github.com/mustafaturan/monoton"
	"github.com/mustafaturan/monoton/sequencer"
)

func main() {
	s := sequencer.NewMillisecond() // has 4 bytes free space for a node
	n := uint(19)                   // Base62 => J

	monoton.Configure(s, n)
	fmt.Println(monoton.Next()[12:])
}
Output:
000J

func Next

func Next() string

Next generates next incremental unique identifier as Base62 The execution will return 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
package main

import (
	"fmt"

	"github.com/mustafaturan/monoton"
	"github.com/mustafaturan/monoton/sequencer"
)

func main() {
	s := sequencer.NewMillisecond()
	n := uint(19)

	monoton.Configure(s, n)
	fmt.Println(len(monoton.Next()))
}
Output:
16

Types

This section is empty.

Directories

Path Synopsis
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.

Jump to

Keyboard shortcuts

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