View Source Sonyflakex.Generator (Sonyflakex v0.2.0)

Logic to generate new IDs.

Summary

Functions

Returns new ID based on current state.

Functions

Link to this function

next_id(state, utc_now \\ &DateTime.utc_now/0)

View Source
@spec next_id(Sonyflakex.State.t(), (-> DateTime.t())) ::
  {:error, :overflow}
  | {:ok, Sonyflakex.State.sonyflake_id(), Sonyflakex.State.t()}

Returns new ID based on current state.

Args:

  • state: Generator state.
  • utc_now: (Optional) Function that returns current datetime using the same contract as DateTime.utc_now/0. It is used to mock datetime generation in tests.

Returns:

  • {:ok, new_id, new_state}: new ID value and state after generating the ID.

  • {:error, :overflow}: if incrementing the sequence would overflow the 8 bit field then it returns an error response.