Skip to content

Commit

Permalink
disable depguard, tidy deps, fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrodriges committed Nov 12, 2024
1 parent 70a6c45 commit de33ece
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 30 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ linters:
disable-all: true
enable:
- deadcode
- depguard
- errcheck
- goconst
- gofmt # On why gofmt when goimports is enabled - https://github.com/golang/go/issues/21476
Expand Down
18 changes: 9 additions & 9 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,22 @@ func (cl *Cluster[T]) Err() error {
}

// Node returns cluster node with specified status
func (cl *Cluster[T]) Node(criteria NodeStateCriteria) *Node[T] {
return pickNodeByCriteria(cl.checkedNodes.Load().(CheckedNodes[T]), cl.picker, criteria)
func (cl *Cluster[T]) Node(criterion NodeStateCriterion) *Node[T] {
return pickNodeByCriterion(cl.checkedNodes.Load().(CheckedNodes[T]), cl.picker, criterion)
}

// WaitForNode with specified status to appear or until context is canceled
func (cl *Cluster[T]) WaitForNode(ctx context.Context, criteria NodeStateCriteria) (*Node[T], error) {
func (cl *Cluster[T]) WaitForNode(ctx context.Context, criterion NodeStateCriterion) (*Node[T], error) {
// Node already exists?
node := cl.Node(criteria)
node := cl.Node(criterion)
if node != nil {
return node, nil
}

ch := cl.addUpdateSubscriber(criteria)
ch := cl.addUpdateSubscriber(criterion)

// Node might have appeared while we were adding waiter, recheck
node = cl.Node(criteria)
node = cl.Node(criterion)
if node != nil {
return node, nil
}
Expand Down Expand Up @@ -190,11 +190,11 @@ func (cl *Cluster[T]) updateNodes(ctx context.Context) {
cl.tracer.NotifiedWaiters()
}

// pickNodeByCriteria is a helper function to pick a single node by given criteria
func pickNodeByCriteria[T Querier](nodes CheckedNodes[T], picker NodePicker[T], criteria NodeStateCriteria) *Node[T] {
// 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 criteria {
switch criterion {
case Alive:
subset = nodes.alive
case Primary:
Expand Down
6 changes: 3 additions & 3 deletions cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ func TestCluster_Node(t *testing.T) {
cl := new(Cluster[*sql.DB])
cl.checkedNodes.Store(CheckedNodes[*sql.DB]{})

// all criterias must return nil node
for i := Alive; i < maxNodeCriteria; i++ {
node := cl.Node(NodeStateCriteria(i))
// all criteria must return nil node
for i := Alive; i < maxNodeCriterion; i++ {
node := cl.Node(NodeStateCriterion(i))
assert.Nil(t, node)
}
})
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
Expand Down
20 changes: 10 additions & 10 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@

package hasql

type NodeStateCriteria uint8
type NodeStateCriterion uint8

const (
// Alive is a criteria to choose any alive node
Alive NodeStateCriteria = iota + 1
// Primary is a criteria to choose primary node
// Alive is a criterion to choose any alive node
Alive NodeStateCriterion = iota + 1
// Primary is a criterion to choose primary node
Primary
// Standby is a criteria to choose standby node
// Standby is a criterion to choose standby node
Standby
// PreferPrimary is a criteria to choose primary or any alive node
// PreferPrimary is a criterion to choose primary or any alive node
PreferPrimary
// PreferStandby is a criteria to choose standby or any alive node
// PreferStandby is a criterion to choose standby or any alive node
PreferStandby

// maxNodeCriteria is for testing purposes only
// any new criteria must be added above this constant
maxNodeCriteria
// maxNodeCriterion is for testing purposes only
// all new criteria must be added above this constant
maxNodeCriterion
)

type Node[T Querier] struct {
Expand Down
10 changes: 5 additions & 5 deletions notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ package hasql

// updateSubscriber represents a waiter for newly checked node event
type updateSubscriber[T Querier] struct {
ch chan *Node[T]
criteria NodeStateCriteria
ch chan *Node[T]
criterion NodeStateCriterion
}

// addUpdateSubscriber adds new dubscriber to notification pool
func (cl *Cluster[T]) addUpdateSubscriber(criteria NodeStateCriteria) <-chan *Node[T] {
func (cl *Cluster[T]) addUpdateSubscriber(criterion NodeStateCriterion) <-chan *Node[T] {
// buffered channel is essential
// read WaitForNode function for more information
ch := make(chan *Node[T], 1)
cl.subscribersMu.Lock()
defer cl.subscribersMu.Unlock()
cl.subscribers = append(cl.subscribers, updateSubscriber[T]{ch: ch, criteria: criteria})
cl.subscribers = append(cl.subscribers, updateSubscriber[T]{ch: ch, criterion: criterion})
return ch
}

Expand All @@ -46,7 +46,7 @@ func (cl *Cluster[T]) notifyUpdateSubscribers(nodes CheckedNodes[T]) {
var nodelessWaiters []updateSubscriber[T]
// Notify all waiters
for _, subscriber := range cl.subscribers {
node := pickNodeByCriteria(nodes, cl.picker, subscriber.criteria)
node := pickNodeByCriterion(nodes, cl.picker, subscriber.criterion)
if node == nil {
// Put waiter back
nodelessWaiters = append(nodelessWaiters, subscriber)
Expand Down

0 comments on commit de33ece

Please sign in to comment.