Documentation
¶
Overview ¶
Package config provides shared SQL (github.com/alexfalkowski/go-service/v2/database/sql) configuration types for go-service.
This package defines configuration structs that are typically embedded into a larger service configuration and then consumed by driver-specific wiring (for example github.com/alexfalkowski/go-service/v2/database/sql/pg).
Pool configuration ¶
Config models reader and writer Pool values. Each pool owns both its datasource names and its complete PoolSettings, including:
- maximum connection lifetime,
- max open connections, and
- max idle connections.
DSNs and source strings ¶
Reader and writer DSNs are configured through Pool.DSNs. Each DSN URL is expressed as a go-service "source string" (resolved via github.com/alexfalkowski/go-service/v2/os.FS.ReadSource), so it can be:
- "env:NAME" to read from an environment variable,
- "file:/path/to/dsn" to read from a file, or
- any other value treated as a literal DSN.
Enabled SQL configurations must provide at least one writer or reader DSN, and each resolved DSN must be non-empty.
Start with Config, Pool, PoolSettings, and DSN.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Reader is the replica (read-only) datasource pool.
Reader *Pool `yaml:"reader,omitempty" json:"reader,omitempty" toml:"reader,omitempty" validate:"omitempty"`
// Writer is the primary (read-write) datasource pool.
Writer *Pool `yaml:"writer,omitempty" json:"writer,omitempty" toml:"writer,omitempty" validate:"omitempty"`
}
Config contains shared github.com/alexfalkowski/go-service/v2/database/sql connection pool settings.
It is intended to be embedded by driver-specific configuration types (for example PostgreSQL) and consumed by the SQL wiring in this repository.
Config.Reader and Config.Writer contain role-specific pools. Each pool owns its DSNs and its pool settings. DSNs are connection strings expressed as go-service "source strings" (literal values, `file:` paths, or `env:` references) that are resolved by os.FS.ReadSource. Enabled SQL configurations must provide at least one writer or reader DSN, and each resolved DSN must be non-empty.
type DSN ¶
type DSN struct {
// URL is a go-service "source string" for the datasource name/connection string.
//
// It is resolved via [os.FS.ReadSource], so it can be:
// - "env:NAME" to read from an environment variable,
// - "file:/path/to/dsn" to read from a file, or
// - any other value treated as a literal connection string.
URL string `yaml:"url,omitempty" json:"url,omitempty" toml:"url,omitempty"`
}
DSN is a SQL datasource name (connection string) configuration.
type Pool ¶ added in v2.640.0
type Pool struct {
// Settings configures connection pool behavior for this role.
Settings *PoolSettings `yaml:"settings,omitempty" json:"settings,omitempty" toml:"settings,omitempty" validate:"required"`
// DSNs is the set of datasource names for this role.
//
// Each DSN URL is a "source string" resolved via [os.FS.ReadSource], so it can be:
// - "env:NAME" to read the DSN from an environment variable,
// - "file:/path/to/dsn" to read the DSN from a file, or
// - any other value treated as a literal DSN string.
DSNs []DSN `yaml:"dsns,omitempty" json:"dsns,omitempty" toml:"dsns,omitempty"`
}
Pool contains DSNs and connection pool settings for a SQL role.
type PoolSettings ¶ added in v2.640.0
type PoolSettings struct {
// ConnMaxLifetime is the maximum amount of time a connection may be reused.
//
// In config files it is encoded as a Go duration string (for example "30s", "5m", "1h").
ConnMaxLifetime time.Duration `yaml:"conn_max_lifetime,omitempty" json:"conn_max_lifetime,omitempty" toml:"conn_max_lifetime,omitempty" validate:"gte=0"`
// ConnMaxIdleTime is the maximum amount of time a connection may remain idle.
//
// In config files it is encoded as a Go duration string (for example "30s", "5m", "1h").
ConnMaxIdleTime time.Duration `yaml:"conn_max_idle_time,omitempty" json:"conn_max_idle_time,omitempty" toml:"conn_max_idle_time,omitempty" validate:"gte=0"`
// MaxOpenConns is the maximum number of open connections to the database.
MaxOpenConns int `yaml:"max_open_conns" json:"max_open_conns" toml:"max_open_conns" validate:"gt=0"`
// MaxIdleConns is the maximum number of connections in the idle connection pool.
MaxIdleConns int `yaml:"max_idle_conns" json:"max_idle_conns" toml:"max_idle_conns" validate:"gt=0,ltefield=MaxOpenConns"`
}
PoolSettings contains SQL connection pool settings for a role.