driver

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package driver provides low-level SQL driver registration and connection helpers for go-service.

This package is the bridge between driver-specific packages (for example github.com/alexfalkowski/go-service/v3/database/sql/pg) and the shared SQL wiring used by the higher-level module graph.

Its main responsibilities are:

  • Register, which wraps a concrete database/sql/driver.Driver with OpenTelemetry instrumentation and installs it in the global database/sql driver registry.
  • Open, which resolves DSNs from go-service source strings, creates writer/reader pools, applies each role's pool settings, registers DB stats metrics, and closes those pools on lifecycle stop.

Pool creation follows the database/sql.Open contract: it may not establish a network connection immediately. Use github.com/alexfalkowski/go-service/v3/database/sql/driver.DBs.Ping, github.com/alexfalkowski/go-service/v3/database/sql/driver.DBs.PingWriter, github.com/alexfalkowski/go-service/v3/database/sql/driver.DBs.PingReader with a deadline, or a health checker when startup/readiness must verify database reachability.

Most applications use this package indirectly through a driver package such as github.com/alexfalkowski/go-service/v3/database/sql/pg rather than calling it directly.

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyDSN = errors.New("driver: empty database DSN")

ErrEmptyDSN is returned when a configured DSN source resolves to an empty string.

View Source
var ErrNoDSNs = errors.New("driver: no database DSNs configured")

ErrNoDSNs is returned when SQL configuration enables a driver without any writer or reader DSNs.

View Source
var ErrSkip = driver.ErrSkip

ErrSkip aliases database/sql/driver.ErrSkip.

Functions

func Register

func Register(name string, driver Driver, opts ...telemetry.Option) (err error)

Register registers a database/sql driver under name.

This function registers the driver with the global database/sql driver registry. It is therefore intended to be called during process initialization (for example from an init hook or DI registration).

Telemetry:

Errors:

  • If the underlying sql.Register panics (for example, due to registering the same name more than once), Register converts that panic into an error and returns it.

Types

type Conn

type Conn = driver.Conn

Conn aliases database/sql/driver.Conn.

type DBs

type DBs struct {
	// contains filtered or unexported fields
}

DBs contains writer and reader SQL connection pools.

Reader returns a random reader pool when readers are configured and falls back to a random writer pool otherwise. Writer returns a random writer pool.

func Connect

func Connect(name string, fs *os.FS, cfg *config.Config, opts ...telemetry.Option) (*DBs, error)

Connect opens writer/reader github.com/alexfalkowski/go-service/v3/database/sql connection pools for a previously registered driver name.

It resolves DSNs from cfg using the provided filesystem (DSNs are configured as go-service "source strings"), opens writer and reader pools, registers OpenTelemetry DB stats metrics for each pool when metrics are enabled, and then applies each role's pool settings.

Like database/sql.Open, this creates pool handles but does not ping the database or verify network reachability. Call DBs.Ping, DBs.PingWriter, DBs.PingReader with a deadline, or a health checker when startup/readiness must prove connectivity.

Preconditions:

  • cfg must be non-nil and already treated as enabled/validated by the caller.

Failure behavior:

  • returns errors encountered while resolving DSNs or creating pool handles,
  • returns ErrEmptyDSN when any configured DSN source resolves to empty bytes, and
  • returns ErrNoDSNs when neither writer nor reader DSNs are configured.

The returned DBs owns repository lifecycle cleanup such as DB stats metric unregistration.

func ConnectWritersReaders

func ConnectWritersReaders(name string, writerDSNs, readerDSNs []string) (*DBs, []error)

ConnectWritersReaders opens writer/reader github.com/alexfalkowski/go-service/v3/database/sql connection pools for a previously registered driver name.

It is a low-level helper for callers that already have resolved literal DSNs. It creates pool handles with database/sql.Open and does not ping the databases.

func Open

func Open(lc signal.Lifecycle, name string, fs *os.FS, cfg *config.Config, opts ...telemetry.Option) (*DBs, error)

Open opens writer/reader github.com/alexfalkowski/go-service/v3/database/sql connection pools for a previously registered driver name.

Open delegates the connection work to Connect and then appends an OnStop hook to the provided lifecycle that closes all returned pools by calling DBs.Destroy.

Preconditions:

The returned type is the same go-service DBs wrapper returned by Connect.

func (*DBs) Destroy

func (d *DBs) Destroy() error

Destroy unregisters repository-owned DB stats metrics and closes all database pools.

func (*DBs) DriverName

func (d *DBs) DriverName() string

DriverName returns the registered database/sql driver name.

func (*DBs) Ping

func (d *DBs) Ping(ctx context.Context) error

Ping pings all writer and reader pools with ctx.

func (*DBs) PingReader

func (d *DBs) PingReader(ctx context.Context) error

PingReader pings all reader pools with ctx.

func (*DBs) PingWriter

func (d *DBs) PingWriter(ctx context.Context) error

PingWriter pings all writer pools with ctx.

func (*DBs) Reader

func (d *DBs) Reader() (*sql.DB, error)

Reader returns a database pool suitable for read queries.

func (*DBs) Readers

func (d *DBs) Readers() []*sql.DB

Readers returns all reader pools.

func (*DBs) Writer

func (d *DBs) Writer() (*sql.DB, error)

Writer returns a database pool suitable for writes and transactions.

func (*DBs) Writers

func (d *DBs) Writers() []*sql.DB

Writers returns all writer pools.

type Driver

type Driver = driver.Driver

Driver aliases database/sql/driver.Driver.

It is the concrete driver type expected by Register.

type NamedValue

type NamedValue = driver.NamedValue

NamedValue aliases database/sql/driver.NamedValue.

type Rows

type Rows = driver.Rows

Rows aliases database/sql/driver.Rows.

type Stmt

type Stmt = driver.Stmt

Stmt aliases database/sql/driver.Stmt.

type Tx

type Tx = driver.Tx

Tx aliases database/sql/driver.Tx.

type Value

type Value = driver.Value

Value aliases database/sql/driver.Value.

Jump to

Keyboard shortcuts

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