snowgleam
A module for generating unique IDs using the Twitter Snowflake algorithm.
Types
The Snowflake ID generator. This is not meant to be used directly but with the provided builder functions.
pub opaque type Generator
Constants
pub const default_epoch: Int
The default epoch for the generator. Corresponds to the Unix epoch.
Functions
pub fn generate(channel: Subject(Message)) -> Int
Generates a new Snowflake ID.
Examples
import gleam/snowgleam
let epoch = 1_420_070_400_000
let worker_id = 12
let assert Ok(generator) =
snowgleam.new_generator()
|> snowgleam.with_epoch(epoch)
|> snowgleam.with_worker_id(worker_id)
|> snowgleam.start()
let id = snowgleam.generate(generator)
pub fn new_generator() -> Generator
Creates a new Snowflake ID generator with default settings.
pub fn start(
generator: Generator,
) -> Result(Subject(Message), String)
Starts the generator.
pub fn timestamp(id: Int, epoch: Int) -> Int
Extracts the timestamp from a Snowflake ID using the provided epoch.
pub fn with_epoch(generator: Generator, epoch: Int) -> Generator
Sets the epoch for the generator.
pub fn with_process_id(
generator: Generator,
process_id: Int,
) -> Generator
Sets the process ID for the generator.
pub fn with_worker_id(
generator: Generator,
worker_id: Int,
) -> Generator
Sets the worker ID for the generator.