2016-06-01 16:12:53 -05:00
|
|
|
snowflake
|
2016-06-01 14:59:26 -05:00
|
|
|
====
|
2016-06-01 16:12:53 -05:00
|
|
|
[![GoDoc](https://godoc.org/github.com/bwmarrin/snowflake?status.svg)](https://godoc.org/github.com/bwmarrin/snowflake) [![Go report](http://goreportcard.com/badge/bwmarrin/snowflake)](http://goreportcard.com/report/bwmarrin/snowflake) [![Build Status](https://travis-ci.org/bwmarrin/snowflake.svg?branch=master)](https://travis-ci.org/bwmarrin/snowflake) [![Discord Gophers](https://img.shields.io/badge/Discord%20Gophers-%23info-blue.svg)](https://discord.gg/0f1SbxBZjYq9jLBk)
|
2016-05-25 14:56:18 -05:00
|
|
|
|
2016-06-01 16:12:53 -05:00
|
|
|
snowflake is a [Go](https://golang.org/) package that provides
|
2016-06-01 16:03:53 -05:00
|
|
|
* A very simple Twitter snowflake generator.
|
2016-06-01 15:52:49 -05:00
|
|
|
* Methods to parse existing snowflake IDs.
|
2016-06-01 15:51:05 -05:00
|
|
|
* Methods to convert a snowflake ID into several other data types.
|
2016-06-01 16:12:53 -05:00
|
|
|
* JSON Marshal/Unmarshal functions to easily use snowflake IDs within a JSON API.
|
2016-05-25 15:07:49 -05:00
|
|
|
|
2016-06-01 15:56:22 -05:00
|
|
|
**For help with this package or general Go discussion, please join the [Discord
|
|
|
|
Gophers](https://discord.gg/0f1SbxBZjYq9jLBk) chat server.**
|
|
|
|
|
2016-06-01 14:59:26 -05:00
|
|
|
## Getting Started
|
|
|
|
|
|
|
|
### Installing
|
|
|
|
|
|
|
|
This assumes you already have a working Go environment, if not please see
|
|
|
|
[this page](https://golang.org/doc/install) first.
|
|
|
|
|
|
|
|
```sh
|
2016-06-01 16:12:53 -05:00
|
|
|
go get github.com/bwmarrin/snowflake
|
2016-05-25 15:07:49 -05:00
|
|
|
```
|
2016-06-01 14:59:26 -05:00
|
|
|
|
2016-06-01 15:17:09 -05:00
|
|
|
### Usage
|
2016-06-01 14:59:26 -05:00
|
|
|
|
2016-06-01 16:12:53 -05:00
|
|
|
Import the package into your project then construct a new snowflake Node using a
|
2016-06-01 15:51:05 -05:00
|
|
|
unique node number from 0 to 1023. With the node object call the Generate()
|
|
|
|
method to generate and return a unique snowflake ID.
|
|
|
|
|
|
|
|
Keep in mind that each node you create must have a unique node number, even
|
|
|
|
across multiple servers. If you do not keep node numbers unique the generator
|
|
|
|
cannot guarantee unique IDs across all nodes.
|
|
|
|
|
2016-06-01 14:59:26 -05:00
|
|
|
|
2016-06-01 15:17:09 -05:00
|
|
|
**Example Program:**
|
2016-06-01 15:15:24 -05:00
|
|
|
|
2016-06-01 14:59:26 -05:00
|
|
|
```go
|
2016-06-01 15:15:24 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-06-01 16:12:53 -05:00
|
|
|
"github.com/bwmarrin/snowflake"
|
2016-06-01 15:15:24 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
// Create a new Node with a Node number of 1
|
2016-06-01 16:12:53 -05:00
|
|
|
node, err := snowflake.NewNode(1)
|
2016-06-01 15:15:24 -05:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a snowflake ID.
|
|
|
|
id, err := node.Generate()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print out the ID in a few different ways.
|
|
|
|
fmt.Printf("Int64 ID: %d\n", id)
|
|
|
|
fmt.Printf("String ID: %s\n", id)
|
|
|
|
fmt.Printf("Base2 ID: %s\n", id.Base2())
|
|
|
|
fmt.Printf("Base64 ID: %s\n", id.Base64())
|
|
|
|
|
|
|
|
// Print out the ID's timestamp
|
|
|
|
fmt.Printf("ID Time : %d\n", id.Time())
|
|
|
|
|
|
|
|
// Print out the ID's node number
|
|
|
|
fmt.Printf("ID Node : %d\n", id.Node())
|
|
|
|
|
|
|
|
// Print out the ID's sequence number
|
|
|
|
fmt.Printf("ID Step : %d\n", id.Step())
|
|
|
|
}
|
2016-05-25 15:07:49 -05:00
|
|
|
```
|
2016-06-01 15:33:04 -05:00
|
|
|
|
|
|
|
### Performance
|
|
|
|
|
2016-06-01 16:12:53 -05:00
|
|
|
This snowflake generator should be sufficiently fast enough on most systems to
|
2016-06-01 15:51:05 -05:00
|
|
|
generate 4096 unique ID's per millisecond. This is the maximum that the
|
|
|
|
snowflake ID format supports. That is, around 243-244 nanoseconds per operation.
|
2016-06-01 15:33:04 -05:00
|
|
|
|
2016-06-01 16:12:53 -05:00
|
|
|
Since the snowflake generator is single threaded the primary limitation will be
|
2016-06-01 15:33:04 -05:00
|
|
|
the maximum speed of a single processor on your system.
|
|
|
|
|
|
|
|
To benchmark the generator on your system run the following command inside the
|
2016-06-01 16:12:53 -05:00
|
|
|
snowflake package directory.
|
2016-06-01 15:33:04 -05:00
|
|
|
|
|
|
|
```sh
|
|
|
|
go test -bench=.
|
|
|
|
```
|
|
|
|
|
2016-06-01 15:51:05 -05:00
|
|
|
If your curious, check out this commit that shows benchmarks that compare a few
|
|
|
|
different ways of implementing a snowflake generator in Go.
|
2016-06-01 16:12:53 -05:00
|
|
|
* https://github.com/bwmarrin/snowflake/tree/9befef8908df13f4102ed21f42b083dd862b5036
|