pool

package
v1.97.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	DSN      string
	ReadOnly bool
}

Connection represents a single database connection configuration.

type Option

type Option func(*Options)

Option configures database connection settings.

func WithConnection

func WithConnection(dsn string, readOnly bool) Option

WithConnection returns an Option to configure the database connection dsn. Multiple calls with the same DSN but different readOnly flags are supported.

func WithConnections

func WithConnections(connections []Connection) Option

WithConnections returns an Option to configure database connections from a slice. Supports adding multiple connections including the same DSN with different readOnly flags.

func WithDialectAdapter added in v1.94.8

func WithDialectAdapter(adapter dialect.DialectAdapter) Option

WithDialectAdapter sets the database driver adapter for this pool. When omitted, the pool uses the Postgres adapter.

func WithInsertBatchSize added in v1.63.3

func WithInsertBatchSize(insertBatchSize int) Option

WithInsertBatchSize returns an Option to configure the database connection insert batch size.

func WithMaxIdle deprecated

func WithMaxIdle(_ int) Option

WithMaxIdle is intentionally a no-op. The dialect adapter forces MaxIdleConns=0 on the sql.DB so every connection release flows through the per-acquire / per-release hook chain (which the tenancy provider relies on for session-state cleanup). The option is kept to avoid breaking callers that still set it.

Deprecated: setting MaxIdle has no effect.

func WithMaxLifetime

func WithMaxLifetime(maxLifetime time.Duration) Option

WithMaxLifetime returns an Option to configure the database connection max lifetime.

func WithMaxOpen

func WithMaxOpen(maxOpen int) Option

WithMaxOpen returns an Option to configure the database connection max open connections.

func WithPreferSimpleProtocol

func WithPreferSimpleProtocol(preferSimpleProtocol bool) Option

WithPreferSimpleProtocol returns an Option to configure the database connection prefer simple protocol.

func WithPreparedStatements added in v1.64.2

func WithPreparedStatements(enabled bool) Option

WithPreparedStatements returns an Option to enable or disable the prepared statement cache.

func WithSkipDefaultTransaction

func WithSkipDefaultTransaction(skipDefaultTransaction bool) Option

WithSkipDefaultTransaction returns an Option to configure the database connection skip default transaction.

func WithTenancyProvider added in v1.94.8

func WithTenancyProvider(prov tenancy.Provider) Option

WithTenancyProvider sets the tenancy provider for this pool. When omitted, the pool uses the Postgres-RLS provider. Pass nil to disable tenancy enforcement (useful in unit tests that want raw database access).

func WithTraceConfig

func WithTraceConfig(traceConfig config.ConfigurationDatabaseTracing) Option

WithTraceConfig returns an Option to configure the database connection trace config.

type Options

type Options struct {
	Connections []Connection

	MaxOpen     int
	MaxLifetime time.Duration

	PreferSimpleProtocol   bool
	SkipDefaultTransaction bool

	TraceConfig     config.ConfigurationDatabaseTracing
	InsertBatchSize int

	PreparedStatements bool

	// DialectAdapter and TenancyProvider are resolved by NewPool with
	// Postgres defaults when unset. TenancyProviderSet distinguishes
	// "use default" from "explicitly disabled (nil)".
	DialectAdapter     dialect.DialectAdapter
	TenancyProvider    tenancy.Provider
	TenancyProviderSet bool
}

Options holds Datastore connection configuration.

type Pool

type Pool interface {
	// DB returns a *gorm.DB routed to a writable (readOnly=false) or
	// read-only (readOnly=true) connection. The returned session has
	// tenancy applied at the connection level — callers do not need
	// to filter by tenant_id or partition_id explicitly.
	DB(ctx context.Context, readOnly bool) *gorm.DB

	// AddConnection opens a new physical connection and adds it to the
	// pool. May be called multiple times for read/write replication.
	AddConnection(ctx context.Context, opts ...Option) error

	// CanMigrate reports whether this pool was constructed in a mode
	// that permits running migrations.
	CanMigrate() bool

	// SaveMigration records the supplied patches in the migrations
	// metadata table without applying them.
	SaveMigration(ctx context.Context, migrationPatches ...*migration.Patch) error

	// Migrate finds missing migrations, applies them, and installs
	// tenancy enforcement (via the configured tenancy.Provider) on
	// every enrolled model.
	Migrate(ctx context.Context, migrationsDirPath string, migrations ...any) error

	// Close gracefully shuts down all opened connections.
	Close(ctx context.Context)
}

Pool is the minimal connection-pool surface. Tenancy enforcement is applied transparently by the dialect adapter + tenancy provider composed via pool options; the Pool itself exposes only routing, migration, and lifecycle methods. Multi-statement atomicity is caller-driven via gorm's db.Transaction(fn).

func NewPool

func NewPool(_ context.Context, opts ...Option) Pool

NewPool constructs a pool. Options may set the dialect adapter and tenancy provider; defaults are Postgres + Postgres-RLS. The provider is wired into the adapter immediately, so hooks attach to every subsequently-opened connection.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL