@@ -10,7 +10,7 @@ import (
1010 "strconv"
1111 "time"
1212
13- "github.com/ccoveille/go-safecast"
13+ "github.com/ccoveille/go-safecast/v2 "
1414 "github.com/jackc/pgx/v5"
1515 "github.com/jackc/pgx/v5/pgxpool"
1616 "github.com/prometheus/client_golang/prometheus"
@@ -87,13 +87,17 @@ type nodeConnectionBalancer[P balancePoolConn[C], C balanceConn] struct {
8787// testing purposes. Callers should use the exported NewNodeConnectionBalancer.
8888func newNodeConnectionBalancer [P balancePoolConn [C ], C balanceConn ](pool balanceablePool [P , C ], healthTracker * NodeHealthTracker , interval time.Duration ) * nodeConnectionBalancer [P , C ] {
8989 seed := int64 (0 )
90+ var err error
9091 for seed == 0 {
91- // Sum64 returns a uint64, and safecast will return 0 if it's not castable,
92+ // Sum64 returns a uint64, and safecast will return an error if it's not castable,
9293 // which will happen about half the time (?). We just keep running it until
9394 // we get a seed that fits in the box.
9495 // Subtracting math.MaxInt64 should mean that we retain the entire range of
9596 // possible values.
96- seed , _ = safecast.Convert [int64 ](new (maphash.Hash ).Sum64 () - math .MaxInt64 )
97+ seed , err = safecast.Convert [int64 ](new (maphash.Hash ).Sum64 () - math .MaxInt64 )
98+ if err != nil {
99+ seed = 0
100+ }
97101 }
98102 return & nodeConnectionBalancer [P , C ]{
99103 ticker : time .NewTicker (interval ),
@@ -159,8 +163,8 @@ func (p *nodeConnectionBalancer[P, C]) mustPruneConnections(ctx context.Context)
159163
160164 // It's highly unlikely that we'll ever have an overflow in
161165 // this context, so we cast directly.
162- nodeCount , _ := safecast.Convert [uint32 ](p .healthTracker .HealthyNodeCount ())
163- if nodeCount == 0 {
166+ nodeCount , err := safecast.Convert [uint32 ](p .healthTracker .HealthyNodeCount ())
167+ if err != nil || nodeCount == 0 {
164168 nodeCount = 1
165169 }
166170
0 commit comments