dxyflake

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2021 License: MIT Imports: 7 Imported by: 0

README

dxyflake

duoxieyun distributed unique ID generator inspired by Twitter's Snowflake

ID Format

+------------------------------------------------------------------------------------------+
| 1 Bit Unused | 41 Bit Timestamp | 5 Bit MachineID | 5 Bit ServiceID | 12 Bit Sequence ID |
+------------------------------------------------------------------------------------------+

41 bits for time in units of 10 msec (697 years)
 5 bits for a machine id (32 nodes)
 5 bits for a service id (32 services per node)
12 bits for a sequence number (0 ~ 4095)

Install

go get github.com/GiterLab/dxyflake

Usage

package main

import (
    "fmt"
    "os"

    "github.com/GiterLab/dxyflake"
)

func main() {
    s := dxyflake.Settings{}
    s.Init(0, 0) // set mID & sID
    dxyid := dxyflake.NewDxyflake(s)

    id, err := dxyid.NextID()
    if err != nil {
        fmt.Println(err)
        os.Exit(0)
    }
    fmt.Println(id, id.LeadingZerosString(19), dxyflake.Decompose(id))
    idBase64 := id.Base64()
    id, err = dxyflake.ParseBase64(idBase64)
    if err != nil {
        fmt.Println(err)
        os.Exit(0)
    }
    fmt.Println(idBase64, "-->", id)

    // 19 MAX
    fmt.Println("9223372036854775807", dxyflake.Decompose(dxyflake.ID(9223372036854775807))) // 697 years
}

// Output:
//
// 475370495148032 0000475370495148032 map[id:475370495148032 machine-id:0 msb:0 sequence:0 service-id:0 time:113337158]
// NDc1MzcwNDk1MTQ4MDMy --> 475370495148032
// 9223372036854775807 map[id:9223372036854775807 machine-id:31 msb:0 sequence:4095 service-id:31 time:2199023255551]

License

The MIT License (MIT)

See LICENSE for details.

Reference

Documentation

Overview

Package dxyflake implements dxyflake, duoxieyun distributed unique ID generator inspired by Twitter's Snowflake.

+---------------------------------------------------------------------------------------+ | 1 Bit Unused | 41 Bit Timestamp | 5 Bit NodeID | 5 Bit ServiceID | 12 Bit Sequence ID | +---------------------------------------------------------------------------------------+

41 bits for time in units of 10 msec (697 years)

5 bits for a machine id (32 nodes)
5 bits for a service id (32 services per node)

12 bits for a sequence number (0 ~ 4095)

Index

Constants

View Source
const (
	BitLenTime      = 41 // bit length of time
	BitLenMachineID = 5  // bit length of machineID
	BitLenServiceID = 5  // bit length of serviceID
	BitLenSequence  = 12 // bit length of sequence number
)

These constants are the bit lengths of dxyflake ID parts.

Variables

View Source
var ErrInvalidBase32 = errors.New("invalid base32")

ErrInvalidBase32 is returned by ParseBase32 when given an invalid []byte

View Source
var ErrInvalidBase58 = errors.New("invalid base58")

ErrInvalidBase58 is returned by ParseBase58 when given an invalid []byte

Functions

func Decompose

func Decompose(id ID) map[string]int64

Decompose returns a set of dxyflake ID parts.

func NewDxyflake

func NewDxyflake(st Settings) *dxyflake

NewDxyflake returns a new dxyflake configured with the given Settings. NewDxyflake returns nil in the following cases: - Settings.StartTime is ahead of the current time. - Settings.MachineID returns an error. - Settings.ServiceID returns an error. - Settings.CheckMachineID returns false. - Settings.CheckServiceID returns false.

Types

type ID

type ID int64

An ID is a custom type used for a dxyflake ID. This is used so we can attach methods onto the ID.

func ParseBase2

func ParseBase2(id string) (ID, error)

ParseBase2 converts a Base2 string into a dxyflake ID

func ParseBase32

func ParseBase32(b []byte) (ID, error)

ParseBase32 parses a base32 []byte into a dxyflake ID NOTE: There are many different base32 implementations so becareful when doing any interoperation.

