From b253ae8c8214629eba4aac0432b7e52055cfc37d Mon Sep 17 00:00:00 2001 From: Lucas Lara Date: Tue, 12 Sep 2023 19:52:45 +0200 Subject: [PATCH] Allow customizing the throttle interval for events --- README.md | 2 ++ cmd.go | 15 +++++++++++++++ config/options.go | 1 + loader.go | 2 +- 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 90f67234..c409d2e7 100644 --- a/README.md +++ b/README.md @@ -501,6 +501,8 @@ Usage of docker-proxy: Which mode this instance should run: standalone | controller | server --polling-interval duration Interval Caddy should manually check Docker for a new Caddyfile (default 30s) + --event-throttle-interval duration + Interval to throttle caddyfile updates triggered by docker events (default 100ms) --process-caddyfile Process Caddyfile before loading it, removing invalid servers (default true) --proxy-service-tasks diff --git a/cmd.go b/cmd.go index 0b78e766..872fb9ed 100644 --- a/cmd.go +++ b/cmd.go @@ -64,6 +64,9 @@ func init() { fs.Duration("polling-interval", 30*time.Second, "Interval caddy should manually check docker for a new caddyfile") + fs.Duration("event-throttle-interval", 100*time.Millisecond, + "Interval to throttle caddyfile updates triggered by docker events") + return fs }(), }) @@ -143,6 +146,7 @@ func createOptions(flags caddycmd.Flags) *config.Options { processCaddyfileFlag := flags.Bool("process-caddyfile") scanStoppedContainersFlag := flags.Bool("scan-stopped-containers") pollingIntervalFlag := flags.Duration("polling-interval") + eventThrottleIntervalFlag := flags.Duration("event-throttle-interval") modeFlag := flags.String("mode") controllerSubnetFlag := flags.String("controller-network") dockerSocketsFlag := flags.String("docker-sockets") @@ -254,5 +258,16 @@ func createOptions(flags caddycmd.Flags) *config.Options { options.PollingInterval = pollingIntervalFlag } + if eventThrottleIntervalEnv := os.Getenv("CADDY_DOCKER_EVENT_THROTTLE_INTERVAL"); eventThrottleIntervalEnv != "" { + if p, err := time.ParseDuration(eventThrottleIntervalEnv); err != nil { + log.Error("Failed to parse CADDY_DOCKER_EVENT_THROTTLE_INTERVAL", zap.String("CADDY_DOCKER_EVENT_THROTTLE_INTERVAL", eventThrottleIntervalEnv), zap.Error(err)) + options.EventThrottleInterval = pollingIntervalFlag + } else { + options.EventThrottleInterval = p + } + } else { + options.EventThrottleInterval = eventThrottleIntervalFlag + } + return options } diff --git a/config/options.go b/config/options.go index 97defc61..92e3e12e 100644 --- a/config/options.go +++ b/config/options.go @@ -17,6 +17,7 @@ type Options struct { ProcessCaddyfile bool ScanStoppedContainers bool PollingInterval time.Duration + EventThrottleInterval time.Duration Mode Mode Secret string ControllerNetwork *net.IPNet diff --git a/loader.go b/loader.go index 7777b899..ca875e66 100644 --- a/loader.go +++ b/loader.go @@ -209,7 +209,7 @@ func (dockerLoader *DockerLoader) listenEvents() { if update { dockerLoader.skipEvents[i] = true - dockerLoader.timer.Reset(100 * time.Millisecond) + dockerLoader.timer.Reset(dockerLoader.options.EventThrottleInterval) } case err := <-errorChan: cancel()