mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-08 00:22:14 +01:00
fix: graceful shutdown with asynchronous processes - fixes #1022
This commit is contained in:
@@ -173,10 +173,13 @@ func webhookMiddleware(w *Webhook) api.Middleware {
|
||||
}
|
||||
}
|
||||
|
||||
w.asyncCount.Add(1)
|
||||
|
||||
// As a webhook URL has been given, we handle the request in a
|
||||
// goroutine and return immediately.
|
||||
go func() {
|
||||
defer cancel()
|
||||
defer w.asyncCount.Add(-1)
|
||||
|
||||
// Call the next middleware in the chain.
|
||||
err := next(c)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package webhook
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/dlclark/regexp2"
|
||||
@@ -25,6 +26,7 @@ type Webhook struct {
|
||||
retryMinWait time.Duration
|
||||
retryMaxWait time.Duration
|
||||
clientTimeout time.Duration
|
||||
asyncCount atomic.Int64
|
||||
disable bool
|
||||
}
|
||||
|
||||
@@ -62,6 +64,7 @@ func (w *Webhook) Provision(ctx *gotenberg.Context) error {
|
||||
w.retryMaxWait = flags.MustDuration("webhook-retry-max-wait")
|
||||
w.clientTimeout = flags.MustDuration("webhook-client-timeout")
|
||||
w.disable = flags.MustBool("webhook-disable")
|
||||
w.asyncCount.Store(0)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -77,9 +80,15 @@ func (w *Webhook) Middlewares() ([]api.Middleware, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// AsyncCount returns the number of asynchronous requests.
|
||||
func (w *Webhook) AsyncCount() int64 {
|
||||
return w.asyncCount.Load()
|
||||
}
|
||||
|
||||
// Interface guards.
|
||||
var (
|
||||
_ gotenberg.Module = (*Webhook)(nil)
|
||||
_ gotenberg.Provisioner = (*Webhook)(nil)
|
||||
_ api.MiddlewareProvider = (*Webhook)(nil)
|
||||
_ gotenberg.Module = (*Webhook)(nil)
|
||||
_ gotenberg.Provisioner = (*Webhook)(nil)
|
||||
_ api.MiddlewareProvider = (*Webhook)(nil)
|
||||
_ api.AsynchronousCounter = (*Webhook)(nil)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user