A distributed unique ID generator of using Sonyflake and encoded by Base58

Indigo

GitHub Actions codecov Go Report Card codebeat badge Maintainability GoDoc GitHub license

About

  • A distributed unique ID generator of using Sonyflake and encoded by Base58.
  • ID max length is 11 characters by unsigned int64 max value.
  • An Encoder can change your original encoder ;)

Install

$ go get -u github.com/osamingo/indigo

Usage

package main

import (
	"log"
	"sync"
	"time"

	"github.com/osamingo/indigo"
)

var g *indigo.Generator

func init() {
	t := time.Unix(1257894000, 0) // 2009-11-10 23:00:00 UTC
	g = indigo.New(nil, indigo.StartTime(t))
	_, err := g.NextID()
	if err != nil {
		log.Fatalln(err)
	}
}

func main() {

	wg := sync.WaitGroup{}
	wg.Add(100)
	for i := 0; i < 100; i++ {
		go func() {
			defer wg.Done()
			id, err := g.NextID()
			if err != nil {
				log.Fatalln(err)
			} else {
				log.Println("ID:", id)
			}
		}()
	}

	wg.Wait()
}

Benchmark

# Machine: MacBook Pro (13-inch, 2018, Four Thunderbolt 3 Ports)
# CPU    : 2.7 GHz Intel Core i7
# Memory : 16 GB 2133 MHz LPDDR3

goos: darwin
goarch: amd64
pkg: github.com/osamingo/indigo
BenchmarkGenerator_NextID-8        30136       39099 ns/op        7 B/op     1 allocs/op
PASS

Bibliography

  • Sonyflake - A distributed unique ID generator inspired by Twitter's Snowflake.
  • Base58 - Base58 is a group of binary-to-text encoding schemes used to represent large integers as alphanumeric text.

License

Released under the MIT License.

Owner
Osamu TONOMORI
I'm an Engineering Manager and a Technical Program Manager working in the FinTech corp. I'm supported by some ventures on the side work.
Osamu TONOMORI
Similar Resources

Unit tests generator for Go programming language

Unit tests generator for Go programming language

GoUnit GoUnit is a commandline tool that generates tests stubs based on source function or method signature. There are plugins for Vim Emacs Atom Subl

Jan 1, 2023

GObject-introspection based bindings generator

WARNING! This project is no longer maintained. Probably doesn't even compile. GObject-introspection based bindings generator for Go. Work in progress

Jan 5, 2022

Typo/error resilient, human-readable token generator

toktok A human-friendly token generator Creates tokens which avoid characters that can be easily misinterpreted, like '1' and 'I' or '8' and 'B', as w

Sep 16, 2022

Jennifer is a code generator for Go

