xid16

package
v2.0.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2019 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package xid is a globally unique id generator suited for web scale

Xid is using Mongo Object ID algorithm to generate globally unique ids: https://docs.mongodb.org/manual/reference/object-id/

  • 4-byte value representing the seconds since the Unix epoch,
  • 3-byte machine identifier,
  • 2-byte process id, and
  • 3-byte counter, starting with a random value.

The binary representation of the id is compatible with Mongo 12 bytes Object IDs. The string representation is using URL safe base64 for better space efficiency when stored in that form (16 bytes).

UUID is 16 bytes (128 bits), snowflake is 8 bytes (64 bits), xid stands in between with 12 bytes with a more compact string representation ready for the web and no required configuration or central generation server.

Features:

  • Size: 12 bytes (96 bits), smaller than UUID, larger than snowflake
  • Base64 URL safe encoded by default (16 bytes storage when transported as printable string)
  • Non configured, you don't need set a unique machine and/or data center id
  • K-ordered
  • Embedded time with 1 second precision
  • Unicity guaranted for 16,777,216 (24 bits) unique ids per second and per host/process

Best used with xlog's RequestIDHandler (https://godoc.org/github.com/rs/xlog#RequestIDHandler).

References:

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidID = errors.New("invalid ID")

ErrInvalidID is returned when trying to unmarshal an invalid ID

Functions

This section is empty.

Types

type ID

type ID [rawLen]byte

ID represents a unique request id

func New

func New() ID

New generates a globaly unique ID

func (ID) Counter

func (id ID) Counter() int32

Counter returns the incrementing value part of the id. It's a runtime error to call this method with an invalid id.

func (ID) Machine

func (id ID) Machine() []byte

Machine returns the 3-byte machine id part of the id. It's a runtime error to call this method with an invalid id.

func (ID) MarshalText

func (id ID) MarshalText() (text []byte, err error)

MarshalText implements encoding/text TextMarshaler interface

func (ID) Pid

func (id ID) Pid() uint16

Pid returns the process id part of the id. It's a runtime error to call this method with an invalid id.

func (ID) String

func (id ID) String() string

String returns a base64 URL safe representation of the id

func (ID) Time

func (id ID) Time() time.Time

Time returns the timestamp part of the id. It's a runtime error to call this method with an invalid id.

func (*ID) UnmarshalText

func (id *ID) UnmarshalText(text []byte) error

UnmarshalText implements encoding/text TextUnmarshaler interface

Jump to

Keyboard shortcuts

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