mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
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:
+25
-17
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user