Repository navigation
Fix: Reuse webhook connections to prevent dropped webhooks under load - #772
Conversation
Each webhook request created a new HTTP client and never read the response body, so no connection was reused. Under load this opened thousands of short-lived connections, and requests failed with connection resets. Use one shared client with a larger idle connection pool, drain the response body, and limit concurrent requests to 100. Requests over the limit wait rather than being dropped, so every message still sends exactly one webhook request.
|
Hi Roman, and thanks for this. Finding the per-request client and the unread response body as the cause is useful, and the shared client with a drained body is the right fix. I'd like to ask for one change: please remove the semaphore and keep the rest. WhyWith the semaphore, a request waiting for a slot has no timeout and there is no limit on how many can wait. Before this PR, every webhook goroutine lived at most the delay plus 5s. If the receiver is slow or hangs, each slot is held for up to 5s. That caps throughput at roughly 20 requests per second (100 slots over 5s), while waiting goroutines, each holding a marshalled payload, keep growing. They would also eventually send stale data. The cap bounds connections by making the waiters unbounded, which moves the problem instead of solving it. What I measuredI tested a scratch copy of the code with and without the semaphore, using 100 senders at about 3,600 msg/s, 10,000 messages per run:
I think that trade-off favours removing it. A slow receiver then behaves as it does today, with each request bounded by the 5s timeout, and the shared client still reduces connections by about three times. Changes to the test
If you think a cap on connections matters for slow receivers, let's discuss it here. Any bound on waiting would need its own timeout, and I'd want to decide separately whether dropping requests is acceptable. Thanks again for the PR and the clear test plan. |
Waiting for a free slot had no timeout and no limit on waiters, so a slow receiver would build up goroutines holding stale payloads. Keep the shared client and drained response body, which provide the connection reuse, and pace the sends in the test like real ingest.
|
Thanks, that's a fair point. I've removed the semaphore and renamed the constant to maxIdleConns, used only for the idle pool size. End to end without the semaphore, 20,000 messages via /api/v1/send at 100 concurrent senders, receiver at 1 ms and 200 ms:
It matches what you saw: with a responsive receiver the shared client does all the work, and with a slow one it still opens about 8× fewer connections than today. |
|
Thank you :) |
Hi,
This is a small one, related to our webhook discussion in #656. With
--webhook-limit 0and high message rates, our receiver kept missing webhooks. Mailpit logs showedconnection reset by peerandconnection refused. The reason is that every webhook creates a newhttp.Client, never reads the response body, and so never reuses a connection. Under load that means thousands of short-lived TCP connections at once.I kept the behaviour exactly as you described it in #656: the rate limit and delay work the same, every message still sends exactly one request, nothing is queued, dropped or retried. Only the connection handling changes.
Summary
server/webhook/webhook.go:http.Clientcreated in the existingonce.Do. Its transport is a clone ofhttp.DefaultTransport(same proxy, TLS and HTTP/2 settings as today) withMaxIdleConnsPerHostraised from the default 2, so connections are actually kept.I hardcoded 100 to keep it simple. I'm happy to make it a flag, change the number, or remove the limit entirely if you'd rather keep this to the shared client only.
Related issues
Refs #656.
Type of change
Test plan
go test ./...passes.TestSendDeliversEveryRequestReusingConnections: anhttptestserver counts requests and new connections, 2,000Send()calls withRateLimit = 0.connection reset by peerin the log)MP_WEBHOOK_URLpointing to a local counting receiver andMP_WEBHOOK_LIMIT=0, 20,000 messages through/api/v1/sendat 100 concurrent (Apple M1 Pro, go1.27.4):develop5c18887)Ingest throughput is unchanged (~3,600–3,700 msg/s in both runs).
AI assistance disclosure
I used Claude Code to find the cause, write the change and the test, and run the end-to-end comparison. I reviewed the change and the results.
Checklist