diff --git a/checked_node.go b/checked_node.go index 7c17ee2..68b6d9c 100644 --- a/checked_node.go +++ b/checked_node.go @@ -158,3 +158,31 @@ func checkNodes[T Querier](ctx context.Context, discoverer NodeDiscoverer[T], ch return res } + +// pickNodeByCriterion is a helper function to pick a single node by given criterion +func pickNodeByCriterion[T Querier](nodes CheckedNodes[T], picker NodePicker[T], criterion NodeStateCriterion) *Node[T] { + var subset []CheckedNode[T] + + switch criterion { + case Alive: + subset = nodes.alive + case Primary: + subset = nodes.primaries + case Standby: + subset = nodes.standbys + case PreferPrimary: + if subset = nodes.primaries; len(subset) == 0 { + subset = nodes.standbys + } + case PreferStandby: + if subset = nodes.standbys; len(subset) == 0 { + subset = nodes.primaries + } + } + + if len(subset) == 0 { + return nil + } + + return picker.PickNode(subset).Node +} diff --git a/cluster.go b/cluster.go index 722ca18..51ce7e2 100644 --- a/cluster.go +++ b/cluster.go @@ -173,7 +173,8 @@ func (cl *Cluster[T]) backgroundNodesUpdate(ctx context.Context) { } } -// updateNodes checks all nodes and notifies all subscribers +// updateNodes performs a new round of cluster state check +// and notifies all subscribers afterwards func (cl *Cluster[T]) updateNodes(ctx context.Context) { cl.tracer.UpdateNodes() @@ -189,31 +190,3 @@ func (cl *Cluster[T]) updateNodes(ctx context.Context) { cl.tracer.NotifiedWaiters() } - -// pickNodeByCriterion is a helper function to pick a single node by given criterion -func pickNodeByCriterion[T Querier](nodes CheckedNodes[T], picker NodePicker[T], criterion NodeStateCriterion) *Node[T] { - var subset []CheckedNode[T] - - switch criterion { - case Alive: - subset = nodes.alive - case Primary: - subset = nodes.primaries - case Standby: - subset = nodes.standbys - case PreferPrimary: - if subset = nodes.primaries; len(subset) == 0 { - subset = nodes.standbys - } - case PreferStandby: - if subset = nodes.standbys; len(subset) == 0 { - subset = nodes.primaries - } - } - - if len(subset) == 0 { - return nil - } - - return picker.PickNode(subset).Node -}