Skip to content

Commit

Permalink
rearrange code
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrodriges committed Nov 18, 2024
1 parent ea27d78 commit 16fcd31
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
28 changes: 28 additions & 0 deletions checked_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
31 changes: 2 additions & 29 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
}

0 comments on commit 16fcd31

Please sign in to comment.