reddit/snooflake

Name: snooflake

Owner: Reddit

Description: A distributed unique ID generator inspired by Twitter's Snowflake

Forked from: sony/sonyflake

Created: 2017-09-14 18:21:09.0

Updated: 2018-02-15 14:57:08.0

Pushed: 2017-09-15 17:25:49.0

Homepage:

Size: 31

Language: Go

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Sonyflake

GoDoc Build Status Coverage Status Go Report Card

Sonyflake is a distributed unique ID generator inspired by Twitter's Snowflake.
A Sonyflake ID is composed of

39 bits for time in units of 10 msec
 8 bits for a sequence number
16 bits for a machine id
Installation
et github.com/sony/sonyflake
Usage

The function NewSonyflake creates a new Sonyflake instance.

 NewSonyflake(st Settings) *Sonyflake

You can configure Sonyflake by the struct Settings:

 Settings struct {
StartTime      time.Time
MachineID      func() (uint16, error)
CheckMachineID func(uint16) bool

In order to get a new unique ID, you just have to call the method NextID.

 (sf *Sonyflake) NextID() (uint64, error)

NextID can continue to generate IDs for about 174 years from StartTime. But after the Sonyflake time is over the limit, NextID returns an error.

AWS VPC and Docker

The awsutil package provides the function AmazonEC2MachineID that returns the lower 16-bit private IP address of the Amazon EC2 instance. It also works correctly on Docker by retrieving instance metadata.

AWS VPC is assigned a single CIDR with a netmask between /28 and /16. So if each EC2 instance has a unique private IP address in AWS VPC, the lower 16 bits of the address is also unique. In this common case, you can use AmazonEC2MachineID as Settings.MachineID.

See example that runs Sonyflake on AWS Elastic Beanstalk.

License

The MIT License (MIT)

See LICENSE for details.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.