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 ¶
const DefaultCheckInterval = 5 * time.Second
DefaultCheckInterval is how often a Tracker probes the server.
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.
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 ¶
IsReadOnlyError reports whether err is a write refused by a read-only server (SQLSTATE 25006).
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 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 Start ¶
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 ¶
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.