Skip to content

Fix: Reuse webhook connections to prevent dropped webhooks under load - #772

Merged
axllent merged 2 commits into
axllent:developfrom
rurbanovich:perf/webhook-client
Nov 14, 2026
Merged

axllent merged 2 commits into
axllent:developfrom
rurbanovich:perf/webhook-client

Conversation

@rurbanovich

Copy link
Copy Markdown
Contributor

Hi,

This is a small one, related to our webhook discussion in #656. With --webhook-limit 0 and high message rates, our receiver kept missing webhooks. Mailpit logs showed connection reset by peer and connection refused. The reason is that every webhook creates a new http.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:

  • One shared http.Client created in the existing once.Do. Its transport is a clone of http.DefaultTransport (same proxy, TLS and HTTP/2 settings as today) with MaxIdleConnsPerHost raised from the default 2, so connections are actually kept.
  • The response body is drained (up to 1MB) before closing, which is what lets Go reuse the connection.
  • At most 100 requests are in flight at once (a small channel used as a semaphore). Requests over the limit wait in their goroutine, as they do now, and are not dropped. The 5s timeout only starts once a request is actually sent, so waiting doesn't cause timeouts.

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

  • Bug fix
  • New feature
  • Refactor / code cleanup
  • Documentation
  • Other (please describe):

Test plan

  • go test ./... passes.
  • New TestSendDeliversEveryRequestReusingConnections: an httptest server counts requests and new connections, 2,000 Send() calls with RateLimit = 0.
    • before: 1,105 / 2,000 received (connection reset by peer in the log)
    • after: 2,000 / 2,000 received, over about 100 connections
  • End to end: Mailpit with MP_WEBHOOK_URL pointing to a local counting receiver and MP_WEBHOOK_LIMIT=0, 20,000 messages through /api/v1/send at 100 concurrent (Apple M1 Pro, go1.27.4):
messages webhooks received TCP connections webhook errors in log
before (develop 5c18887) 20,000 19,853 302 147
after 20,000 20,000 65 0

Ingest throughput is unchanged (~3,600–3,700 msg/s in both runs).

AI assistance disclosure

  • No AI was used
  • Assisted — AI was used for suggestions, explanations, or small snippets, but the code was primarily written and understood by me
  • Extensive — AI generated a significant portion of the code in this PR

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

  • I have tested my changes locally
  • I have read and understood all code being submitted (including any AI-generated portions)
  • Documentation has been updated where relevant (not applicable: no new options)

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.
@axllent

axllent commented Nov 13, 2026

Copy link
Copy Markdown
Owner

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.

Why

With 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 measured

I 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:

Receiver latency develop PR as submitted PR without semaphore
1ms 6,442 connections 83 connections 79 connections
200ms 7,395 connections 100 connections 2,464 connections
  • Responsive receiver (1ms): the shared client and drain account for the whole improvement, and the semaphore adds nothing.
  • Slow receiver (200ms): the semaphore does cap connections, but the run took 23s against about 6s of ingest because requests queue.

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

  • Remove sem and its acquire and release.
  • Keep maxConcurrent (but rename it maxIdleConns) for the idle pool size only.
  • TestSendDeliversEveryRequestReusingConnections fires 2,000 sends in a tight loop, so without the semaphore it opens hundreds to thousands of connections and fails its 2*maxConcurrent assertion. Please pace the sends, for example with a fixed number of sender goroutines each sending in a loop, so the test matches real ingest. The "before" run should still fail it, and the "after" run should keep every request delivered over a small number of connections.
  • Please also re-run your end-to-end comparison without the semaphore and share the received, connection and error counts.

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.
@rurbanovich

Copy link
Copy Markdown
Contributor Author

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:

Receiver latency develop PR as submitted PR without semaphore
1 ms 20,000 received / 4,299 conns / 0 errors 20,000 / 206 / 0 20,000 / 196 / 0
200 ms 20,000 / 8,265 / 0 20,000 / 280 / 0 20,000 / 1,014 / 0

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.

@axllent

axllent commented Nov 14, 2026

Copy link
Copy Markdown
Owner

Thank you :)

@axllent
axllent merged commit cf90799 into axllent:develop Nov 14, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants