Snakepit.Bridge.SessionStore (Snakepit v0.6.10)
View SourceCentralized session store using ETS for high-performance session management.
This GenServer manages a centralized ETS table for storing session data, providing CRUD operations, TTL-based expiration, and automatic cleanup. The store is designed for high concurrency with optimized ETS settings.
Summary
Functions
Returns a specification to start this module under a supervisor.
Manually triggers cleanup of expired sessions.
Creates a new session with the given ID and options.
Deletes a globally stored program.
Deletes a session by ID.
Retrieves a globally stored program.
Gets a program from a session.
Gets a session by ID, automatically updating the last_accessed timestamp.
Gets statistics about the session store.
Lists all active session IDs.
Checks if a session exists.
Starts the SessionStore GenServer.
Stores a program globally, accessible to any worker.
Stores a program in a session.
Stores worker-session affinity mapping.
Updates a program in a session.
Updates a session using the provided update function.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec cleanup_expired_sessions() :: non_neg_integer()
Manually triggers cleanup of expired sessions.
Returns
The number of sessions that were cleaned up.
@spec cleanup_expired_sessions(GenServer.server()) :: non_neg_integer()
@spec create_session( String.t(), keyword() ) :: {:ok, Snakepit.Bridge.Session.t()} | {:error, term()}
Creates a new session with the given ID and options.
Parameters
session_id- Unique session identifieropts- Keyword list of options passed to Session.new/2
Returns
{:ok, session} if successful, {:error, reason} if failed.
Examples
{:ok, session} = SessionStore.create_session("session_123")
{:ok, session} = SessionStore.create_session("session_456", ttl: 7200)
@spec create_session(GenServer.server(), String.t(), keyword()) :: {:ok, Snakepit.Bridge.Session.t()} | {:error, term()}
@spec delete_global_program(String.t()) :: :ok
Deletes a globally stored program.
Parameters
program_id- The program identifier
Returns
:ok always (idempotent operation).
@spec delete_global_program(GenServer.server(), String.t()) :: :ok
@spec delete_session(String.t()) :: :ok
Deletes a session by ID.
Parameters
session_id- The session identifier
Returns
:ok always (idempotent operation).
@spec delete_session(GenServer.server(), String.t()) :: :ok
Retrieves a globally stored program.
Parameters
program_id- The program identifier
Returns
{:ok, program_data} if found, {:error, :not_found} if not found.
@spec get_global_program(GenServer.server(), String.t()) :: {:ok, map()} | {:error, :not_found}
Gets a program from a session.
@spec get_session(String.t()) :: {:ok, Snakepit.Bridge.Session.t()} | {:error, :not_found}
Gets a session by ID, automatically updating the last_accessed timestamp.
Parameters
session_id- The session identifier
Returns
{:ok, session} if found, {:error, :not_found} if not found.
@spec get_session(GenServer.server(), String.t()) :: {:ok, Snakepit.Bridge.Session.t()} | {:error, :not_found}
@spec get_stats() :: map()
Gets statistics about the session store.
Returns
A map containing various statistics about the session store.
@spec get_stats(GenServer.server()) :: map()
@spec list_sessions() :: [String.t()]
Lists all active session IDs.
Returns
A list of all active session IDs.
@spec list_sessions(GenServer.server()) :: [String.t()]
Checks if a session exists.
Parameters
session_id- The session identifier
Returns
true if the session exists, false otherwise.
@spec session_exists?(GenServer.server(), String.t()) :: boolean()
@spec start_link(keyword()) :: GenServer.on_start()
Starts the SessionStore GenServer.
Options
:name- The name to register the GenServer (default: MODULE):table_name- The ETS table name (default: :snakepit_sessions):cleanup_interval- Cleanup interval in milliseconds (default: 60_000):default_ttl- Default TTL for sessions in seconds (default: 3600)
Stores a program globally, accessible to any worker.
This is used for anonymous operations where programs need to be accessible across different pool workers.
Parameters
program_id- Unique program identifierprogram_data- Program data to store
Returns
:ok if successful, {:error, reason} if failed.
@spec store_global_program(GenServer.server(), String.t(), map()) :: :ok | {:error, term()}
Stores a program in a session.
@spec store_program(GenServer.server(), String.t(), String.t(), map()) :: :ok | {:error, term()}
Stores worker-session affinity mapping.
Updates a program in a session.
@spec update_session(String.t(), (Snakepit.Bridge.Session.t() -> Snakepit.Bridge.Session.t())) :: {:ok, Snakepit.Bridge.Session.t()} | {:error, term()}
Updates a session using the provided update function.
The update function receives the current session and should return the updated session. The operation is atomic.
Parameters
session_id- The session identifierupdate_fn- Function that takes a session and returns an updated session
Returns
{:ok, updated_session} if successful, {:error, reason} if failed.
Examples
{:ok, session} = SessionStore.update_session("session_123", fn session ->
Session.put_program(session, "prog_1", %{data: "example"})
end)
@spec update_session(GenServer.server(), String.t(), (Snakepit.Bridge.Session.t() -> Snakepit.Bridge.Session.t())) :: {:ok, Snakepit.Bridge.Session.t()} | {:error, term()}