GitHub

SnowflakeGenerator

API reference for SnowflakeGenerator.

Makes unique snowflakes for one machine. Within a millisecond the sequence counts up; after 4096 the generator borrows the next millisecond, and a clock behind it (gone back, or behind the borrowed milliseconds) keeps counting on the last one.

Functions

__init__

__init__(clock, machine_id) -> None
ParameterTypeDescription
clockClockService
machine_idMachineId
def __init__(self, clock: ClockService, machine_id: MachineId) -> None:
    self._clock = clock
    # Runs on other threads may share a generator: each snowflake is
    # counted out holding this, so none is handed out twice.
    self._lock = threading.Lock()
    self._machine_id = machine_id
    self._sequence = 0
    self._sequence_at = self._now()

set_machine_id

set_machine_id(machine_id) -> Effect[None]
ParameterTypeDescription
machine_idMachineId

Returns Effect[None]

def set_machine_id(self, machine_id: MachineId) -> Effect[None]:
    def run() -> None:
        self._machine_id = machine_id

    return sync(run)

next_unsafe

next_unsafe() -> Snowflake

Returns Snowflake

def next_unsafe(self) -> Snowflake:
    with self._lock:
        return self._next()