mirror of
https://github.com/axllent/mailpit.git
synced 2026-03-03 00:27:01 +00:00
Chore: Add support for webhook delay (#627)
This commit is contained in:
committed by
Ralph Slooten
parent
a87b2a9455
commit
9391b075d0
@@ -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")
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user