dbstate

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: AGPL-3.0 Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dbstate tracks whether the PostgreSQL server a Studio replica is connected to takes writes.

With a replicated database the node behind a connection can stop being the primary while the connection stays open: it was demoted, and every write on it fails with SQLSTATE 25006 until the connection is closed and dialled again (to the new primary, with a multi-host DATABASE_URL and target_session_attrs=read-write). A Tracker probes the server on a period and is told about 25006 as it happens; when the server is not writable it raises a generation number, and the pool discards every connection dialled under an older generation the next time it would be reused (pkg/studio wires that to the driver).

The Tracker's Status is what readiness probes and the write-hold wrapper read. SQLite has no tracker: For returns nil, and a nil Tracker reports a writable database.

Index

Constants

View Source
const DefaultCheckInterval = 5 * time.Second

DefaultCheckInterval is how often a Tracker probes the server.

View Source
const DefaultProbeTimeout = 30 * time.Second

DefaultProbeTimeout bounds one probe when Options name no other bound. A probe may have to dial: its bound must cover a connection attempt to every host in the connection string (pkg/studio passes that), or an unreachable first host would make a database that answers on the second look unreachable.

View Source
const ReadOnlySQLState = "25006"

ReadOnlySQLState is the SQLSTATE of a write refused by a server that is read-only (read_only_sql_transaction).

Variables

This section is empty.

Functions

func IsReadOnlyError

func IsReadOnlyError(err error) bool

IsReadOnlyError reports whether err is a write refused by a read-only server (SQLSTATE 25006).

func Register

func Register(pool *sql.DB, t *Tracker)

Register makes For return t for databases on pool.

func Unregister

func Unregister(pool *sql.DB)

Unregister forgets pool's tracker.

Types

type Options

type Options struct {
	// Interval is how often the server is probed (DefaultCheckInterval
	// when not positive).
	Interval time.Duration
	// ProbeTimeout bounds one probe, connecting included
	// (DefaultProbeTimeout when not positive).
	ProbeTimeout time.Duration
}

Options configure a Tracker.

type Probe

type Probe struct {
	// ReadOnly: the server is in recovery, or its sessions are read-only.
	ReadOnly bool
	// Server identifies the node (address and port), and Timeline its
	// timeline when it can be read (0 otherwise); a promotion starts a
	// new one.
	Server   string
	Timeline int64
}

Probe is one look at the server.

type ProbeFunc

type ProbeFunc func(ctx context.Context) (Probe, error)

ProbeFunc looks at the server through the pool.

type State

type State string

State is what the database can do for this replica.

const (
	// Writable: the server answers and takes writes.
	Writable State = "writable"
	// ReadOnly: the server answers but refuses writes (a hot standby, or a
	// primary that was demoted).
	ReadOnly State = "read_only"
	// Unreachable: no server answers.
	Unreachable State = "unreachable"
)

type Status

type Status struct {
	State State
	// Since is when State last changed.
	Since time.Time
	// Failovers counts the times the writable server changed (another
	// node, or the same address on a new timeline) since the tracker
	// started.
	Failovers int64
	// NotWritableSince is when the database last stopped taking writes
	// (by a probe, or a write refused as read-only), kept while it moves
	// between ReadOnly and Unreachable; zero while it is Writable.
	NotWritableSince time.Time
}

Status is a Tracker's view of the database.

type Tracker

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

Tracker follows one database.

func For

func For(db *gorm.DB) *Tracker

For returns the tracker of db's pool, or nil when it has none.

func Start

func Start(probe ProbeFunc, opts Options) *Tracker

Start returns a Tracker that probes every opts.Interval until Stop, or until the probe reports that the database was closed. The first probe has run when it returns.

func (*Tracker) Generation

func (t *Tracker) Generation() uint64

Generation is the number connections are stamped with when they are dialled; one stamped with a lower number than the current one must not be reused. A nil Tracker's generation is always 0.

func (*Tracker) ReportReadOnly

func (t *Tracker) ReportReadOnly()

ReportReadOnly tells the tracker that a statement was just refused with SQLSTATE 25006. The first report while the database counted as writable retires the pool's connections at once and brings the next probe forward; while it already counts as not writable the probes do that, once per interval, so a server that stays read-only is not dialled again for every statement.

func (*Tracker) Status

func (t *Tracker) Status() Status

Status reports the database as last seen. A nil Tracker (SQLite, or a database opened without one) is writable.

func (*Tracker) Stop

func (t *Tracker) Stop()

Stop ends the probes.

Jump to

Keyboard shortcuts

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