Documentation
¶
Index ¶
- Constants
- Variables
- func DecodeString(s string) (int64, error)
- func EncodeString(id int64) string
- type Generator
- func (g *Generator) Generate() (int64, error)
- func (g *Generator) GenerateString() (string, error)
- func (g *Generator) Inspect(id int64) (timestamp int64, nodeID int64, sequence int64, err error)
- func (g *Generator) InspectString(id string) (timestamp int64, nodeID int64, sequence int64, err error)
- func (g *Generator) UpdateLease(leaseStart, leaseEnd int64) bool
Constants ¶
const ( // Sunday, October 27, 2024 3:33:20 AM UTC RANDFLAKE_EPOCH_OFFSET = 1730000000 // 30 bits for timestamp (lifetime of 34 years) RANDFLAKE_TIMESTAMP_BITS = 30 // 17 bits for node id (max 131072 nodes) RANDFLAKE_NODE_BITS = 17 // 17 bits for sequence (max 131072 sequences) RANDFLAKE_SEQUENCE_BITS = 17 // Tuesday, November 5, 2058 5:10:23 PM UTC RANDFLAKE_MAX_TIMESTAMP = RANDFLAKE_EPOCH_OFFSET + 1<<RANDFLAKE_TIMESTAMP_BITS - 1 // 131071 nodes RANDFLAKE_MAX_NODE = 1<<RANDFLAKE_NODE_BITS - 1 // 131071 sequences RANDFLAKE_MAX_SEQUENCE = 1<<RANDFLAKE_SEQUENCE_BITS - 1 )
Variables ¶
var ( ErrRandflakeDead = errors.New("randflake: the randflake id is dead after 34 years of lifetime") ErrInvalidSecret = errors.New("randflake: invalid secret, secret must be 16 bytes long") ErrInvalidLease = errors.New("randflake: invalid lease, lease expired or not started yet") ErrInvalidNode = errors.New("randflake: invalid node id, node id must be between 0 and 131071") ErrResourceExhausted = errors.New("randflake: resource exhausted (generator can't handle current throughput, try using multiple randflake instances)") ErrConsistencyViolation = errors.New("randflake: timestamp consistency violation, the current time is less than the last time") ErrInvalidID = errors.New("randflake: invalid id") )
Functions ¶
func DecodeString ¶ added in v1.6.1
func EncodeString ¶ added in v1.6.1
Types ¶
type Generator ¶
type Generator struct { // TimeSource is a function that returns the current time in seconds since the epoch. // If TimeSource is nil, time.Now().Unix() will be used. TimeSource func() int64 // contains filtered or unexported fields }
func NewGenerator ¶
func NewGenerator(nodeID int64, leaseStart int64, leaseEnd int64, secret []byte) (*Generator, error)
NewGenerator creates a new randflake generator.
nodeID is the node ID of the randflake generator. (must be unique in the cluster in a specific lease interval) leaseStart is the start time of the lease in seconds since the epoch. leaseEnd is the end time of the lease in seconds since the epoch. secret is the secret used to generate the randflake id. (must be 16 bytes long)
func (*Generator) GenerateString ¶ added in v1.6.1
GenerateString generates a unique, encrypted ID and returns it as a string.
func (*Generator) Inspect ¶
Inspect returns the timestamp, node ID, and sequence number of the given ID.
func (*Generator) InspectString ¶ added in v1.6.1
func (g *Generator) InspectString(id string) (timestamp int64, nodeID int64, sequence int64, err error)
InspectString returns the timestamp, node ID, and sequence number of the given ID.
func (*Generator) UpdateLease ¶
UpdateLease updates the lease end time and returns true if the lease was updated.
the leaseStart must equal to the leaseStart of the generator. the leaseEnd must be greater than the leaseStart. the leaseEnd must be less than or equal to the maximum timestamp (2058-11-05 17:10:23 UTC). the leaseEnd must be greater than the current leaseEnd.