Documentation ¶
Overview ¶
Package flaki provides the implementation of Flaki - Das kleine Generator. Flaki is a distributed unique IDs generator inspired by Snowflake (https://github.com/twitter/snowflake). It returns unique IDs of type uint64 or string. The ID is composed of: 5-bit component ID, 2-bit node ID, 15-bit sequence number, and 42-bit time's milliseconds since the epoch. Unique IDs will be generated until 139 years 4 months and a few days after the epoch. After that, there will be an overflow and the newly generated IDs won't be unique anymore.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Flaki ¶
type Flaki struct {
// contains filtered or unexported fields
}
Flaki is the unique ID generator.
func New ¶
New returns a new unique IDs generator.
If you do not specify options, Flaki will use the following default parameters: 0 for the node ID, 0 for the component ID, and 01.01.2017 as start epoch.
To change the default settings, use the options in the call to New, i.e. New(logger, ComponentID(1), NodeID(2), StartEpoch(startEpoch))
func (*Flaki) NextID ¶
NextID returns a new unique ID, or an error if the clock moves backward.
Example ¶
This example demonstrates the use of NextID to obtain a unique uint64 ID.
flaki, err := New() if err != nil { log.Fatalf("could not create flaki generator: %v", err) } id, err := flaki.NextID() if err != nil { log.Fatalf("could not generate ID: %v", err) } fmt.Printf("Unique ID: %d", id)
Output:
func (*Flaki) NextIDString ¶
NextIDString returns the NextID as a string.
Example ¶
This example demonstrates the use of NextIDString to obtain a unique string ID.
flaki, err := New() if err != nil { log.Fatalf("could not create flaki generator: %v", err) } id, err := flaki.NextIDString() if err != nil { log.Fatalf("could not generate ID: %v", err) } fmt.Printf("Unique ID: %s", id)
Output:
func (*Flaki) NextValidID ¶
NextValidID always returns a new unique ID, it never returns an error. If the clock moves backward, it waits until the situation goes back to normal and then returns the valid ID.
Example ¶
This example demonstrates the use of NextID to obtain a unique uint64 ID.
flaki, err := New() if err != nil { log.Fatalf("could not create flaki generator: %v", err) } id := flaki.NextValidID() if err != nil { log.Fatalf("could not generate ID: %v", err) } fmt.Printf("Unique ID: %d", id)
Output:
func (*Flaki) NextValidIDString ¶
NextValidIDString returns the NextValidID as a string.
Example ¶
This example demonstrates the use of NextIDString to obtain a unique string ID.
flaki, err := New() if err != nil { log.Fatalf("could not create flaki generator: %v", err) } id := flaki.NextValidIDString() if err != nil { log.Fatalf("could not generate ID: %v", err) } fmt.Printf("Unique ID: %s", id)
Output:
type Option ¶
Option type is use to configure the Flaki generator. It takes one argument: the Flaki we are operating on.
func ComponentID ¶
ComponentID is the option used to set the component ID.
func StartEpoch ¶
StartEpoch is the option used to set the epoch.