GoFlake

command module
v0.0.0-...-c95a47d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2021 License: Apache-2.0 Imports: 2 Imported by: 0

README

GOFLAKE

goflake

GitHub go.mod Go version Github code coverage GitHub repo size GitHub tag (latest by date) GitHub last commit GitHub

Go Report Card Go Docs Mentioned in Awesome Go

A highly scalable, customizable, and serverless unique ID generator for use in distributed systems. Written in GoLang. Inspired by Twitters Snowflake.

GoFlake is a small library that quickly generates unique identifiers in the microservices space that don't require an additional server to operate. It is based off of Twitters Snowflake project.

  • Its criteria are :
    • A check digit
    • An epoch time in Milliseconds
    • A service number
    • A process ID for the machine
    • A sequence number.

      The Check digit uses 0 by default. The purpose for it is for auditing. You can set the checkdigit to a 1 later if you wish and observe all IDs that were generated with a 1. Use it as an event code if you would like. Its your call.

Running things on the same clock in distributed systems is a tough problem to solve. An Epoch time is used. The Epoch time is set in components/epoch.go . Set an epoch time in milliseconds that you would consider your microservice's Epoch and whenever an object is created the time since then in milliseconds will be generated into the IDs field.

A service number is used to determine where the object created is being used. For example 311 . In my Microservice 3 is for the Comments Service. 1 for the REST protocol. and 1 for the Deployment method - localhost. This can be tailored to your liking.

The process ID is the next field and is taken from the machine that is running the service

Finally a 5 digit random sequence number adds a 5 character string with : a-z A-Z 0-9

A generated Identifier would look like this : 0-14591498880493-0311-425471-43Que




Installation

Linux:

If you have GoLang installed. Clone the repository, CD into it and :

go run main.go 

This should run an example.

Copy the generator and components packages into your folder. Add them into mod.go to use GoFlake in your microservice. The packages also come with full code coverage.

Usage example

func main () {

	for i := 0; i <= 100; i++ {
		fmt.Println(generator.GenerateIdentifier())
        //will print 100 IDs 
	}

}

Set your options in the generator/generateid.go and then the GenerateIdentifier() function returns a unique identifier.

Additional documentation is available through godoc. http://localhost:6060/pkg and can be found under "Third Party". The Go Reference is available at https://pkg.golang.ir/github.com/hart87/goflake

Release History

  • 1.0.0
    • Minimum Viable Product

Meta

James Hart – [email protected]

Distributed under the Apache license. See LICENSE for more information.

https://github.com/hart87/goflake

Contributing

  1. Please submit any pull requests on the master branch. If you add a feature, please submit it with a passing test.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL