[notifications] Give each HTTP request its own context (#3569)

Give each HTTP request its own context to avoid timing out subsequent
notification requests if the first already times out.

### Pull Request Checklist

<!-- Please read
https://matrix-org.github.io/dendrite/development/contributing before
submitting your pull request -->

* [ ] I have added Go unit tests or [Complement integration
tests](https://github.com/matrix-org/complement) for this PR _or_ I have
justified why this PR doesn't need tests
* [x] Pull request includes a [sign off
below](https://element-hq.github.io/dendrite/development/contributing#sign-off)
_or_ I have already signed off privately

---------

Signed-off-by: Till Faelligen <2353100+S7evinK@users.noreply.github.com>
This commit is contained in:
Till 2025-05-16 17:35:30 +02:00 committed by GitHub
parent 9de69bc53a
commit 654fee9818
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -604,7 +604,9 @@ func (s *OutputRoomEventConsumer) notifyLocal(ctx context.Context, event *rstype
// ordering guarantees we must provide.
go func() {
// This background processing cannot be tied to a request.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
// We're generous with the "global" timeout, each HTTP request gets its own context with
// at most 30 seconds below.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
var rejected []*pushgateway.Device
@ -621,7 +623,10 @@ func (s *OutputRoomEventConsumer) notifyLocal(ctx context.Context, event *rstype
// device, rather than per URL. For now, we must
// notify each one separately.
for _, dev := range devices {
rej, err := s.notifyHTTP(ctx, event, url, format, []*pushgateway.Device{dev}, mem.Localpart, roomName, int(userNumUnreadNotifs))
// Give each HTTP request its own context.
httpCtx, httpCancel := context.WithTimeout(ctx, 30*time.Second)
rej, err := s.notifyHTTP(httpCtx, event, url, format, []*pushgateway.Device{dev}, mem.Localpart, roomName, int(userNumUnreadNotifs))
httpCancel()
if err != nil {
log.WithFields(log.Fields{
"event_id": event.EventID(),