Phoenix.SessionProcess.ProcessSupervisor (phoenix_session_process v1.0.1)

Copy Markdown View Source

Dynamic supervisor for managing individual session processes.

This supervisor is responsible for the lifecycle management of all session processes. It handles starting, stopping, and monitoring session processes dynamically as they are created and destroyed during application runtime.

Key Responsibilities

Process Management

  • Dynamically starts session processes on demand
  • Terminates session processes when requested or when they expire
  • Tracks all active session processes
  • Provides process lookup by session ID

Registry Integration

  • Registers each session process in Phoenix.SessionProcess.Registry
  • Ensures unique session ID to process mappings
  • Enables fast process lookup for communication

Telemetry Integration

  • Emits telemetry events for all operations
  • Tracks session lifecycle metrics
  • Monitors performance and error rates

Public API

This module provides the main internal API for session process management:

Session Lifecycle

Process Communication

  • call_session/4 - GenServer call to session process
  • cast_session/3 - GenServer cast to session process

Administrative Functions

Process Strategy

Uses :one_for_one strategy which means:

  • If a session process crashes, only that process is restarted
  • Other session processes continue unaffected
  • Provides isolation between user sessions

Error Handling

All operations include comprehensive error handling:

  • Invalid session IDs are rejected
  • Session limits are enforced
  • Process failures are logged and monitored
  • Telemetry events are emitted for debugging

Performance Considerations

  • Registry provides O(1) session lookups
  • Dynamic supervisor scales to thousands of sessions
  • Each session process is isolated and lightweight
  • Memory usage scales linearly with session count

Example Usage

This module is typically used internally by the main Phoenix.SessionProcess API, but can also be used directly for advanced scenarios:

# Start a session process
{:ok, pid} = ProcessSupervisor.start_session("user_123")

# Check if session exists
started? = ProcessSupervisor.session_process_started?("user_123")

# Terminate a session
:ok = ProcessSupervisor.terminate_session("user_123")

Summary

Functions

Returns a specification to start this module under a supervisor.

Initializes the dynamic supervisor.

Checks if a session process has been started for the given session ID.

Starts a child process using the dynamic supervisor.

Starts the process supervisor.

Starts a session process using the default configured module.

Starts a session process with options.

Terminates a child process.

Terminates a session process for the given session ID.

Functions

call_on_session(session_id, request, timeout \\ 15000)

cast_on_session(session_id, request)

child_name(session_id)

@spec child_name(binary()) ::
  {:via, Registry, {Phoenix.SessionProcess.Registry, binary()}}

child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

init(init_arg)

Initializes the dynamic supervisor.

Parameters

  • _init_arg - Initialization arguments (unused)

Returns

  • {:ok, supervisor_config} - Supervisor initialized successfully

session_process_pid(session_id)

@spec session_process_pid(binary()) :: nil | pid()

session_process_started?(session_id)

@spec session_process_started?(binary()) :: boolean()

Checks if a session process has been started for the given session ID.

Parameters

  • session_id - The session ID to check

Returns

  • boolean() - true if the session process exists, false otherwise

start_child(worker, worker_arg)

Starts a child process using the dynamic supervisor.

This is a generic function that can start any child process under this supervisor. It's primarily used internally for starting session processes.

Parameters

  • worker - The module to start as a child process
  • worker_arg - Arguments to pass to the child process

Returns

  • {:ok, pid()} - Child process started successfully
  • {:error, reason} - Failed to start child process

start_link(init_arg)

@spec start_link(any()) :: :ignore | {:error, any()} | {:ok, pid()}

Starts the process supervisor.

This function is typically called by the top-level supervisor when starting the Phoenix.SessionProcess system.

Parameters

  • init_arg - Initialization arguments (unused, kept for compatibility)

Returns

  • {:ok, pid()} - Supervisor started successfully
  • {:error, reason} - Failed to start supervisor
  • :ignore - Supervisor should be ignored

start_session(session_id)

Starts a session process using the default configured module.

Parameters

  • session_id - Unique identifier for the session

Returns

  • {:ok, pid()} - Session process started successfully
  • {:error, reason} - Failed to start session process

start_session(session_id, opts)

Starts a session process with options.

Parameters

  • session_id - Unique identifier for the session
  • opts - Keyword list of options:
    • :module - Session process module to use (defaults to configured module)
    • :args - Initialization arguments passed to init/1 (defaults to nil)

Returns

  • {:ok, pid()} - Session process started successfully
  • {:error, reason} - Failed to start session process

Examples

# Use default module with no args
start_session("session_123")

# Use custom module with default args
start_session("session_123", module: MyApp.SessionProcess)

# Use custom module with custom args
start_session("session_123", module: MyApp.SessionProcess, args: %{user_id: 123})

# Use default module with custom args
start_session("session_123", args: %{user_id: 123})

terminate_child(pid)

Terminates a child process.

Parameters

  • pid - The PID of the child process to terminate

Returns

  • :ok - Child process terminated successfully
  • {:error, reason} - Failed to terminate child process

terminate_session(session_id)

@spec terminate_session(binary()) :: :ok | {:error, :not_found}

Terminates a session process for the given session ID.

This function gracefully shuts down the session process and emits telemetry events for monitoring and debugging purposes.

Parameters

  • session_id - The session ID whose process should be terminated

Returns

  • :ok - Session process terminated successfully
  • {:error, :not_found} - Session process not found

Side Effects

  • Emits telemetry event for session termination
  • Removes process from registry
  • Cancels scheduled cleanup and removes activity tracking
  • Logs the termination operation