Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrodriges committed Nov 29, 2024
1 parent a17d83b commit 2d9c06c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion checked_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestCheckNodes(t *testing.T) {
}

// mock node checker func
checkFn := func(_ context.Context, q Querier) (NodeInfoProvider, error) {
checkFn := func(_ context.Context, _ Querier) (NodeInfoProvider, error) {
return nil, io.EOF
}

Expand Down
1 change: 1 addition & 0 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
limitations under the License.
*/

// Package hasql provides simple and reliable way to access high-availability database setups with multiple hosts.
package hasql

import (
Expand Down
6 changes: 3 additions & 3 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func TestEnd2End_FlakyCluster(t *testing.T) {
// it will fail with error on every second attempt to query state
var attempts uint32
db1 := &mockQuerier{
queryFn: func(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
queryFn: func(_ context.Context, _ string, _ ...any) (*sql.Rows, error) {
call := atomic.AddUint32(&attempts, 1)
if call%2 == 0 {
return nil, io.EOF
Expand All @@ -344,12 +344,12 @@ func TestEnd2End_FlakyCluster(t *testing.T) {

// set db2 and db3 to be standbys
db2 := &mockQuerier{
queryFn: func(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
queryFn: func(_ context.Context, _ string, _ ...any) (*sql.Rows, error) {
return nil, errIsStandby
},
}
db3 := &mockQuerier{
queryFn: func(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
queryFn: func(_ context.Context, _ string, _ ...any) (*sql.Rows, error) {
return nil, errIsStandby
},
}
Expand Down
2 changes: 2 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package hasql

// NodeStateCriterion represents a node selection criterion
type NodeStateCriterion uint8

const (
Expand All @@ -35,6 +36,7 @@ const (
maxNodeCriterion
)

// Node holds reference to database connection pool with some additional data
type Node[T Querier] struct {
name string
db T
Expand Down
7 changes: 5 additions & 2 deletions node_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import (
type NodeRole uint8

const (
// NodeRoleUnknown used to report node with unconvetional role in cluster
NodeRoleUnknown NodeRole = iota
// NodeRolePrimary used to report node with primary role in cluster
NodeRolePrimary
// NodeRoleStandby used to report node with standby role in cluster
NodeRoleStandby
)

Expand Down Expand Up @@ -117,7 +120,7 @@ func PostgreSQLChecker(ctx context.Context, db Querier) (NodeInfoProvider, error
}, nil
}

// MySQL checks state of MySQL node.
// MySQLChecker checks state of MySQL node.
// ATTENTION: database user must have REPLICATION CLIENT privilege to perform underlying query.
func MySQLChecker(ctx context.Context, db Querier) (NodeInfoProvider, error) {
start := time.Now()
Expand Down Expand Up @@ -152,7 +155,7 @@ func MySQLChecker(ctx context.Context, db Querier) (NodeInfoProvider, error) {
}, nil
}

// MSSQL checks state of MSSQL node
// MSSQLChecker checks state of MSSQL node
func MSSQLChecker(ctx context.Context, db Querier) (NodeInfoProvider, error) {
start := time.Now()

Expand Down
1 change: 1 addition & 0 deletions node_discoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func NewStaticNodeDiscoverer[T Querier](nodes ...*Node[T]) StaticNodeDiscoverer[
return StaticNodeDiscoverer[T]{nodes: nodes}
}

// DiscoverNodes returns static list of nodes from StaticNodeDiscoverer
func (s StaticNodeDiscoverer[T]) DiscoverNodes(_ context.Context) ([]*Node[T], error) {
return s.nodes, nil
}
8 changes: 8 additions & 0 deletions node_picker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ var _ NodePicker[*sql.DB] = (*RandomNodePicker[*sql.DB])(nil)
// RandomNodePicker returns random node on each call and does not sort checked nodes
type RandomNodePicker[T Querier] struct{}

// PickNode returns random node from picker
func (_ *RandomNodePicker[T]) PickNode(nodes []CheckedNode[T]) CheckedNode[T] {
return nodes[rand.IntN(len(nodes))]
}

// CompareNodes always treats nodes as equal, effectively not changing nodes order
func (_ *RandomNodePicker[T]) CompareNodes(_, _ CheckedNode[T]) int {
return 0
}
Expand All @@ -54,11 +56,13 @@ type RoundRobinNodePicker[T Querier] struct {
idx uint32
}

// PickNode returns next node in Round-Robin sequence
func (r *RoundRobinNodePicker[T]) PickNode(nodes []CheckedNode[T]) CheckedNode[T] {
n := atomic.AddUint32(&r.idx, 1)
return nodes[(int(n)-1)%len(nodes)]
}

// CompareNodes performs lexicographical comparison of two nodes
func (r *RoundRobinNodePicker[T]) CompareNodes(a, b CheckedNode[T]) int {
aName, bName := a.Node.String(), b.Node.String()
if aName < bName {
Expand All @@ -77,10 +81,12 @@ var _ NodePicker[*sql.DB] = (*LatencyNodePicker[*sql.DB])(nil)
// WARNING: This picker requires that NodeInfoProvider can report node's network latency otherwise code will panic!
type LatencyNodePicker[T Querier] struct{}

// PickNode returns node with least network latency
func (_ *LatencyNodePicker[T]) PickNode(nodes []CheckedNode[T]) CheckedNode[T] {
return nodes[0]
}

// CompareNodes performs nodes comparison based on reported network latency
func (_ *LatencyNodePicker[T]) CompareNodes(a, b CheckedNode[T]) int {
aLatency := a.Info.(interface{ Latency() time.Duration }).Latency()
bLatency := b.Info.(interface{ Latency() time.Duration }).Latency()
Expand All @@ -102,10 +108,12 @@ var _ NodePicker[*sql.DB] = (*ReplicationNodePicker[*sql.DB])(nil)
// WARNING: This picker requires that NodeInfoProvider can report node's replication lag otherwise code will panic!
type ReplicationNodePicker[T Querier] struct{}

// PickNode returns node with lowest replication lag value
func (_ *ReplicationNodePicker[T]) PickNode(nodes []CheckedNode[T]) CheckedNode[T] {
return nodes[0]
}

// CompareNodes performs nodes comparison based on reported replication lag
func (_ *ReplicationNodePicker[T]) CompareNodes(a, b CheckedNode[T]) int {
aLag := a.Info.(interface{ ReplicationLag() int }).ReplicationLag()
bLag := b.Info.(interface{ ReplicationLag() int }).ReplicationLag()
Expand Down

0 comments on commit 2d9c06c

Please sign in to comment.