GithubHelp home page GithubHelp logo

mr-maxwang / snowflake Goto Github PK

View Code? Open in Web Editor NEW

This project forked from starrylab/tsid.go

0.0 0.0 0.0 94 KB

A unique ID generator based on a timestamp or time series, inspired by Twitter's Snowflake.

License: MIT License

Go 100.00%

snowflake's Introduction

TSID

GitHub GitHub go.mod Go version Go Reference Go Go Report Card codecov GitHub release (latest SemVer including pre-releases) GitHub last commit GitHub repo size GitHub repo file count GitHub Repo stars

English | 中文

A unique ID generator based on a timestamp or time series, inspired by Twitter's Snowflake.

The goal is to provide a unique identification (or UUID) solution that is reliable and flexible (configurable, extensible), but the performance is lower than the classical (fixed width and position) snowflake algorithm. Also provides the implementation of the classic snowflake algorithm(func Simple(server int64) func()int64). See Example 2

NOTES! ❗️Timestamp segment and sequence segment is REQUIRED!

HOWTO 🛠️

go get github.com/StarryLab/tsid.go

FEATURES ✨

  1. The maximum 126 bits
  2. Customize the width of each bit-segment
  3. Customize the order of bit-segments
  4. Support customize encoder
  5. BASE36 is default, using the go package strconv.FormatInt
  6. An improved BASE64 encoder to encode/decode identifiers
  7. Customize the options or use the provided default settings
  8. Supports random or auto-increment identifiers. Notes: auto-increment identifiers are still random and not strictly increment
  9. Provides a classic snowflake algorithm (fixed width and position), with better performance
  10. Data source types
    • Timestamps of various precision: nanosecond, millisecond, microsecond, and second
    • Various date and time values: year, month, day, week, hour, minute, second, millisecond, and the number of days and weeks in a year
    • 1 to 63 bits secure random number
    • Option value
    • Environment variables
    • Fixed value
    • Simple sequence/serial number, like a counter
    • Data sources
    • Parameter by caller

USAGE 🚀

Example 1

package main

import (
  "fmt"

  . "github.com/StarryLab/tsid.go"
)

func main() {
  // $> ./tsid -host=8 -node=6
  host := flag.Int("host", "data center(host) id")
  node := flag.Int("node", "server node id")
  b, e := Snowflake(host, node)
  if e != nil {
    fmt.Println("Error: ", e)
    return
  }
  fmt.Println("TSID: ", b.NextString()) // strconv.FormatInt
}

Example 2

package main

import (
  "flag"
  "fmt"

  . "github.com/StarryLab/tsid.go"
)

func main() {
  // $> ./tsid -host=8
  host := flag.Int("host", "data center(host) id")
  c, e := Simple(host)
  if e != nil {
    fmt.Println("Error: ", e)
    return
  }
  for i := 0; i < 100; i++ {
    fmt.Printf("%3d. %d", i+1, c())
  }
}

Example 3

  1. examples/demo.go
package examples

import (
  "github.com/StarryLab/tsid.go"
)

func init() {
  tsid.Register("my_data_source", DemoDataSource{map[string]int64{
    "demo": 1,
    "other": 9,
  }})
}

type DemoDataSource struct{
  data map[string]int64
}

func(d *DemoDataSource)Read(query ...interface{}) (int64, error) {
  if len(query)>0 {
    if s, o := query[0].(string); o {
      if v, o := d.data[s]; o {
        return v, nil
      }
    }
  }
  return 0, errors.New("data not found")
}
  1. main.go
package main

import (
  "fmt"

  _ "examples"
  . "github.com/StarryLab/tsid.go"
)

func main() {
  // Environment variable: SERVER_HOST, SERVER_NODE
  opt := O(
    Sequence(12),                         // 12 bits, REQUIRED!
    Env(6, "SERVER_HOST", 0)              // 6 bits [0, 31], data center id
    Env(4, "SERVER_NODE", 0)              // 4 bits [0, 15], server node id
    Data(10, "my_data_source", 2, "demo") // 10 bits [0, 1023], data source
    Random(30),                           // 30 bits
    Timestamp(41, TimestampMilliseconds), // 41 bits, REQUIRED!
  )
  b, e := Make(opt)
  if e != nil {
    fmt.Println("Error: ", e)
    return
  }
  fmt.Println("TSID: ", b.NextString()) // strconv.FormatInt
}

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.