Snakepit.Bridge.Session (snakepit v0.1.2)

Session data structure for centralized session management.

This struct represents a session in the centralized session store, containing all session-related data including programs, metadata, and lifecycle information.

Summary

Functions

Removes a program from the session.

Checks if a session has expired based on its TTL.

Gets metadata from the session.

Gets a program from the session.

Creates a new session with the given ID and options.

Updates session metadata.

Adds or updates a program in the session.

Updates the last_accessed timestamp to the current time.

Validates that a session struct has all required fields and valid data.

Types

t()

@type t() :: %Snakepit.Bridge.Session{
  created_at: integer(),
  id: String.t(),
  last_accessed: integer(),
  last_worker_id: String.t() | nil,
  metadata: map(),
  programs: map(),
  ttl: integer()
}

Functions

delete_program(session, program_id)

@spec delete_program(t(), String.t()) :: t()

Removes a program from the session.

Parameters

  • session - The session to update
  • program_id - The program identifier to remove

Returns

Updated session with the program removed.

expired?(session, current_time \\ nil)

@spec expired?(t(), integer() | nil) :: boolean()

Checks if a session has expired based on its TTL.

Parameters

  • session - The session to check
  • current_time - Optional current time (defaults to current monotonic time)

Returns

true if the session has expired, false otherwise.

get_metadata(session, key, default \\ nil)

@spec get_metadata(t(), term(), term()) :: term()

Gets metadata from the session.

Parameters

  • session - The session to query
  • key - The metadata key
  • default - Default value if key not found

Returns

The metadata value or the default.

get_program(session, program_id)

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

Gets a program from the session.

Parameters

  • session - The session to query
  • program_id - The program identifier

Returns

{:ok, program_data} if found, {:error, :not_found} if not found.

new(id, opts \\ [])

@spec new(
  String.t(),
  keyword()
) :: t()

Creates a new session with the given ID and options.

Parameters

  • id - Unique session identifier
  • opts - Keyword list of options:
    • :ttl - Time-to-live in seconds (default: 3600)
    • :metadata - Initial metadata map (default: %{})
    • :programs - Initial programs map (default: %{})

Examples

iex> Session.new("session_123")
%Session{id: "session_123", programs: %{}, metadata: %{}, ...}

iex> Session.new("session_456", ttl: 7200, metadata: %{user_id: "user_1"})
%Session{id: "session_456", ttl: 7200, metadata: %{user_id: "user_1"}, ...}

put_metadata(session, key, value)

@spec put_metadata(t(), term(), term()) :: t()

Updates session metadata.

Parameters

  • session - The session to update
  • key - The metadata key
  • value - The metadata value

Returns

Updated session with the metadata updated.

put_program(session, program_id, program_data)

@spec put_program(t(), String.t(), term()) :: t()

Adds or updates a program in the session.

Parameters

  • session - The session to update
  • program_id - The program identifier
  • program_data - The program data to store

Returns

Updated session with the program added/updated.

touch(session)

@spec touch(t()) :: t()

Updates the last_accessed timestamp to the current time.

Parameters

  • session - The session to touch

Returns

Updated session with current last_accessed timestamp.

validate(session)

@spec validate(t()) :: :ok | {:error, term()}

Validates that a session struct has all required fields and valid data.

Parameters

  • session - The session to validate

Returns

:ok if valid, {:error, reason} if invalid.