Skip to content

Commit ec35452

Browse files
committed
pass debounce options as a parameter
Take *DebounceOptions on HandleSubscription instead of exposing a second entry point, so the gateway-api call site keeps its handler inline rather than hoisting it into a variable to feed two branches. Other callers pass nil. Signed-off-by: Huabing (Robin) Zhao <zhaohuabing@gmail.com> Signed-off-by: Huabing (Robin) Zhao <huabing@tetrate.io>
1 parent 1ce8e4e commit ec35452

8 files changed

Lines changed: 407 additions & 403 deletions

File tree

internal/cmd/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func GetServerCommand(asyncErrHandler func(string, error)) *cobra.Command {
6363
asyncErrHandler(update.Key, update.Value)
6464
}
6565
},
66+
nil,
6667
)
6768

6869
hook := func(c context.Context, cfg *config.Server) error {

internal/gatewayapi/runner/runner.go

Lines changed: 366 additions & 368 deletions
Large diffs are not rendered by default.

internal/globalratelimit/runner/runner.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ func (r *Runner) translateFromSubscription(ctx context.Context, c <-chan watchab
184184
}
185185
}
186186
},
187+
nil,
187188
)
188189
r.Logger.Info("subscriber shutting down")
189190
}

internal/infrastructure/runner/runner.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func (r *Runner) updateProxyInfraFromSubscription(ctx context.Context, sub <-cha
147147
}
148148
}
149149
},
150+
nil,
150151
)
151152
select {
152153
case <-ctx.Done():

internal/message/watchutil.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,33 +93,13 @@ func handleWithCrashRecovery[K comparable, V any](
9393
// This is better than simply iterating over snapshot.Updates because
9494
// it handles the case where the watchable.Map already contains
9595
// entries before .Subscribe is called.
96-
func HandleSubscription[K comparable, V any](l logging.Logger,
97-
meta Metadata,
98-
subscription <-chan watchable.Snapshot[K, V],
99-
handle func(updateFunc Update[K, V], errChans chan error),
100-
) {
101-
handleSubscription(l, meta, subscription, handle, nil)
102-
}
103-
104-
// HandleSubscriptionWithDebounce is HandleSubscription with time-based coalescing:
105-
// bursts of updates are merged before being handed off, which bounds how often the
106-
// handler runs when the upstream source is churning.
10796
//
108-
// A pending batch is flushed when either no new update has arrived for After, or the
109-
// batch has been held for Max. The former keeps propagation fast for isolated changes;
110-
// the latter caps how long propagation can be delayed while changes keep arriving.
111-
func HandleSubscriptionWithDebounce[K comparable, V any](l logging.Logger,
112-
meta Metadata,
113-
subscription <-chan watchable.Snapshot[K, V],
114-
handle func(updateFunc Update[K, V], errChans chan error),
115-
debounce DebounceOptions,
116-
) {
117-
handleSubscription(l, meta, subscription, handle, &debounce)
118-
}
119-
120-
// handleSubscription implements both entry points. A nil debounce selects the
121-
// undebounced loop, in which every delivered snapshot is handled immediately.
122-
func handleSubscription[K comparable, V any](l logging.Logger,
97+
// A non-nil debounce merges bursts of updates before handing them off, which bounds
98+
// how often handle runs when the upstream source is churning. A pending batch is
99+
// flushed when either no new update has arrived for After, or the batch has been held
100+
// for Max. The former keeps propagation fast for isolated changes; the latter caps how
101+
// long propagation can be delayed while changes keep arriving.
102+
func HandleSubscription[K comparable, V any](l logging.Logger,
123103
meta Metadata,
124104
subscription <-chan watchable.Snapshot[K, V],
125105
handle func(updateFunc Update[K, V], errChans chan error),

internal/message/watchutil_test.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func TestHandleSubscriptionAlreadyClosed(t *testing.T) {
3131
message.Metadata{Runner: "demo", Message: "demo"},
3232
ch,
3333
func(_ message.Update[string, any], _ chan error) { calls++ },
34+
nil,
3435
)
3536
require.Equal(t, 0, calls)
3637
}
@@ -60,6 +61,7 @@ func TestPanicInSubscriptionHandler(t *testing.T) {
6061
numCalls++
6162
panic("oops " + update.Key)
6263
},
64+
nil,
6365
)
6466
require.Equal(t, 2, numCalls)
6567
}
@@ -93,6 +95,7 @@ func TestHandleSubscriptionAlreadyInitialized(t *testing.T) {
9395
storeCalls++
9496
}
9597
},
98+
nil,
9699
)
97100
require.LessOrEqual(t, storeCalls, 2) // updates can be coalesced
98101
require.Equal(t, 1, deleteCalls)
@@ -283,7 +286,9 @@ func TestControllerResourceUpdate(t *testing.T) {
283286
if u.Key == "end" {
284287
m.GatewayAPIResources.Close()
285288
}
286-
})
289+
},
290+
nil,
291+
)
287292
if tc.updates > 1 {
288293
require.LessOrEqual(t, updates, tc.updates) // Updates can be coalesced
289294
} else {
@@ -315,14 +320,14 @@ func TestHandleSubscriptionDebounceQuietPeriod(t *testing.T) {
315320
}()
316321

317322
var updates []any
318-
message.HandleSubscriptionWithDebounce(
323+
message.HandleSubscription(
319324
logging.NewLogger(t.Output(), egv1a1.DefaultEnvoyGatewayLogging()),
320325
message.Metadata{Runner: "demo", Message: "demo"},
321326
sub,
322327
func(update message.Update[string, any], _ chan error) {
323328
updates = append(updates, update.Value)
324329
},
325-
message.DebounceOptions{After: 200 * time.Millisecond, Max: 10 * time.Second},
330+
&message.DebounceOptions{After: 200 * time.Millisecond, Max: 10 * time.Second},
326331
)
327332

328333
// Without debouncing this burst yields up to 10 calls; with it the batch is
@@ -363,7 +368,7 @@ func TestHandleSubscriptionDebounceMaxDelay(t *testing.T) {
363368
start := time.Now()
364369

365370
var updates int
366-
message.HandleSubscriptionWithDebounce(
371+
message.HandleSubscription(
367372
logging.NewLogger(t.Output(), egv1a1.DefaultEnvoyGatewayLogging()),
368373
message.Metadata{Runner: "demo", Message: "demo"},
369374
m.Subscribe(context.Background()),
@@ -375,7 +380,7 @@ func TestHandleSubscriptionDebounceMaxDelay(t *testing.T) {
375380
}
376381
},
377382
// Quiet period far longer than the store interval, so only max can flush.
378-
message.DebounceOptions{After: time.Hour, Max: 200 * time.Millisecond},
383+
&message.DebounceOptions{After: time.Hour, Max: 200 * time.Millisecond},
379384
)
380385

381386
require.NotZero(t, updates, "max delay should have forced at least one flush")
@@ -399,14 +404,14 @@ func TestHandleSubscriptionDebounceFlushesOnClose(t *testing.T) {
399404
}()
400405

401406
var updates []message.Update[string, any]
402-
message.HandleSubscriptionWithDebounce(
407+
message.HandleSubscription(
403408
logging.NewLogger(t.Output(), egv1a1.DefaultEnvoyGatewayLogging()),
404409
message.Metadata{Runner: "demo", Message: "demo"},
405410
m.Subscribe(context.Background()),
406411
func(update message.Update[string, any], _ chan error) {
407412
updates = append(updates, update)
408413
},
409-
message.DebounceOptions{After: 30 * time.Second, Max: time.Hour},
414+
&message.DebounceOptions{After: 30 * time.Second, Max: time.Hour},
410415
)
411416

412417
require.Len(t, updates, 1)
@@ -437,7 +442,7 @@ func TestHandleSubscriptionDebounceCoalescesAcrossKeys(t *testing.T) {
437442

438443
got := map[string]any{}
439444
deletes := map[string]bool{}
440-
message.HandleSubscriptionWithDebounce(
445+
message.HandleSubscription(
441446
logging.NewLogger(t.Output(), egv1a1.DefaultEnvoyGatewayLogging()),
442447
message.Metadata{Runner: "demo", Message: "demo"},
443448
sub,
@@ -448,7 +453,7 @@ func TestHandleSubscriptionDebounceCoalescesAcrossKeys(t *testing.T) {
448453
}
449454
got[update.Key] = update.Value
450455
},
451-
message.DebounceOptions{After: 150 * time.Millisecond, Max: 10 * time.Second},
456+
&message.DebounceOptions{After: 150 * time.Millisecond, Max: 10 * time.Second},
452457
)
453458

454459
// Last write wins per key, and the trailing delete for "baz" survives.

internal/provider/kubernetes/status.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
5959
}),
6060
})
6161
},
62+
nil,
6263
)
6364
r.log.Info("gatewayclass status subscriber shutting down")
6465
}()
@@ -84,6 +85,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
8485
gtw.Status = *update.Value
8586
r.updateStatusForGateway(ctx, gtw)
8687
},
88+
nil,
8789
)
8890
r.log.Info("gateway status subscriber shutting down")
8991
}()
@@ -126,6 +128,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
126128
}),
127129
})
128130
},
131+
nil,
129132
)
130133
r.log.Info("httpRoute status subscriber shutting down")
131134
}()
@@ -168,6 +171,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
168171
}),
169172
})
170173
},
174+
nil,
171175
)
172176
r.log.Info("grpcRoute status subscriber shutting down")
173177
}()
@@ -210,6 +214,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
210214
}),
211215
})
212216
},
217+
nil,
213218
)
214219
r.log.Info("tlsRoute status subscriber shutting down")
215220
}()
@@ -252,6 +257,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
252257
}),
253258
})
254259
},
260+
nil,
255261
)
256262
r.log.Info("tcpRoute status subscriber shutting down")
257263
}()
@@ -294,6 +300,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
294300
}),
295301
})
296302
},
303+
nil,
297304
)
298305
r.log.Info("udpRoute status subscriber shutting down")
299306
}()
@@ -331,6 +338,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
331338
}),
332339
})
333340
},
341+
nil,
334342
)
335343
r.log.Info("listenerSet status subscriber shutting down")
336344
}()
@@ -369,6 +377,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
369377
}),
370378
})
371379
},
380+
nil,
372381
)
373382
r.log.Info("envoyPatchPolicy status subscriber shutting down")
374383
}()
@@ -407,6 +416,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
407416
}),
408417
})
409418
},
419+
nil,
410420
)
411421
r.log.Info("clientTrafficPolicy status subscriber shutting down")
412422
}()
@@ -445,6 +455,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
445455
}),
446456
})
447457
},
458+
nil,
448459
)
449460
r.log.Info("backendTrafficPolicy status subscriber shutting down")
450461
}()
@@ -483,6 +494,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
483494
}),
484495
})
485496
},
497+
nil,
486498
)
487499
r.log.Info("securityPolicy status subscriber shutting down")
488500
}()
@@ -524,6 +536,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
524536
}),
525537
})
526538
},
539+
nil,
527540
)
528541
r.log.Info("backendTlsPolicy status subscriber shutting down")
529542
}()
@@ -562,6 +575,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
562575
}),
563576
})
564577
},
578+
nil,
565579
)
566580
r.log.Info("envoyExtensionPolicy status subscriber shutting down")
567581
}()
@@ -600,6 +614,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
600614
}),
601615
})
602616
},
617+
nil,
603618
)
604619
r.log.Info("backend status subscriber shutting down")
605620
}()
@@ -644,6 +659,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
644659
}),
645660
})
646661
},
662+
nil,
647663
)
648664
r.log.Info("extensionServerPolicies status subscriber shutting down")
649665
}()
@@ -685,6 +701,7 @@ func (r *gatewayAPIReconciler) updateStatusFromSubscriptions(ctx context.Context
685701
}),
686702
})
687703
},
704+
nil,
688705
)
689706
r.log.Info("envoyProxy status subscriber shutting down")
690707
}()

internal/xds/runner/runner.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ func (r *Runner) translateFromSubscription(sub <-chan watchable.Snapshot[string,
395395
}
396396
}
397397
},
398+
nil,
398399
)
399400
r.Logger.Info("subscriber shutting down")
400401
}

0 commit comments

Comments
 (0)