# `GrpcConnectionPool.Config`
[🔗](https://github.com/nyo16/grpc_connection_pool/blob/v0.5.3/lib/grpc_connection_pool/config.ex#L1)

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)

> Note: gun's internal reconnect is disabled (`adapter_opts: [retry: 0]`) so the
> pool's own backoff governs reconnection and connects to a dead endpoint fail
> fast instead of stalling. This is not user-configurable.

## 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]
      }
    }

# `connection_config`

```elixir
@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`

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

# `endpoint_type`

```elixir
@type endpoint_type() :: atom()
```

# `pool_config`

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

# `retry_config`

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

# `t`

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

# `default_production_ssl`

```elixir
@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`

```elixir
@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`

```elixir
@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`

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

Gets the retry configuration for connection attempts.

# `local`

```elixir
@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`

```elixir
@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`

```elixir
@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: [])

---

*Consult [api-reference.md](api-reference.md) for complete listing*
