dbconn

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package dbconn opens read-only, time-limited connections to MySQL and MariaDB servers.

Index

Constants

This section is empty.

Variables

View Source
var ErrBadDSN = errors.New("the DSN doesn't parse: expected user:password@tcp(host:port)/database or mysql://user:password@host:port/database")

ErrBadDSN is returned for a DSN that doesn't parse. The driver's own errors quote parts of the DSN, which can be the password, so ParseDSN returns this instead.

View Source
var ErrNotRepeatableRead = errors.New("the session did not take REPEATABLE READ, so it can't hold a snapshot")

ErrNotRepeatableRead is returned by OpenSnapshot when the session won't take REPEATABLE READ, the only level that holds a consistent snapshot.

View Source
var ErrSnapshotLost = errors.New("lost the connection that held the snapshot")

ErrSnapshotLost is returned by every call on a Snapshot after its connection or its transaction was lost: the snapshot went with it.

View Source
var ErrStatementRefused = errors.New("refused a statement the snapshot doesn't allow")

ErrStatementRefused is returned, before anything runs, for a statement a Snapshot doesn't allow: one that could end its transaction, write, lock rows or change the server. Its text never quotes the statement.

Functions

func Address

func Address(cfg *mysql.Config) string

Address renders where the server is, without credentials.

func Describe

func Describe(cfg *mysql.Config) string

Describe renders the connection target without the password, e.g. shop_ro@db.internal:3306/shop.

func ErrorText added in v0.3.0

func ErrorText(err error) string

ErrorText describes a failed statement without quoting the server's message, which can repeat bound arguments or bytes from a row. Reports and warnings use it for every statement that reads keys or values.

func IsTimeout added in v0.3.0

func IsTimeout(err error) bool

IsTimeout reports whether a statement was stopped by the client or server time limit.

func ParseDSN

func ParseDSN(dsn, password string) (*mysql.Config, error)

ParseDSN reads a connection string in either of these forms:

mysql://user:pass@host:3306/dbname?tls=true
user:pass@tcp(host:3306)/dbname?tls=true

The URL form also accepts ?socket=/path/to/mysqld.sock. When the DSN has no password, password (usually from PRODMASK_PASSWORD) is used instead. Its errors never quote the DSN.

func WritePrivileges

func WritePrivileges(grants []string, database string) []string

WritePrivileges reads SHOW GRANTS output and returns the privileges that would let the user change the given database.

Types

type Row added in v0.6.0

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

Row is the result of QueryRowContext.

func (*Row) Scan added in v0.6.0

func (r *Row) Scan(dest ...any) error

Scan copies the row's columns into dest, or returns sql.ErrNoRows.

type Session

type Session struct {
	DB       *sql.DB
	Server   model.ServerInfo
	Database string
	Timeout  time.Duration
	// ServerTimeout reports whether the server enforces Timeout itself
	// (max_statement_time or max_execution_time). Without it, a statement
	// the client gives up on keeps running on the server.
	ServerTimeout bool
	Warnings      []string
}

Session is a pool of exactly one connection to the scanned database. Every connection in it is read-only, gives up waiting for metadata locks after a few seconds, and has a server-side statement timeout. Statements run one at a time, and autocommit is on, so no lock or snapshot outlives a statement. When a timeout kills the connection, database/sql opens a new one with the same settings.

func Open

func Open(ctx context.Context, cfg *mysql.Config, timeout time.Duration) (*Session, error)

Open connects, checks the server and the user's privileges, and prepares the read-only session.

func (*Session) Close

func (s *Session) Close() error

Close closes the pool.

func (*Session) Flavor

func (s *Session) Flavor() string

Flavor is "mariadb" or "mysql".

type Snapshot added in v0.6.0

type Snapshot struct {
	Server   model.ServerInfo
	Database string
	// ID is the connection's CONNECTION_ID(), which another session can
	// look for in the process list or end with KILL.
	ID int64
	// Started is when the snapshot was taken, by the local clock.
	Started time.Time
	// contains filtered or unexported fields
}

Snapshot is one pinned connection that sees a database as it was at one moment: a REPEATABLE READ transaction started READ ONLY, WITH CONSISTENT SNAPSHOT. Every statement runs on that connection, so InnoDB tables read minutes apart still agree with each other.

The connection is never replaced. A database/sql pool moves its work to a new connection when one breaks, which would quietly read the rest from a later moment; a pinned connection fails instead. Once the connection is lost, every later call returns ErrSnapshotLost, and the reader has to start again with a new snapshot.

The session reads TIMESTAMP values in UTC (time_zone '+00:00'), whatever the server's zone, and starts with SET NAMES utf8mb4. Statement time limits are off, since reading a whole table takes as long as it takes; waits for metadata locks are bounded. The connection may idle for as long as the server allows (wait_timeout at its maximum).

Only statements that keep the snapshot run: ExecContext takes SETs of session variables, and the query methods take reads (SELECT, SHOW, DESCRIBE, EXPLAIN, VALUES, TABLE, and WITH ... SELECT). INTO, locking reads and executable comments are refused anywhere. A refused statement returns ErrStatementRefused without running, and the snapshot stays. The rules don't vet functions, so callers must not call ones that write.

func OpenSnapshot added in v0.6.0

func OpenSnapshot(ctx context.Context, cfg *mysql.Config, opts SnapshotOptions) (*Snapshot, error)

OpenSnapshot connects to the database in cfg and starts the snapshot.

func (*Snapshot) Check added in v0.6.0

func (s *Snapshot) Check(ctx context.Context) error

Check confirms the snapshot still holds, so nothing was read from a later moment: the same connection answers, and its transaction is still open. SET TRANSACTION fails with error 1568 inside an open transaction; any other outcome means the transaction, and the snapshot, ended.

func (*Snapshot) Close added in v0.6.0

func (s *Snapshot) Close() error

Close ends the snapshot and its connection.

func (*Snapshot) Err added in v0.6.0

func (s *Snapshot) Err() error

Err returns ErrSnapshotLost once a call found the connection lost, and nil before.

func (*Snapshot) ExecContext added in v0.6.0

func (s *Snapshot) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

ExecContext runs a SET of session variables in the snapshot's session. Anything else returns ErrStatementRefused without running.

func (*Snapshot) PrepareContext added in v0.6.0

func (s *Snapshot) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

PrepareContext prepares a read on the snapshot's connection. Queries through it use the binary protocol, which returns FLOAT and DOUBLE values exactly; the text protocol rounds them to a few digits.

func (*Snapshot) QueryContext added in v0.6.0

func (s *Snapshot) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

QueryContext runs a read in the snapshot. A connection lost while the rows are read shows up in rows.Err(), and then in every later call.

func (*Snapshot) QueryRowContext added in v0.6.0

func (s *Snapshot) QueryRowContext(ctx context.Context, query string, args ...any) *Row

QueryRowContext runs a read that returns at most one row. Its error shows up in Scan.

type SnapshotOptions added in v0.6.0

type SnapshotOptions struct {
	// LockWait bounds how long a statement waits for a metadata lock
	// (lock_wait_timeout). Default 10 seconds.
	LockWait time.Duration
	// NetWrite is how long the server waits for the client to take more
	// rows before it gives up (net_write_timeout). A reader that writes
	// each batch somewhere slow needs it long. Default 10 minutes.
	NetWrite time.Duration
}

SnapshotOptions tunes the snapshot's session. Zero values pick the defaults.

Jump to

Keyboard shortcuts

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