Skip to content

Agent Mesh

fix(node): configurable policy sync jitter and timer leak prevention

Antonio Ojeac236f3942c73
diff --git a/internal/node/node.go b/internal/node/node.go
index b73286fe..7ba1ed69 100644
--- a/internal/node/node.go
+++ b/internal/node/node.go
@@ -1133,10 +1133,15 @@ func (n *SamNode) listenForHubEvents(ctx context.Context) {
case api.MeshEvent_POLICY_UPDATE:
logger.Infof("[Mesh Event] Received POLICY_UPDATE event from %s, triggering sync", msg.ReceivedFrom)
go func() {
- // Jitter delay (0-2s) to prevent thundering herd on Control Plane
- jitter := time.Duration(rand.Intn(2000)) * time.Millisecond
+ maxJitter := n.config.PolicySyncJitter
+ if maxJitter <= 0 {
+ maxJitter = 10 * time.Second
+ }
+ jitter := time.Duration(rand.Int63n(int64(maxJitter)))
+ timer := time.NewTimer(jitter)
+ defer timer.Stop()
select {
- case <-time.After(jitter):
+ case <-timer.C:
case <-ctx.Done():
return
}
diff --git a/internal/node/options.go b/internal/node/options.go
index fff8ef8a..2f8dcae8 100644
--- a/internal/node/options.go
+++ b/internal/node/options.go
@@ -60,6 +60,8 @@ type Options struct {
RequiredRole string
// PolicySyncInterval specifies how often the node syncs the mesh policy from the Hub.
PolicySyncInterval time.Duration
+ // PolicySyncJitter specifies the maximum jitter delay when scheduling policy syncs on event broadcasts.
+ PolicySyncJitter time.Duration
}
// Default applies default values to Options if they are not specified.
@@ -103,6 +105,9 @@ func (o *Options) Default() {
if o.PolicySyncInterval == 0 {
o.PolicySyncInterval = 1 * time.Hour
}
+ if o.PolicySyncJitter <= 0 {
+ o.PolicySyncJitter = 10 * time.Second
+ }
}
// Validate verifies that the required options are provided and valid.