Documentation
¶
Index ¶
- Constants
- func Decompose(spaceflakeID, baseEpoch uint64) map[string]uint64
- func DecomposeBinary(spaceflakeID, baseEpoch uint64) map[string]string
- func ParseNodeID(spaceflakeID uint64) uint64
- func ParseSequence(spaceflakeID uint64) uint64
- func ParseTime(spaceflakeID, baseEpoch uint64) uint64
- func ParseWorkerID(spaceflakeID uint64) uint64
- type BulkGeneratorSettings
- type GeneratorSettings
- type Node
- type Spaceflake
- func (s *Spaceflake) BinaryID() string
- func (s *Spaceflake) Decompose() map[string]uint64
- func (s *Spaceflake) DecomposeBinary() map[string]string
- func (s *Spaceflake) ID() uint64
- func (s *Spaceflake) NodeID() uint64
- func (s *Spaceflake) Sequence() uint64
- func (s *Spaceflake) StringID() string
- func (s *Spaceflake) Time() uint64
- func (s *Spaceflake) WorkerID() uint64
- type Worker
Constants ¶
const ( // EPOCH is the base epoch for the Spaceflake generation. First second of 2015, year when I got my username "Krypton" EPOCH = 1420070400000 // MAXWORKERBITS is the maximum value for a 5 bits number MAXWORKERBITS = 15 MAXNODEBITS = 3 // MAXSEQUENCEBITS is the maximum value for a 12 bits number MAXSEQUENCEBITS = 1023 // 4095 // MAX41BITS is the maximum value for a 41 bits number MAX41BITS = 2199023255551 IdPad = 53 NodePad = 2 WorkerPad = 4 SequencePad = 10 TimePad = 37 // 41 )
Variables ¶
This section is empty.
Functions ¶
func DecomposeBinary ¶
DecomposeBinary returns all the parts of the Spaceflake ID in binary format
func ParseNodeID ¶
ParseNodeID returns the node ID from the Spaceflake ID
func ParseSequence ¶
ParseSequence returns the sequence number from the Spaceflake ID
func ParseTime ¶
ParseTime returns the time in milliseconds since the base epoch from the Spaceflake ID
func ParseWorkerID ¶
ParseWorkerID returns the worker ID from the Spaceflake ID
Types ¶
type BulkGeneratorSettings ¶
type BulkGeneratorSettings struct { // Amount is the amount of Spaceflakes to generate Amount int // BaseEpoch is the base epoch that the Spaceflake generator will use BaseEpoch uint64 }
BulkGeneratorSettings is a struct that contains the settings for the bulk Spaceflake generator
func NewBulkGeneratorSettings ¶
func NewBulkGeneratorSettings(amount int) BulkGeneratorSettings
NewBulkGeneratorSettings is used for the settings of the BulkGenerate function below
type GeneratorSettings ¶
type GeneratorSettings struct { // BaseEpoch is the epoch that the Spaceflake generator will use for the first 41 bits BaseEpoch uint64 // NodeID is the node ID that the Spaceflake generator will use for the next 5 bits NodeID uint64 // Sequence is the last 12 bits, usually an incremented number but can be anything. If set to 0, it will be random. Sequence uint64 // WorkerID is the worker ID that the Spaceflake generator will use for the next 5 bits WorkerID uint64 }
GeneratorSettings is a struct that contains the settings for the Spaceflake generator
func NewGeneratorSettings ¶
func NewGeneratorSettings() GeneratorSettings
NewGeneratorSettings is used for the settings of the Generate function below
type Node ¶
type Node struct { // ID is the ID of the node ID uint64 // contains filtered or unexported fields }
Node is a node in the Spaceflake Network that can hold workers
func (*Node) BulkGenerateSpaceflakes ¶
func (n *Node) BulkGenerateSpaceflakes(amount int) ([]*Spaceflake, error)
BulkGenerateSpaceflakes will generate the amount of Spaceflakes specified and auto scale the worker IDs
func (*Node) GetWorkers ¶
GetWorkers returns the list of workers in the node
func (*Node) RemoveWorker ¶
RemoveWorker removes a worker from the node
type Spaceflake ¶
type Spaceflake struct {
// contains filtered or unexported fields
}
Spaceflake represents a Spaceflake
func BulkGenerate ¶
func BulkGenerate(s BulkGeneratorSettings) ([]*Spaceflake, error)
Bulkgenerate will generate the amount of Spaceflakes specified and auto scale the node and worker IDs
func Generate ¶
func Generate(s GeneratorSettings) (*Spaceflake, error)
Generate only a Spaceflake ID when you need to generate one without worker and node objects. Fastest way of creating a Spaceflake.
func GenerateAt ¶
func GenerateAt(s GeneratorSettings, at time.Time) (*Spaceflake, error)
GenerateAt generates a Spaceflake at a specific time
func (*Spaceflake) BinaryID ¶
func (s *Spaceflake) BinaryID() string
BinaryID returns the Spaceflake ID in binary format
func (*Spaceflake) Decompose ¶
func (s *Spaceflake) Decompose() map[string]uint64
Decompose returns all the parts of the Spaceflake
func (*Spaceflake) DecomposeBinary ¶
func (s *Spaceflake) DecomposeBinary() map[string]string
DecomposeBinary returns all the parts of the Spaceflake in binary format
func (*Spaceflake) NodeID ¶
func (s *Spaceflake) NodeID() uint64
NodeID returns the node ID from the Spaceflake
func (*Spaceflake) Sequence ¶
func (s *Spaceflake) Sequence() uint64
Sequence returns the sequence number from the Spaceflake
func (*Spaceflake) StringID ¶
func (s *Spaceflake) StringID() string
StringID returns the Spaceflake ID as a string
func (*Spaceflake) Time ¶
func (s *Spaceflake) Time() uint64
Time returns the time in milliseconds since the base epoch from the Spaceflake
func (*Spaceflake) WorkerID ¶
func (s *Spaceflake) WorkerID() uint64
WorkerID returns the worker ID from the Spaceflake
type Worker ¶
type Worker struct { // Node is the node that the worker belongs to Node *Node // BaseEpoch is the epoch that will be used for the first 41 bits when generating a Spaceflake BaseEpoch uint64 // Sequence is the last 12 bits, usually an incremented number but can be anything. If set to 0, it will be the incremented number. Sequence uint64 // ID is the worker ID that the Spaceflake generator will use for the next 5 bits ID uint64 // contains filtered or unexported fields }
Worker is a worker in the Spaceflake Network that can generate Spaceflakes
func (*Worker) BulkGenerateSpaceflakes ¶
func (w *Worker) BulkGenerateSpaceflakes(amount int) ([]*Spaceflake, error)
BulkGenerateSpaceflakes will generate the amount of Spaceflakes specified and scale nothing, it will wait one millisecond if the sequence is over 4095
func (*Worker) GenerateSpaceflake ¶
func (w *Worker) GenerateSpaceflake() (*Spaceflake, error)
GenerateSpaceflake generates a Spaceflake
func (*Worker) GenerateSpaceflakeAt ¶
func (w *Worker) GenerateSpaceflakeAt(at time.Time) (*Spaceflake, error)
GenerateSpaceflakeAt generates a Spaceflake at a specific time