Giter Site home page Giter Site logo

flaki's Introduction

Flaki - Das kleine Generator Build Status Coverage Status GoDoc Go Report Card

Flaki is an unique ID generator inspired by Snowflake. It generates 64-bit unique IDs of type uint64 or string. The string IDs are simply the uint64 IDs represented as a string. Each ID is composed of

42-bit timestamp 2-bit node ID 5-bit component ID 15-bit sequence

The node ID and component ID are configured during the creation of the Flaki generator and do not change afterwards. The 42-bit timestamp is the number of millisecond elapsed from the start epoch. If several IDs are requested during the same millisecond, the sequence is incremented to obtain a unique ID every time. There is a mechanism that does not let the sequence overflow. If it happen, we wait till the next millisecond to return new IDs. This ensure the IDs uniqueness.

Usage

Create a new Flaki generator.

var flaki, err = New()
if err != nil {
    // handle error
}

You can configure the Flaki's node ID, component ID and start epoch by submitting options to the call to New. New takes a variable number of options as parameter. If no option is given, the following default parameters are used:

  • 0 for the node ID
  • 0 for the component ID
  • 01.01.2017 for the epoch

If you want to modify any of the default parameter, use the corresponding option in the call to New.

  • ComponentID(uint64)
  • NodeID(uint64)
  • StartEpoch(time.Time)
var cID uint64 = ...
var nID uint64 = ..
var e time.Time = ...

var flaki, err = New(ComponentID(cID), NodeID(nID), StartEpoch(e))
if err != nil {
    // handle error
}

To obtain IDs, flaki provides two methods: NextID() (uint64, error), NextIDString() (string, error), NextValidID() uint64 and NextValidIDString() string.

var id uint64
var err error

id, err = flaki.NextID()

id = flaki.NextValidID()

var idStr string

idStr, err = flaki.NextIDString()

idStr = flaki.NextValidIDString()

NextID returns either a unique ID or an error if the clock moves backward.

Unlike NextID, NexValidID always returns a valid ID, never an error. If the clock moves backward, it wait until the situation goes back to normal before returning new IDs.

Limitations

Flaki won't generate valid IDs after the year 2262. This is due to the fact that the UnixNano function of the package time returns undefined result if the Unix time in nanoseconds cannot be represented by an int64, i.e. a date before the year 1678 or after 2262.

flaki's People

Contributors

johandroz avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.