Jennifer Jennifer is a code generator for Go. package main import ( "fmt" . "github.com/dave/jennifer/jen" ) func main() { f := NewFile("m

Dec 25, 2022

General Golang Code Generator

gg gg is a General Golang Code Generator: A Good Game to play with Golang. package main import ( "fmt" . "github.com/Xuanwo/gg" ) func main() {

Jan 7, 2023

Default godoc generator - make your first steps towards better code documentation

godoc-generate Overview godoc-generate is a simple command line tool that generates default godoc comments on all exported types, functions, consts an

Sep 14, 2022

accessor methods generator for Go programming language

accessory accessory is an accessor generator for Go programming language. What is accessory? Accessory is a tool that generates accessor methods from

Nov 15, 2022

RinkWorks fantasy name generator in golang

RinkWorks fantasy name generator in golang

Dec 25, 2022

Go Param Generator for golang

Go Param Generator Overview Generates struct's for you. No need to write it by hands Adds Getters and Setters Adds Constructor Easy to use Usage gopar

Dec 13, 2021
Comments
  • generate same id in same node

    generate same id in same node

    eg: dateTmStr := "2017-10-11 16:00:00" loc, _ := time.LoadLocation("Local") dateTime, err := time.ParseInLocation("2006-01-02 15:04:05", dateTmStr, loc)

    genHandle := indigo.New(nil, indigo.StartTime(dateTime), indigo.MachineID(1))

    idStr, err := genHandle .NextID() // 这idstr会是相同的id eg: my log show this( id generation in first time) 2020/03/05 16:45:31.579 [D] [****.go:42] new idstr: J6asGGvR1A 2020/03/05 16:45:31.579 [D] [***.go:40] new global uniq id: J6asGGvR1A

    (then generate an other id, log show the same as first one):

    2020/03/05 16:45:31.579 [D] [****.go:339] new token id: J6asGGvR1A 2020/03/05 16:45:31.579 [D] [****.go:347] new token: J6asGGvR1A

    question: the implementation of unique id generator is not ok, as it produce same id in one node. its' implementation need to check..

  • Repeated IDs and multiple instance use case

    Repeated IDs and multiple instance use case

    I am hoping to use this in some code for generating unique max 11 character IDs.

    A couple of questions:

    i) Can the library be used where different instances of a code, say in a Lambda Functions, need to get a unique ID, or there needs to be a running server, coordinating the IDs? ii) I tried a small unit test:

    g := indigo.New(nil, indigo.StartTime(time.Now()))
    
    id1, err := g.NextID()
    if err != nil {
    	t.Error("should be nil")
    }
    
    fmt.Println(id1)
    
    id2, err := g.NextID()
    if err != nil {
    	t.Error("should be nil")
    }
    
    fmt.Println(id2)
    

    I was getting same values for different executions of the test:

    === RUN TestIndigoID nvReu8adFt nvReu8axjp

    === RUN TestIndigoID nvReu8adFt nvReu8axjp

    Another set of consecutive runs:

    === RUN TestIndigoID 2Uzir 2VLCn

    === RUN TestIndigoID 2Uzir 2VLCn Does this mean it starts from an initial value whenever there is a call to indigo.New?

    Thanks

Super short, fully unique, non-sequential and URL friendly Ids

Generator of unique non-sequential short Ids The package shortidenables the generation of short, fully unique, non-sequential and by default URL frien

Dec 30, 2022
PKSUID is a small extension for KSUID (K-Sortable Globally Unique IDs) which allows prefixing it with arbitrary up to 16 bytes strings

PKSUID is a small extension for KSUID (K-Sortable Globally Unique IDs) which allows prefixing it with arbitrary up to 16 bytes strings

Dec 1, 2022
Simple distributed kv-store using ABD algorithm.

Distributed-kv-store Simple distributed kv-store using ABD algorithm. API GET /key Get value by key. 302 = found key. PUT /key Put key with value. 201

Dec 14, 2021
Random fake data and struct generator for Go.

Faker Random fake data and struct generator for Go. More than 100 generator functions Struct generator Unique data generator Builtin types support Eas

Oct 3, 2022
:guardsman: A teeny tiny and somewhat opinionated generator for your next golang project

A Yeoman Golang Generator We are very sorry Gophers, but other names for the generator where taken, so we choose go-lang. But we have gocreate as an a

Sep 27, 2022
XSD (XML Schema Definition) parser and Go/C/Java/Rust/TypeScript code generator

xgen Introduction xgen is a library written in pure Go providing a set of functions that allow you to parse XSD (XML schema definition) files. This li

Jan 1, 2023
Fast and secure initramfs generator
Fast and secure initramfs generator

Booster - fast and secure initramfs generator Initramfs is a specially crafted small root filesystem that mounted at the early stages of Linux OS boot

Dec 28, 2022
Go-sudoku - Sudoku generator and solver implementation in GoLang

go-sudoku An implementation of Sudoku generators and solvers in GoLang. Usage He

Nov 7, 2022
Visualize how a projects source code is distributed among its files and folders
Visualize how a projects source code is distributed among its files and folders

Source Code Visualizer Visualize the code distribution in a project. Applications Applications include: Visualizing code distribution for more educate

Jul 31, 2022
Random fake data generator written in go
Random fake data generator written in go

Gofakeit Random data generator written in go Features 160+ Functions!!! Concurrent Global Rand Struct Generator Custom Functions Http Server Command L

Jan 1, 2023