Skip to content

Commit 2081927

Browse files
committed
replace jitterbug with stdlib jitter
1 parent 6cd66d8 commit 2081927

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
github.com/google/uuid v1.6.0
88
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.2
99
github.com/jackc/pgx/v5 v5.7.5
10-
github.com/lthibault/jitterbug v2.0.0+incompatible
1110
github.com/magefile/mage v1.15.0
1211
github.com/prometheus/client_golang v1.22.0
1312
github.com/rs/zerolog v1.34.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
3333
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
3434
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
3535
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
36-
github.com/lthibault/jitterbug v2.0.0+incompatible h1:qouq51IKzlMx25+15jbxhC/d79YyTj0q6XFoptNqaUw=
37-
github.com/lthibault/jitterbug v2.0.0+incompatible/go.mod h1:2l7akWd27PScEs6YkjyUVj/8hKgNhbbQ3KiJgJtlf6o=
3836
github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
3937
github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
4038
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=

pkg/health.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"time"
88

99
"github.com/jackc/pgx/v5"
10-
"github.com/lthibault/jitterbug"
1110
"github.com/prometheus/client_golang/prometheus"
1211
"golang.org/x/time/rate"
1312

@@ -57,19 +56,30 @@ func NewNodeHealthChecker(url string) (*NodeHealthTracker, error) {
5756

5857
// Poll starts polling the cluster and recording the node IDs that it sees.
5958
func (t *NodeHealthTracker) Poll(ctx context.Context, interval time.Duration) {
60-
ticker := jitterbug.New(interval, jitterbug.Uniform{
61-
// nolint:gosec
62-
// G404 use of non cryptographically secure random number generator is not concern here,
63-
// as it's used for jittering the interval for health checks.
64-
Source: rand.New(rand.NewSource(time.Now().Unix())),
65-
Min: interval,
66-
})
67-
defer ticker.Stop()
59+
if interval <= 0 {
60+
log.Ctx(ctx).Warn().Dur("interval", interval).Msg("health check poll interval must be positive, polling disabled")
61+
return
62+
}
63+
64+
// nolint:gosec
65+
// G404 use of non cryptographically secure random number generator is not a concern here,
66+
// as it's used for jittering the interval for health checks.
67+
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
68+
69+
// Reusable timer - avoids allocating a new channel on each tick
70+
halfInterval := int64(interval) / 2
71+
jitter := time.Duration(rng.Int63n(int64(interval)) - halfInterval)
72+
timer := time.NewTimer(interval + jitter)
73+
defer timer.Stop()
74+
6875
for {
6976
select {
7077
case <-ctx.Done():
7178
return
72-
case <-ticker.C:
79+
case <-timer.C:
80+
// Reset timer for next tick before tryConnect so timer runs concurrently
81+
jitter = time.Duration(rng.Int63n(int64(interval)) - halfInterval)
82+
timer.Reset(interval + jitter)
7383
t.tryConnect(interval)
7484
}
7585
}

0 commit comments

Comments
 (0)