Chore: Add support for webhook delay (#627)

This commit is contained in:
Roman Urbanovich
2026-01-23 13:50:35 +03:00
committed by Ralph Slooten
parent a87b2a9455
commit 9391b075d0
2 changed files with 13 additions and 0 deletions

View File

@@ -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")

View File

@@ -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 {