Foundation.Infrastructure.PoolWorkers.HttpWorker (foundation v0.1.0)

Sample HTTP connection pool worker for demonstrating connection pooling patterns.

This worker maintains persistent HTTP connections and provides a reusable template for implementing custom pool workers for different resource types.

Usage

# Start pool with HTTP workers
ConnectionManager.start_pool(:http_pool, [
  size: 10,
  max_overflow: 5,
  worker_module: Foundation.Infrastructure.PoolWorkers.HttpWorker,
  worker_args: [base_url: "https://api.example.com", timeout: 30_000]
])

# Use pooled connection
ConnectionManager.with_connection(:http_pool, fn worker ->
  HttpWorker.get(worker, "/users/123")
end)

Worker Configuration

  • :base_url - Base URL for HTTP requests
  • :timeout - Request timeout in milliseconds
  • :headers - Default headers for all requests
  • :max_redirects - Maximum number of redirects to follow

Summary

Functions

Returns a specification to start this module under a supervisor.

Performs a GET request using the pooled worker.

Gets the current status and configuration of the worker.

Performs a POST request using the pooled worker.

Starts an HTTP worker with the given configuration.

Types

worker_config()

@type worker_config() :: [
  base_url: String.t(),
  timeout: timeout(),
  headers: [{String.t(), String.t()}],
  max_redirects: non_neg_integer()
]

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get(worker, path, options \\ [])

@spec get(pid(), String.t(), keyword()) :: {:ok, map()} | {:error, term()}

Performs a GET request using the pooled worker.

Parameters

  • worker - Worker PID from the pool
  • path - Request path (relative to base_url)
  • options - Request options (headers, params, etc.)

Returns

  • {:ok, response} - Request successful
  • {:error, reason} - Request failed

get_status(worker)

@spec get_status(pid()) :: {:ok, map()}

Gets the current status and configuration of the worker.

Parameters

  • worker - Worker PID from the pool

Returns

  • {:ok, status} - Worker status information

post(worker, path, body, options \\ [])

@spec post(pid(), String.t(), term(), keyword()) :: {:ok, map()} | {:error, term()}

Performs a POST request using the pooled worker.

Parameters

  • worker - Worker PID from the pool
  • path - Request path (relative to base_url)
  • body - Request body (will be JSON encoded)
  • options - Request options (headers, etc.)

Returns

  • {:ok, response} - Request successful
  • {:error, reason} - Request failed

start_link(config)

@spec start_link(worker_config()) :: GenServer.on_start()

Starts an HTTP worker with the given configuration.

This function is called by Poolboy to create worker instances.