SnmpKit.SnmpMgr.RequestIdGenerator (snmpkit v2.0.1)

Atomic request ID generator using ETS for thread-safe counter operations.

Provides unique request IDs for SNMP operations without requiring GenServer synchronization, eliminating serialization bottlenecks.

Summary

Functions

Returns a specification to start this module under a supervisor.

Gets the current counter value without incrementing.

Like next_id/0, but returns {:error, :services_not_started} instead of raising when the generator is not running. There is no fallback id: a random one could collide with a request already in flight.

Generates the next unique request ID.

Resets the counter to 0.

Starts the RequestIdGenerator GenServer.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

current_value()

Gets the current counter value without incrementing.

Useful for debugging and monitoring.

fetch_next_id()

@spec fetch_next_id() :: {:ok, pos_integer()} | {:error, :services_not_started}

Like next_id/0, but returns {:error, :services_not_started} instead of raising when the generator is not running. There is no fallback id: a random one could collide with a request already in flight.

next_id()

@spec next_id() :: pos_integer()

Generates the next unique request ID.

Uses atomic ETS operations for thread-safe counter increment. Wraps around at 1000000; SnmpKit.SnmpMgr.Engine refuses to register an id that is still pending, so a wrapped id is never reused while its earlier request is in flight.

Raises ArgumentError when the generator is not running; fetch_next_id/0 is the non-raising form.

Examples

iex> SnmpKit.SnmpMgr.RequestIdGenerator.next_id()
1

iex> SnmpKit.SnmpMgr.RequestIdGenerator.next_id()
2

reset()

Resets the counter to 0.

Primarily used for testing.

start_link(opts \\ [])

Starts the RequestIdGenerator GenServer.

Creates the ETS table for atomic counter operations.