goid

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2017 License: BSD-3-Clause Imports: 8 Imported by: 0

README

Globally Unique ID Generator

godoc license Build Status Coverage

Package goid is a globally unique id generator library, ready to be used safely directly in your server code.

goid is using 128bit and hashids algorithm to generate globally unique ids to make it shorter when transported as a string: https://github.com/speps/go-hashids

  • 8-byte value representing the milliseconds since the Unix epoch,
  • 4-byte counter, starting with a random value,
  • 6-bit server room id,
  • 8-bit cluster id,
  • 9-bit machine id,
  • 3-bit work id, and
  • 6-bit op id.

The string representation is using hashids to generates (28bytes).

Features:

  • Size: 16 bytes (128 bits)
  • Lock-free (i.e.: unlike UUIDv1 and v2)

Install

go get -u github.com/speps/go-hashids
go get -u github.com/jefurry/goid

Usage

import (
    "github.com/jefurry/goid"
)

id := goid.New()

s, _ := id.Encode(0, 0, 0, 0, 0)
err := id.FromString(s)

func init() {
    goid.SeedMathRand()
}

Get goid embedded info:

id.Timestamp()
id.Counter()
id.ServerRoomID()
id.ClusterID()
id.MachineID()
id.WorkID()
id.OpID()

Licenses

All source code is licensed under the BSD License.

Documentation

Index

Constants

View Source
const (
	VERSION            = "0.0.1"
	DEFAULT_ALPHABET   = hashids.DefaultAlphabet
	DEFAULT_MIN_LENGTH = 28
	DEFAULT_SALT       = `aC%eNsc8@!03Yb#69d`
	RAW_LEN            = 4 // 4 * 4 bytes
)

Variables

View Source
var (

	// SeededSecurely is set to true if a cryptographically secure seed
	// was used to initialize rand.  When false, the start time is used
	// as a seed.
	SeededSecurely bool
)

Functions

func SeedMathRand

func SeedMathRand()

SeedMathRand provides weak, but guaranteed seeding, which is better than running with Go's default seed of 1. A call to SeedMathRand() is expected to be called via init(), but never a second time.

Types

type ID

type ID struct {
	// contains filtered or unexported fields
}

func New

func New() *ID

New generates a globaly unique ID

func (*ID) ClusterID

func (id *ID) ClusterID() uint32

Clusterid returns the clusterid part of the id.

func (*ID) Counter

func (id *ID) Counter() uint32

Counter returns the counter part of the id.

func (*ID) Encode

func (id *ID) Encode(serverRoomID, clusterID, machineID, workID, opID uint32) (string, error)

Encode generates globally unique id serverroomID: [0, 64) clusterID: [0, 256) machineID: [0, 512) workID: [0, 8) opID: [0, 64)

func (*ID) FromString

func (id *ID) FromString(s string) error

FromString reads an ID from its string representation

func (*ID) MachineID

func (id *ID) MachineID() uint32

Machineid returns the machineid part of the id.

func (*ID) OpID

func (id *ID) OpID() uint32

OpID returns the opID part of the id.

func (*ID) ServerRoomID

func (id *ID) ServerRoomID() uint32

Serverroomid returns the serverroomid part of the id.

func (*ID) SetAlphabet

func (id *ID) SetAlphabet(alphabet string) *ID

SetAlphabet set alphabet of hashids

func (*ID) SetMinLength

func (id *ID) SetMinLength(minLength int) *ID

SetMinLength set minlength of hashids

func (*ID) SetSalt

func (id *ID) SetSalt(salt string) *ID

SetSalt set salt as key secret of hashids

func (*ID) Timestamp

func (id *ID) Timestamp() int64

Timestamp returns the timestamp part of the id.

func (*ID) WorkID

func (id *ID) WorkID() uint32

WorkID returns the workID part of the id.

Jump to

Keyboard shortcuts

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