GrpcConnectionPool.Config (grpc_connection_pool v0.4.0)

Copy Markdown View Source

Configuration module for gRPC connection pooling.

This module provides a unified configuration interface that works with both production gRPC services and local/test environments with custom endpoints.

Configuration Options

  • :endpoint - Connection endpoint configuration
    • :type - Either :production, :custom, or any atom for different environments
    • :host - Hostname (required for custom endpoints)
    • :port - Port number (required for custom endpoints)
    • :ssl - SSL configuration (default: auto-configured based on type)
    • :credentials - Custom gRPC credentials
    • :retry_config - Retry configuration for connection failures
    • :interceptors - List of gRPC client interceptors (modules implementing the interceptor behavior)
  • :pool - Pool configuration
    • :size - Number of connections in pool (default: 5)
    • :name - Pool name (default: auto-generated)
    • :strategy - Channel selection strategy: :round_robin (default), :random, :power_of_two, or a custom module implementing GrpcConnectionPool.Strategy
    • :telemetry_interval - Interval in ms for the periodic :status event (default: 5_000)
    • :telemetry_sample_rate - Per-call :get_channel telemetry: 1 = emit every call (default), 0 = never, N = emit ~1-in-N (default: 1)
  • :connection - Connection-specific settings
    • :keepalive - Keepalive interval in milliseconds (default: 30_000)
    • :health_check - Whether to enable connection health checks (default: true)
    • :ping_interval - Interval to send ping to keep connection warm (default: 25_000)

Examples

Production Configuration

config = %GrpcConnectionPool.Config{
  endpoint: %{
    type: :production,
    host: "api.example.com",
    port: 443,
    ssl: []
  },
  pool: %{
    size: 10,
    name: MyApp.GrpcPool
  }
}

Custom Endpoint Configuration

config = %GrpcConnectionPool.Config{
  endpoint: %{
    type: :test,
    host: "localhost",
    port: 9090
  },
  pool: %{
    size: 3
  }
}

Using Custom Credentials

config = %GrpcConnectionPool.Config{
  endpoint: %{
    type: :production,
    host: "secure-api.example.com", 
    port: 443,
    credentials: GRPC.Credential.new(ssl: [
      verify: :verify_peer,
      cacertfile: "/path/to/ca.pem"
    ])
  }
}

Using Interceptors

config = %GrpcConnectionPool.Config{
  endpoint: %{
    type: :production,
    host: "api.example.com",
    port: 443,
    interceptors: [MyApp.LoggingInterceptor, MyApp.AuthInterceptor]
  }
}

Summary

Functions

Default TLS options for production endpoints: verify the peer certificate against the system CA store and check the hostname.

Creates a pool configuration from application environment variables.

Gets the gRPC connection endpoint from the configuration.

Gets the retry configuration for connection attempts.

Creates a local/test configuration without SSL.

Creates a new pool configuration from keyword options or environment variables.

Creates a production configuration with SSL.

Types

connection_config()

@type connection_config() :: %{
  keepalive: pos_integer(),
  health_check: boolean(),
  ping_interval: pos_integer() | nil,
  suppress_connection_errors: boolean(),
  backoff_min: pos_integer(),
  backoff_max: pos_integer(),
  max_reconnect_attempts: pos_integer()
}

endpoint_config()

@type endpoint_config() :: %{
  type: endpoint_type(),
  host: String.t() | nil,
  port: pos_integer() | nil,
  ssl: keyword() | boolean() | nil,
  credentials: GRPC.Credential.t() | nil,
  retry_config: retry_config() | nil,
  interceptors: [module()] | nil
}

endpoint_type()

@type endpoint_type() :: atom()

pool_config()

@type pool_config() :: %{
  size: pos_integer(),
  name: atom() | nil,
  telemetry_interval: pos_integer(),
  telemetry_sample_rate: non_neg_integer(),
  strategy: atom()
}

retry_config()

@type retry_config() :: %{
  max_attempts: pos_integer(),
  base_delay: pos_integer(),
  max_delay: pos_integer()
}

t()

@type t() :: %GrpcConnectionPool.Config{
  connection: connection_config(),
  endpoint: endpoint_config(),
  pool: pool_config()
}

Functions

default_production_ssl()

@spec default_production_ssl() :: keyword()

Default TLS options for production endpoints: verify the peer certificate against the system CA store and check the hostname.

On OTP 25+ this returns a fully-verifying config. On older OTP (no :public_key.cacerts_get/0) it returns [] and logs a warning, since there is no portable system-CA accessor — set :ssl explicitly in that case.

from_env(app, key \\ GrpcConnectionPool)

@spec from_env(atom(), atom()) :: {:ok, t()} | {:error, String.t()}

Creates a pool configuration from application environment variables.

Looks for configuration under the specified application key:

config :my_app, GrpcConnectionPool,
  endpoint: [type: :production, host: "api.example.com", port: 443],
  pool: [size: 10],
  connection: [keepalive: 45_000]

get_endpoint(config)

@spec get_endpoint(t()) :: {String.t(), pos_integer(), keyword()}

Gets the gRPC connection endpoint from the configuration.

Returns a tuple of {host, port, options} suitable for GRPC.Stub.connect/3.

get_retry_config(config)

@spec get_retry_config(t()) :: retry_config() | nil

Gets the retry configuration for connection attempts.

local(opts \\ [])

@spec local(keyword()) :: {:ok, t()}

Creates a local/test configuration without SSL.

Options

  • :host - Hostname (default: "localhost")
  • :port - Port (default: 9090)
  • :pool_size - Number of connections (default: 3)
  • :pool_name - Pool name (default: auto-generated)

new(opts \\ [])

@spec new(keyword()) :: {:ok, t()} | {:error, String.t()}

Creates a new pool configuration from keyword options or environment variables.

Options

See module documentation for available options.

Examples

# From explicit options
{:ok, config} = GrpcConnectionPool.Config.new([
  endpoint: [type: :test, host: "localhost", port: 9090],
  pool: [size: 8, name: MyApp.CustomPool]
])

# From application environment
{:ok, config} = GrpcConnectionPool.Config.from_env(:my_app)

production(opts \\ [])

@spec production(keyword()) :: {:ok, t()} | {:error, String.t()}

Creates a production configuration with SSL.

Options

  • :host - Hostname (required)
  • :port - Port (default: 443)
  • :pool_size - Number of connections (default: 5)
  • :pool_name - Pool name (default: auto-generated)
  • :ssl - SSL configuration (default: [])