项目作者: rosbit

项目描述 :
distributed id generators, with a snowflake-like id generator and an auto-incremented id generator. 分布式id生成器,包括snowflake id、自增id、订单id
高级语言: Go
项目地址: git://github.com/rosbit/id-generator.git
创建时间: 2019-05-13T09:57:11Z
项目社区:https://github.com/rosbit/id-generator

开源协议:MIT License

下载


Distributed unique ID Generator

id-generator is a dirstributed unique ID generator. There are 3 kinds of generators
provided by id-generator:

  1. snowflake-like ID generator

    • inspired by Twitter’s Snowflake.
    • A snowflake-like ID is composed of

      1. 31 bits for time in units of 1 second (limit: 2**31/3600/24/365 = 68 years)
      2. 22 bits for a sequence number (limit: 2**22-1 = 4,194,303 per second)
      3. 10 bits for a worker id (limit: 2**10 = 1024)
  2. auto-inremented sequence ID generator

    • id auto-incremented
    • sequence ID is composed of

      1. 53 bits for a sequence number (limit: 2**53-1 = 9,007,199,254,740,991)
      2. 10 bits for a worker id (limit: 2**10 = 1024)
  3. order ID generator

    • id is of type uint64
    • as a digit string, its head 6 chars are date with layout “YYMMDD”
    • its tail chars form the workerId specified when initing
    • the digit of the whole order id is composed of 3 parts

      • short order id

        1. parts format: YYMMDDxxxxxxxWW
        2. YYMMDD stands for Year, Month, Day. (upper limit: 991231)
        3. xxxxxxx stands for order Id sequence. (upper limit: 10,000,000 per day)
        4. WW stands for worker id. (upper limit: 100)
      • long order id

        1. parts format: YYMMDDhhmmxxxxxxWW
        2. YYMMDDhhmm stands for Year, Month, Day, Hour, Minute. (upper limit: 9912312359)
        3. xxxxxx stands for order Id sequence. (upper limit: 1,000,000 per minute)
        4. WW stands for worker id. (upper limit: 100)

Installation

The package is fully go-getable without any dependency, So, just type

go get github.com/rosbit/id-generator

Usage

  1. package main
  2. import (
  3. "github.com/rosbit/id-generator"
  4. "time"
  5. )
  6. func main() {
  7. // 1. for snowflake-like ID
  8. epochTime := time.Now().AddDate(0, 0, -1) // set your epochTime, a history time
  9. workerId := uint16(1) // set your own workerId
  10. sf := idgen.NewFlakeIdGenerator(echochTime, workerId) // 64bit id
  11. // sf := idgen.NewFlakeIdGenerator32(echochTime, workerId) // 32bit id, most of time, 32bit is ok.
  12. for i:=0; i<10; i++ {
  13. id := sf.NextID()
  14. // id
  15. }
  16. sf.Exit()
  17. // 2. for order id
  18. ord := idgen.NewOrderIdGenerator(workerId, "Asia/Shanghai") // any valid tz string is ok
  19. for i:=0; i<10; i++ {
  20. id := ord.NextID()
  21. // id
  22. }
  23. ord.Exit()
  24. ord2 := idgen.NewLongOrderIdGenerator(workerId, "Asia/Shanghai") // any valid tz string is ok
  25. for i:=0; i<10; i++ {
  26. id := ord2.NextID()
  27. // id
  28. }
  29. ord2.Exit()
  30. // 3. for sequence id
  31. seq := idgen.NewSeqIdGenerator(workerId, 0) // startId, any uint64 is ok
  32. for i:=0; i<10; i++ {
  33. id := seq.NextID()
  34. // id
  35. }
  36. seq.Exit()
  37. }

Benchmarks

Under my aliyun-ECS: 1core CPU, Intel(R) Xeon(R) CPU E5-2650 v2 @ 2.60GHz

The benchmark result is:

  1. goos: linux
  2. goarch: amd64
  3. pkg: github.com/rosbit/id-generator
  4. Benchmark_orderNextId 3000000 479 ns/op
  5. Benchmark_seqNextId 3000000 469 ns/op
  6. Benchmark_snowFlakeNextId 3000000 472 ns/op
  7. PASS
  8. ok github.com/rosbit/id-generator 3.781s

Contribution

Pull requests are welcome! Also, if you want to discuss something,
send a pull request with proposal and changes.