All Projects → godruoyi → go-snowflake

godruoyi / go-snowflake

Licence: MIT license
❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to go-snowflake

Snow Stamp
Get the timestamp from a Discord snowflake ❄
Stars: ✭ 95 (-53.88%)
Mutual labels:  snowflake
Snowflakes
❄️ Snowflakes in JavaScript
Stars: ✭ 170 (-17.48%)
Mutual labels:  snowflake
sno
Compact, sortable and fast unique IDs with embedded metadata.
Stars: ✭ 77 (-62.62%)
Mutual labels:  snowflake
Id Generator
id-generator部署即使用的ID生成器, 支持HTTP、Dubbo、Spring Cloud方式.
Stars: ✭ 112 (-45.63%)
Mutual labels:  snowflake
Yuniql
Free and open source schema versioning and database migration made natively with .NET Core.
Stars: ✭ 156 (-24.27%)
Mutual labels:  snowflake
Sqitch
Sensible database change management
Stars: ✭ 2,320 (+1026.21%)
Mutual labels:  snowflake
Sonyflake Rs
🃏 A distributed unique ID generator inspired by Twitter's Snowflake.
Stars: ✭ 91 (-55.83%)
Mutual labels:  snowflake
snowflake
a language
Stars: ✭ 16 (-92.23%)
Mutual labels:  snowflake
Terraform Provider Snowflake
Terraform provider for managing Snowflake accounts
Stars: ✭ 165 (-19.9%)
Mutual labels:  snowflake
onionfruit
OnionFruit™ Connect - Tor access client with country selection, bridge configuration, pluggable transports and experimental DNS support
Stars: ✭ 150 (-27.18%)
Mutual labels:  snowflake
Snowflake
java edition of [Twitter Snowflake](https://github.com/twitter/snowflake), a network service for generating unique ID numbers at high scale with some simple guarantees.
Stars: ✭ 114 (-44.66%)
Mutual labels:  snowflake
Did
高性能的ID生成器, 基于rpcx和Memcached协议提供网络服务调用
Stars: ✭ 120 (-41.75%)
Mutual labels:  snowflake
Fluentmigrator
Fluent migrations framework for .NET
Stars: ✭ 2,636 (+1179.61%)
Mutual labels:  snowflake
Gosnowflake
Go Snowflake Driver
Stars: ✭ 108 (-47.57%)
Mutual labels:  snowflake
Excelerator
This is an Excel Addin for Windows that reads and writes data to Snowflake
Stars: ✭ 53 (-74.27%)
Mutual labels:  snowflake
Snowflake
A simple to use Go (golang) package to generate or parse Twitter snowflake IDs
Stars: ✭ 1,314 (+537.86%)
Mutual labels:  snowflake
Idworker
idworker 是一个基于zookeeper和snowflake算法的分布式ID生成工具,通过zookeeper自动注册机器(最多1024台),无需手动指定workerId和datacenterId
Stars: ✭ 171 (-16.99%)
Mutual labels:  snowflake
snowflake
Yet another snowflake
Stars: ✭ 22 (-89.32%)
Mutual labels:  snowflake
DBTestCompare
Application to compare results of two SQL queries
Stars: ✭ 15 (-92.72%)
Mutual labels:  snowflake
DiscordLookup
DiscordLookup | Get more out of Discord with Discord Lookup! Snowflake Decoder, Guild List with Stats, Invite Info and more...
Stars: ✭ 86 (-58.25%)
Mutual labels:  snowflake

An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).

test Go report Coverage status

Description

An Lock Free ID Generator for Golang implementation.

file

Snowflake is a network service for generating unique ID numbers at high scale with some simple guarantees.

  • The first bit is unused sign bit.
  • The second part consists of a 41-bit timestamp (milliseconds) whose value is the offset of the current time relative to a certain time.
  • The 10 bits machineID(5 bit workid + 5 bit datacenter id), max value is 2^10 -1 = 1023.
  • The last part consists of 12 bits, its means the length of the serial number generated per millisecond per working node, a maximum of 2^12 -1 = 4095 IDs can be generated in the same millisecond.
  • The binary length of 41 bits is at most 2^41 -1 millisecond = 69 years. So the snowflake algorithm can be used for up to 69 years, In order to maximize the use of the algorithm, you should specify a start time for it.

The ID generated by the snowflake algorithm is not guaranteed to be unique. For example, when two different requests enter the same machine at the same time, and the sequence generated by the node is the same, the generated ID will be duplicated.

So if you want use the snowflake algorithm to generate unique ID, You must ensure: The sequence-number generated in the same millisecond of the same node is unique.

Based on this, we created this package and integrated multiple sequence-number providers into it.

  • AtomicResolver (base sync/atomic)

Each provider only needs to ensure that the serial number generated in the same millisecond is different. You can get a unique ID.

Feature

  • Lock Free
  • 🎈 Zero configuration, out of the box
  • 🚀 Concurrency safety
  • 🌵 Support private ip to machineid
  • 🐡 Support custom sequence resolver

Installation

$ go get github.com/godruoyi/go-snowflake

Usage

  1. simple to use.
package main

import (
    "fmt"

    "github.com/godruoyi/go-snowflake"
)

func main() {
    id := snowflake.ID()
    fmt.Println(id)
    // 1537200202186752
}
  1. Specify the MachineID.
package main

import (
    "fmt"

    "github.com/godruoyi/go-snowflake"
)

func main() {
    snowflake.SetMachineID(1)

    // Or set private ip to machineid, testing...
    // snowflake.SetMachineID(snowflake.PrivateIPToMachineID())

    id := snowflake.ID()
    fmt.Println(id)
}
  1. Specify start time.
package main

import (
    "fmt"
    "time"

    "github.com/godruoyi/go-snowflake"
)

func main() {
    snowflake.SetStartTime(time.Date(2014, 9, 1, 0, 0, 0, 0, time.UTC))
    id := snowflake.ID()
    fmt.Println(id)
}
  1. Parse ID.
package main

import (
    "fmt"
    "time"

    "github.com/godruoyi/go-snowflake"
)

func main() {
    id := snowflake.ID()
    sid := snowflake.ParseID(id)

    fmt.Println(sid.ID)             // 132271570944000000
    fmt.Println(sid.MachineID)      // 0
    fmt.Println(sid.Sequence)       // 0
    fmt.Println(sid.Timestamp)      // 31536000000
    fmt.Println(sid.GenerateTime()) // 2009-11-10 23:00:00 +0000 UTC
}

Best practices

⚠️⚠️ All SetXXX method is thread-unsafe, recommended you call him in the main function.

package main

import (
    "fmt"
    "time"
    "net/http"

    "github.com/godruoyi/go-snowflake"
)

func main() {
    snowflake.SetMachineID(1) // change to your machineID
    snowflake.SetStartTime(time.Date(2014, 9, 1, 0, 0, 0, 0, time.UTC))

    http.HandleFunc("/order", submitOrder)
    http.ListenAndServe(":8090", nil)
}

func submitOrder(w http.ResponseWriter, req *http.Request) {
    orderId := snowflake.ID()
    // save order
}

Advanced

Custom sequence resolver. you can customize the sequence-number resolver by following way:

package main

import (
    "fmt"
    "time"

    "github.com/godruoyi/go-snowflake"
)

func yourSequenceNumber(ms int64) (uint16, error) {

}

// usage

snowflake.SetSequenceResolver(yourSequenceNumber)
snowflake.ID()

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].