func ParseBase36

func ParseBase36(id string) (ID, error)

ParseBase36 converts a Base36 string into a dxyflake ID

func ParseBase58

func ParseBase58(b []byte) (ID, error)

ParseBase58 parses a base58 []byte into a dxyflake ID

func ParseBase64

func ParseBase64(id string) (ID, error)

ParseBase64 converts a base64 string into a dxyflake ID

func ParseBytes

func ParseBytes(id []byte) (ID, error)

ParseBytes converts a byte slice into a dxyflake ID

func ParseInt64

func ParseInt64(id int64) ID

ParseInt64 converts an int64 into a dxyflake ID

func ParseIntBytes

func ParseIntBytes(id [8]byte) ID

ParseIntBytes converts an array of bytes encoded as big endian integer as a dxyflake ID

func ParseString

func ParseString(id string) (ID, error)

ParseString converts a string into a dxyflake ID

func (ID) Base2

func (f ID) Base2() string

Base2 returns a string base2 of the dxyflake ID

func (ID) Base32

func (f ID) Base32() string

Base32 uses the z-base-32 character set but encodes and decodes similar to base58, allowing it to create an even smaller result string. NOTE: There are many different base32 implementations so becareful when doing any interoperation.

func (ID) Base36

func (f ID) Base36() string

Base36 returns a base36 string of the dxyflake ID

func (ID) Base58

func (f ID) Base58() string

Base58 returns a base58 string of the dxyflake ID

func (ID) Base64

func (f ID) Base64() string

Base64 returns a base64 string of the dxyflake ID

func (ID) Bytes

func (f ID) Bytes() []byte

Bytes returns a byte slice of the dxyflake ID

func (ID) Int64

func (f ID) Int64() int64

Int64 returns an int64 of the dxyflake ID

func (ID) IntBytes

func (f ID) IntBytes() [8]byte

IntBytes returns an array of bytes of the dxyflake ID, encoded as a big endian integer.

func (ID) LeadingZerosString

func (f ID) LeadingZerosString(zeroN uint8) string

LeadingZerosString returns a string of the dxyflake ID, leading zeros

func (ID) MarshalJSON

func (f ID) MarshalJSON() ([]byte, error)

MarshalJSON returns a json byte array string of the dxyflake ID.

func (ID) String

func (f ID) String() string

String returns a string of the dxyflake ID

func (*ID) UnmarshalJSON

func (f *ID) UnmarshalJSON(b []byte) error

UnmarshalJSON converts a json byte array of a dxyflake ID into an ID type.

type JSONSyntaxError

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

A JSONSyntaxError is returned from UnmarshalJSON if an invalid ID is provided.

func (JSONSyntaxError) Error

func (j JSONSyntaxError) Error() string

type Settings

type Settings struct {
	StartTime      time.Time
	MachineID      func() (uint16, error)
	ServiceID      func() (uint16, error)
	CheckMachineID func(uint16) bool
	CheckServiceID func(uint16) bool
}

Settings configures dxyflake:

StartTime is the time since which the dxyflake time is defined as the elapsed time. If StartTime is 0, the start time of the dxyflake is set to "2021-10-01 00:00:00 +0000 UTC". If StartTime is ahead of the current time, dxyflake is not created.

MachineID returns the unique ID of the dxyflake instance. If MachineID returns an error, dxyflake is not created. If MachineID is nil, default MachineID(0) is used.

ServiceID returns the unique ID of the dxyflake service per machine. If ServiceID returns an error, dxyflake is not created. If ServiceID is nil, default ServiceID(0) is used.

CheckMachineID validates the uniqueness of the machine ID. If CheckMachineID returns false, dxyflake is not created. If CheckMachineID is nil, no validation is done.

CheckServiceID validates the uniqueness of the service ID. If CheckServiceID returns false, dxyflake is not created. If CheckServiceID is nil, no validation is done.

func (*Settings) Init

func (s *Settings) Init(mID, sID uint16)

Init set default MachineID & ServiceID

func (*Settings) StartTimeSet

func (s *Settings) StartTimeSet(t time.Time)

StartTimeSet set start time

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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