fix runtime tests: close watcher & set default GracefulShutdownPeriod (#7991)

* runtime server: close watcher
* add default GracefulShutdownPeriod to avoid immediate context deadline exceed

Signed-off-by: Max Qian <zq2@illinois.edu>
This commit is contained in:
Q
2025-10-27 13:07:55 -05:00
committed by GitHub
parent 2a5ecb4127
commit bafd4db332
+25 -17
View File
@@ -298,10 +298,11 @@ type LoggingConfig struct {
// NewParams returns a new Params object.
func NewParams() Params {
return Params{
Output: os.Stdout,
BundleMode: false,
EnableVersionCheck: false,
Brand: "OPA", // default
Output: os.Stdout,
BundleMode: false,
EnableVersionCheck: false,
GracefulShutdownPeriod: 1,
Brand: "OPA", // default
}
}
@@ -912,20 +913,27 @@ func (rt *Runtime) startWatcher(ctx context.Context, paths []string, onReload fu
}
func (rt *Runtime) readWatcher(ctx context.Context, watcher *fsnotify.Watcher, paths []string, onReload func(time.Duration, error)) {
for evt := range watcher.Events {
removalMask := fsnotify.Remove | fsnotify.Rename
mask := fsnotify.Create | fsnotify.Write | removalMask
if (evt.Op & mask) != 0 {
rt.logger.WithFields(map[string]any{
"event": evt.String(),
}).Debug("Registered file event.")
t0 := time.Now()
removed := ""
if (evt.Op & removalMask) != 0 {
removed = evt.Name
for {
select {
case evt := <-watcher.Events:
removalMask := fsnotify.Remove | fsnotify.Rename
mask := fsnotify.Create | fsnotify.Write | removalMask
if (evt.Op & mask) != 0 {
rt.logger.WithFields(map[string]any{
"event": evt.String(),
}).Debug("Registered file event.")
t0 := time.Now()
removed := ""
if (evt.Op & removalMask) != 0 {
removed = evt.Name
}
err := rt.processWatcherUpdate(ctx, paths, removed)
onReload(time.Since(t0), err)
}
err := rt.processWatcherUpdate(ctx, paths, removed)
onReload(time.Since(t0), err)
case <-ctx.Done():
watcher.Close()
return
}
}
}