Snakepit.Bridge.SessionStore (Snakepit v0.13.0)

Copy Markdown View Source

Centralized 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.

Gets a session by ID, automatically updating the last_accessed timestamp.

Gets statistics about the session store.

Lists all active session IDs.

Starts the SessionStore GenServer.

Stores worker-session affinity mapping.

Updates a session using the provided update function.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

cleanup_expired_sessions(server \\ __MODULE__)

@spec cleanup_expired_sessions(GenServer.server()) :: non_neg_integer()

Manually triggers cleanup of expired sessions.

Returns

The number of sessions that were cleaned up.

create_session(session_id, opts \\ [])

@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 identifier
  • opts - 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)

create_session(server, session_id, opts)

@spec create_session(GenServer.server(), String.t(), keyword()) ::
  {:ok, Snakepit.Bridge.Session.t()} | {:error, term()}

delete_session(server \\ __MODULE__, session_id)

@spec delete_session(GenServer.server(), String.t()) :: :ok

Deletes a session by ID.

Parameters

  • session_id - The session identifier

Returns

:ok always (idempotent operation).

get_session(server \\ __MODULE__, session_id)

@spec get_session(GenServer.server(), 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.

get_stats(server \\ __MODULE__)

@spec get_stats(GenServer.server()) :: map()

Gets statistics about the session store.

Returns

A map containing various statistics about the session store.

list_sessions(server \\ __MODULE__)

@spec list_sessions(GenServer.server()) :: [String.t()]

Lists all active session IDs.

Returns

A list of all active session IDs.

session_exists?(server \\ __MODULE__, session_id)

@spec session_exists?(GenServer.server(), String.t()) :: boolean()

Checks if a session exists.

Parameters

  • session_id - The session identifier

Returns

true if the session exists, false otherwise.

start_link(opts \\ [])

@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)

store_worker_session(session_id, worker_id)

@spec store_worker_session(String.t(), term()) :: :ok | {:error, term()}

Stores worker-session affinity mapping.

store_worker_session(server, session_id, worker_id)

@spec store_worker_session(GenServer.server(), String.t(), term()) ::
  :ok | {:error, term()}

update_session(server \\ __MODULE__, session_id, update_fn)

@spec update_session(GenServer.server(), 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 identifier
  • update_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 ->
  Map.put(session, :data, %{key: "value"})
end)