View Source Flake (Flake v0.1.0)

Public-facing API for starting and generating flake ID's.

Summary

Functions

Given a flake ID, return a map that breaks down the different components of the flake ID.

Generates a 64-bit unsigned integer flake ID given a worker ID.

Creates worker processes and assigns the application's machine ID to all workers.

Creates worker processes and assigns the application's machine ID to all workers.

Types

@type flake_id() :: 0..18_446_744_073_709_551_615
@type machine_id() :: 0..255
@type worker_id() :: 0..63

Functions

Link to this function

get_flake_components(flake)

View Source
@spec get_flake_components({:ok, flake_id()} | flake_id()) :: results
when results: %{
       time: 0..17_179_869_183,
       machine_id: machine_id(),
       worker_id: worker_id(),
       counter: 0..65535
     }

Given a flake ID, return a map that breaks down the different components of the flake ID.

This is particularly useful for troubleshooting or for use in automated tests.

Examples

iex> Flake.get_flake_components(1846405654575644672)
%{time: 1719599268, machine_id: 1, worker_id: 1, counter: 0}
@spec get_id(worker_id()) :: {:ok, flake_id()} | {:error, error}
when error: :potential_duplicate_id | :invalid_worker_id

Generates a 64-bit unsigned integer flake ID given a worker ID.

Worker ID must be within the range of 0..(N-1), where N is the total number of workers passed in to the start/1 or start/2 functions.

For best performance, try to evenly distribute calls to flake worker processes from different calling processes.

@spec start(machine_id()) :: :ok | {:error, error}
when error: :machine_id | :already_started

Creates worker processes and assigns the application's machine ID to all workers.

The number of worker processes defaults to the number of BEAM schedulers, or 64, whichever is less.

This must be called before generating a flake ID.

Link to this function

start(machine_id, workers)

View Source
@spec start(machine_id(), workers) :: :ok | {:error, error}
when workers: 1..64, error: :machine_id | :invalid_workers | :already_started

Creates worker processes and assigns the application's machine ID to all workers.

This must be called before generating a flake ID.