Documentation ¶
Index ¶
- Constants
- func PostgreSQL(ctx context.Context, db dbsql.Node) (bool, error)
- type AliveNodes
- type Checker
- type Cluster
- func (cl *Cluster) Alive() dbsql.Node
- func (cl *Cluster) Close() error
- func (cl *Cluster) Err() error
- func (cl *Cluster) HealthCheck(ctx context.Context) []health.CheckResult
- func (cl *Cluster) Node(criteria NodeStateCriteria) dbsql.Node
- func (cl *Cluster) Nodes() []dbsql.Node
- func (cl *Cluster) Primary() dbsql.Node
- func (cl *Cluster) PrimaryPreferred() dbsql.Node
- func (cl *Cluster) Shutdown(p *shutdown.Process) error
- func (cl *Cluster) Standby() dbsql.Node
- func (cl *Cluster) StandbyPreferred() dbsql.Node
- func (cl *Cluster) WaitForAlive(ctx context.Context) (dbsql.Node, error)
- func (cl *Cluster) WaitForNode(ctx context.Context, criteria NodeStateCriteria) (dbsql.Node, error)
- func (cl *Cluster) WaitForPrimary(ctx context.Context) (dbsql.Node, error)
- func (cl *Cluster) WaitForPrimaryPreferred(ctx context.Context) (dbsql.Node, error)
- func (cl *Cluster) WaitForStandby(ctx context.Context) (dbsql.Node, error)
- func (cl *Cluster) WaitForStandbyPreferred(ctx context.Context) (dbsql.Node, error)
- type CollectedErrors
- type ForecastStore
- type NodeError
- type NodeStateCriteria
- type Option
- type Picker
- type Store
- type Tracer
Constants ¶
const ( DefaultUpdateInterval = time.Second * 5 DefaultUpdateTimeout = time.Second )
Default values for Cluster config
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AliveNodes ¶
AliveNodes of Store
type Checker ¶
Checker is a signature for functions that check if a specific node is alive and is primary. Returns true for primary and false if not. If error is returned, the node is considered dead. Check function can be used to perform a Query returning single boolean value that signals if node is primary or not.
type Cluster ¶
type Cluster struct {
// contains filtered or unexported fields
}
Cluster consists of number of 'nodes' of a single SQL database. Background goroutine periodically checks nodes and updates their status.
func New ¶
New constructs Cluster object representing a single 'Cluster' of SQL database. Close function must be called when a Cluster isn't necessary anymore.
func (*Cluster) Err ¶
Err returns the combined error including most recent errors for all nodes. This error is CollectedErrors or nil.
func (*Cluster) HealthCheck ¶
func (cl *Cluster) HealthCheck(ctx context.Context) []health.CheckResult
func (*Cluster) Node ¶
func (cl *Cluster) Node(criteria NodeStateCriteria) dbsql.Node
Node returns Cluster node with specified status.
func (*Cluster) Primary ¶
Primary returns first available node that is considered alive and is primary (able to execute write operations)
func (*Cluster) PrimaryPreferred ¶
PrimaryPreferred returns primary node if possible, standby otherwise
func (*Cluster) Standby ¶
Standby returns node that is considered alive and is standby (unable to execute write operations)
func (*Cluster) StandbyPreferred ¶
StandbyPreferred returns standby node if possible, primary otherwise
func (*Cluster) WaitForAlive ¶
WaitForAlive node to appear or until context is canceled
func (*Cluster) WaitForNode ¶
WaitForNode with specified status to appear or until context is canceled
func (*Cluster) WaitForPrimary ¶
WaitForPrimary node to appear or until context is canceled
func (*Cluster) WaitForPrimaryPreferred ¶
WaitForPrimaryPreferred node to appear or until context is canceled
func (*Cluster) WaitForStandby ¶
WaitForStandby node to appear or until context is canceled
type CollectedErrors ¶
type CollectedErrors struct {
Errors []NodeError
}
CollectedErrors are errors collected when checking node statuses
func (*CollectedErrors) Error ¶
func (e *CollectedErrors) Error() string
type ForecastStore ¶
type ForecastStore Store
ForecastStore is a type alias for plumbing it through Wire.
type NodeStateCriteria ¶
type NodeStateCriteria int
NodeStateCriteria for choosing a node
const ( // Alive for choosing any alive node Alive NodeStateCriteria = iota + 1 // Primary for choosing primary node Primary // Standby for choosing standby node Standby // PreferPrimary for choosing primary or any alive node PreferPrimary // PreferStandby for choosing standby or any alive node PreferStandby )
type Option ¶
type Option func(*Cluster)
Option is a functional option type for Cluster constructor.
func WithNodePicker ¶
WithNodePicker sets algorithm for node selection (e.g., random, round-robin etc.).
func WithTracer ¶
WithTracer sets tracer for actions happening in the background.
func WithUpdate ¶
func WithUpdate() Option
WithUpdate decides whether to update node states. Useful for tests with mocked sql.DB.
func WithUpdateInterval ¶
WithUpdateInterval sets interval between Cluster node updates.
func WithUpdateTimeout ¶
WithUpdateTimeout sets ping timeout for update of each node in Cluster.
type Picker ¶
Picker is a signature for functions that determine how to pick single node from set of nodes. Nodes passed to the picker function are sorted according to latency (from lowest to greatest).
func PickNodeClosest ¶
func PickNodeClosest() Picker
PickNodeClosest returns node with the least latency
func PickNodeRandom ¶
func PickNodeRandom() Picker
PickNodeRandom returns random node from nodes set
func PickNodeRoundRobin ¶
func PickNodeRoundRobin() Picker
PickNodeRoundRobin returns next node based on Round Robin algorithm
type Store ¶
type Store interface { Close() error Err() error Nodes() []dbsql.Node Alive() dbsql.Node Primary() dbsql.Node Standby() dbsql.Node StandbyPreferred() dbsql.Node Node(criteria NodeStateCriteria) dbsql.Node WaitForPrimary(ctx context.Context) (dbsql.Node, error) WaitForStandby(ctx context.Context) (dbsql.Node, error) WaitForPrimaryPreferred(ctx context.Context) (dbsql.Node, error) WaitForStandbyPreferred(ctx context.Context) (dbsql.Node, error) WaitForAlive(ctx context.Context) (dbsql.Node, error) }
Store represents a store that manages a Cluster of nodes. It provides methods for retrieving information about the nodes in the Cluster, as well as closing the store and checking for any errors.
type Tracer ¶
type Tracer struct { // UpdateNodes is called when before updating nodes status. UpdateNodes func() // UpdatedNodes is called after all nodes are updated. The nodes is a list of currently alive nodes. UpdatedNodes func(nodes AliveNodes) // NodeDead is called when it is determined that specified node is dead. NodeDead func(node dbsql.Node, err error) // NodeAlive is called when it is determined that specified node is alive. NodeAlive func(node dbsql.Node) // NotifiedWaiters is called when all callers of 'WaitFor*' functions have been notified. NotifiedWaiters func() }
Tracer is a set of hooks to run at various stages of background nodes status update. Any particular hook may be nil. Functions may be called concurrently from different goroutines.