|
7 | 7 | "time" |
8 | 8 |
|
9 | 9 | "github.com/jackc/pgx/v5" |
10 | | - "github.com/lthibault/jitterbug" |
11 | 10 | "github.com/prometheus/client_golang/prometheus" |
12 | 11 | "golang.org/x/time/rate" |
13 | 12 |
|
@@ -57,19 +56,30 @@ func NewNodeHealthChecker(url string) (*NodeHealthTracker, error) { |
57 | 56 |
|
58 | 57 | // Poll starts polling the cluster and recording the node IDs that it sees. |
59 | 58 | 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 | + |
68 | 75 | for { |
69 | 76 | select { |
70 | 77 | case <-ctx.Done(): |
71 | 78 | 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) |
73 | 83 | t.tryConnect(interval) |
74 | 84 | } |
75 | 85 | } |
|
0 commit comments