database

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ServerMaxIdleConnsMultiplier = 4
	ServerMaxOpenConnsMultiplier = 16
	ServerMinIdleConns           = 25
	ServerMinOpenConns           = 100
	ServerConnMaxIdleTime        = 5 * time.Minute
	ServerConnMaxLifetime        = 30 * time.Minute
)

Server-dialect (Postgres/MySQL) pool defaults. Sized relative to GOMAXPROCS with a generous floor so a busy service is not starved on small core counts.

View Source
const (
	SQLiteFileMaxOpenConns    = 4
	SQLiteFileMaxIdleConns    = 4
	SQLiteFileConnMaxIdleTime = 5 * time.Minute
	SQLiteFileConnMaxLifetime = 30 * time.Minute
)

SQLite file-mode pool defaults. SQLite is effectively single-writer, so a large server-sized pool only burns file descriptors and amplifies SQLITE_BUSY contention. The cap is kept small but strictly above one: the schema inspector (and any nested-query code path) opens a second statement while a result set is still streaming, which would deadlock against a single-connection pool. WAL mode (enabled by the SQLite provider for files) lets the remaining headroom serve concurrent readers.

Variables

This section is empty.

Functions

func LogVersion added in v0.27.0

func LogVersion(ctx context.Context, kind config.DBKind, db *sql.DB, logger logx.Logger) error

LogVersion resolves the provider for kind, logs the connected server version, and emits a ready line. It no-ops when kind has no registered provider. The orm data source start hook calls it so version introspection stays inside the database layer.

func Open added in v0.27.0

func Open(cfg config.DataSourceConfig) (*sql.DB, error)

Open establishes a connection to the configured data source and returns the raw *sql.DB with a dialect-appropriate connection pool applied (see poolConfigFor — file-mode SQLite gets a small pool, server dialects the large one). Building an ORM handle on top of it (bun.DB, dialect, query hooks) is the caller's concern — see internal/orm.

func SupportsKind added in v0.27.0

func SupportsKind(kind config.DBKind) bool

SupportsKind reports whether a connection provider is registered for kind. It lets higher layers (e.g. orm.DialectFor) assert that the connector and dialect halves of a dialect agree on the same set of supported kinds.

func Version added in v0.27.0

func Version(ctx context.Context, kind config.DBKind, db *sql.DB) (string, error)

Version resolves the provider for kind and returns the connected server's version string. It returns an unsupported-kind error when no provider is registered for kind.

Types

type ConnectionPoolConfig

type ConnectionPoolConfig struct {
	MaxIdleConns    int
	MaxOpenConns    int
	ConnMaxIdleTime time.Duration
	ConnMaxLifetime time.Duration
}

ConnectionPoolConfig holds the database/sql pool knobs applied to a freshly opened *sql.DB. Profiles are dialect-aware (see poolConfigFor): server dialects get a large concurrent pool, while SQLite — effectively single-writer — is capped low to avoid burning file descriptors and amplifying SQLITE_BUSY contention.

func (ConnectionPoolConfig) ApplyToDB

func (c ConnectionPoolConfig) ApplyToDB(db interface {
	SetMaxIdleConns(int)
	SetMaxOpenConns(int)
	SetConnMaxIdleTime(time.Duration)
	SetConnMaxLifetime(time.Duration)
},
)

type Provider added in v0.27.0

type Provider interface {
	// Connect establishes a database connection and returns the *sql.DB and any
	// error. On success (nil error) the returned *sql.DB MUST be non-nil — Open
	// applies the connection pool to it without a nil check.
	Connect(config *config.DataSourceConfig) (*sql.DB, error)
	// Kind returns the database kind this provider handles (postgres, mysql, or sqlite).
	Kind() config.DBKind
	// Version queries and returns the database server version string.
	Version(ctx context.Context, db *sql.DB) (string, error)
}

Provider defines the contract for database-specific connection logic.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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