From 9391b075d0f3363be421abdad88af5c589a9f274 Mon Sep 17 00:00:00 2001 From: Roman Urbanovich Date: Fri, 23 Jan 2026 13:50:35 +0300 Subject: [PATCH] Chore: Add support for webhook delay (#627) --- cmd/root.go | 4 ++++ server/webhook/webhook.go | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/cmd/root.go b/cmd/root.go index 6327128..7030ba4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -160,6 +160,7 @@ func init() { // Webhook rootCmd.Flags().StringVar(&config.WebhookURL, "webhook-url", config.WebhookURL, "Send a webhook request for new messages") rootCmd.Flags().IntVar(&webhook.RateLimit, "webhook-limit", webhook.RateLimit, "Limit webhook requests per second") + rootCmd.Flags().IntVar(&webhook.Delay, "webhook-delay", webhook.Delay, "Delay in seconds before sending webhook requests (0 = no delay)") // DEPRECATED FLAG 2024/04/12 - but will not be removed to maintain backwards compatibility rootCmd.Flags().StringVar(&config.Database, "db-file", config.Database, "Database file to store persistent data") @@ -387,6 +388,9 @@ func initConfigFromEnv() { if len(os.Getenv("MP_WEBHOOK_LIMIT")) > 0 { webhook.RateLimit, _ = strconv.Atoi(os.Getenv("MP_WEBHOOK_LIMIT")) } + if len(os.Getenv("MP_WEBHOOK_DELAY")) > 0 { + webhook.Delay, _ = strconv.Atoi(os.Getenv("MP_WEBHOOK_DELAY")) + } // Demo mode config.DemoMode = getEnabledFromEnv("MP_DEMO_MODE") diff --git a/server/webhook/webhook.go b/server/webhook/webhook.go index 0b60d76..7f58376 100644 --- a/server/webhook/webhook.go +++ b/server/webhook/webhook.go @@ -16,6 +16,10 @@ var ( // RateLimit is the minimum number of seconds between requests RateLimit = 1 + // Delay is the number of seconds to wait before sending each webhook request + // This can allow for other processing to complete before the webhook is triggered. + Delay = 0 + rl rate.Sometimes rateLimiterSet bool @@ -38,6 +42,11 @@ func Send(msg any) { } go func() { + // Apply delay if configured + if Delay > 0 { + time.Sleep(time.Duration(Delay) * time.Second) + } + rl.Do(func() { b, err := json.Marshal(msg) if err != nil {