Documentation ¶
Index ¶
- Constants
- type AliveNodes
- type Cluster
- func (cl *Cluster[T]) Alive() Node[T]
- func (cl *Cluster[T]) Close() error
- func (cl *Cluster[T]) Err() error
- func (cl *Cluster[T]) Node(criteria NodeStateCriteria) Node[T]
- func (cl *Cluster[T]) Nodes() []Node[T]
- func (cl *Cluster[T]) Primary() Node[T]
- func (cl *Cluster[T]) PrimaryPreferred() Node[T]
- func (cl *Cluster[T]) Standby() Node[T]
- func (cl *Cluster[T]) StandbyPreferred() Node[T]
- func (cl *Cluster[T]) WaitForAlive(ctx context.Context) (Node[T], error)
- func (cl *Cluster[T]) WaitForNode(ctx context.Context, criteria NodeStateCriteria) (Node[T], error)
- func (cl *Cluster[T]) WaitForPrimary(ctx context.Context) (Node[T], error)
- func (cl *Cluster[T]) WaitForPrimaryPreferred(ctx context.Context) (Node[T], error)
- func (cl *Cluster[T]) WaitForStandby(ctx context.Context) (Node[T], error)
- func (cl *Cluster[T]) WaitForStandbyPreferred(ctx context.Context) (Node[T], error)
- type ClusterOption
- type CollectedErrors
- type ConnCloser
- type Node
- type NodeChecker
- type NodeError
- type NodePicker
- type NodeStateCriteria
- type Tracer
Constants ¶
const ( DefaultUpdateInterval = time.Second * 5 DefaultUpdateTimeout = time.Second )
Default values for cluster config
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AliveNodes ¶
AliveNodes of Cluster
type Cluster ¶
type Cluster[T any] 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 NewCluster ¶
func NewCluster[T any](nodes []Node[T], checker NodeChecker[T], closer ConnCloser[T], opts ...ClusterOption[T]) (*Cluster[T], error)
NewCluster constructs cluster object representing a single 'cluster' of SQL database. Close function must be called when cluster is not needed anymore.
func (*Cluster[T]) Err ¶
Err returns the combined error including most recent errors for all nodes. This error is CollectedErrors or nil.
func (*Cluster[T]) Node ¶
func (cl *Cluster[T]) Node(criteria NodeStateCriteria) Node[T]
Node returns cluster node with specified status.
func (*Cluster[T]) Primary ¶
Primary returns first available node that is considered alive and is primary (able to execute write operations)
func (*Cluster[T]) PrimaryPreferred ¶
PrimaryPreferred returns primary node if possible, standby otherwise
func (*Cluster[T]) Standby ¶
Standby returns node that is considered alive and is standby (unable to execute write operations)
func (*Cluster[T]) StandbyPreferred ¶
StandbyPreferred returns standby node if possible, primary otherwise
func (*Cluster[T]) WaitForAlive ¶
WaitForAlive node to appear or until context is canceled
func (*Cluster[T]) WaitForNode ¶
WaitForNode with specified status to appear or until context is canceled
func (*Cluster[T]) WaitForPrimary ¶
WaitForPrimary node to appear or until context is canceled
func (*Cluster[T]) WaitForPrimaryPreferred ¶
WaitForPrimaryPreferred node to appear or until context is canceled
func (*Cluster[T]) WaitForStandby ¶
WaitForStandby node to appear or until context is canceled
type ClusterOption ¶
ClusterOption is a functional option type for Cluster constructor
func WithNodePicker ¶
func WithNodePicker[T any](picker NodePicker[T]) ClusterOption[T]
WithNodePicker sets algorithm for node selection (e.g. random, round robin etc)
func WithTracer ¶
func WithTracer[T any](tracer Tracer[T]) ClusterOption[T]
WithTracer sets tracer for actions happening in the background
func WithUpdateInterval ¶
func WithUpdateInterval[T any](d time.Duration) ClusterOption[T]
WithUpdateInterval sets interval between cluster node updates
func WithUpdateTimeout ¶
func WithUpdateTimeout[T any](d time.Duration) ClusterOption[T]
WithUpdateTimeout sets ping timeout for update of each node in cluster
type CollectedErrors ¶
type CollectedErrors struct {
Errors []NodeError
}
CollectedErrors are errors collected when checking node statuses
func (*CollectedErrors) Error ¶
func (e *CollectedErrors) Error() string
type ConnCloser ¶
type NodePicker ¶
func PickNodeClosest ¶
func PickNodeClosest[T any]() NodePicker[T]
PickNodeClosest returns node with least latency
func PickNodeRandom ¶
func PickNodeRandom[T any]() NodePicker[T]
PickNodeRandom returns random node from nodes set
func PickNodeRoundRobin ¶
func PickNodeRoundRobin[T any]() NodePicker[T]
PickNodeRoundRobin returns next node based on Round Robin algorithm
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 )
func (NodeStateCriteria) String ¶
func (c NodeStateCriteria) String() string
type Tracer ¶
type Tracer[T any] 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[T]) // NodeDead is called when it is determined that specified node is dead. NodeDead func(node Node[T], err error) // NodeAlive is called when it is determined that specified node is alive. NodeAlive func(node Node[T]) // 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.