Skip to content

Commit

Permalink
Add timeouts for portal event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Mar 20, 2024
1 parent dad3dbc commit 473c9d5
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,22 @@ func (portal *Portal) handleMessageLoopItem(msg PortalMessage) {
defer portal.forwardBackfillLock.Unlock()
switch {
case msg.evt != nil:
portal.handleMessage(msg.source, msg.evt, msg.raw)
doneChan := make(chan struct{})
go func() {
defer close(doneChan)
portal.handleMessage(msg.source, msg.evt, msg.raw)
}()
timer := time.NewTimer(1 * time.Minute)
select {
case <-doneChan:
if !timer.Stop() {
<-timer.C
}
case <-timer.C:
portal.zlog.Error().
Str("message_id", msg.evt.MessageID).
Msg("Google Messages event handling is taking over a minute, unblocking loop")
}
default:
portal.zlog.Warn().Interface("portal_message", msg).Msg("Unexpected PortalMessage with no message")
}
Expand All @@ -330,17 +345,32 @@ func (portal *Portal) handleMatrixMessageLoopItem(msg PortalMatrixMessage) {
portalQueue: time.Since(msg.receivedAt),
totalReceive: time.Since(evtTS),
}
switch msg.evt.Type {
case event.EventMessage, event.EventSticker:
portal.HandleMatrixMessage(msg.user, msg.evt, timings)
case event.EventReaction:
portal.HandleMatrixReaction(msg.user, msg.evt)
case event.EventRedaction:
portal.HandleMatrixRedaction(msg.user, msg.evt)
default:
portal.zlog.Warn().
Str("event_type", msg.evt.Type.Type).
Msg("Unsupported event type in portal message channel")
doneChan := make(chan struct{})
go func() {
defer close(doneChan)
switch msg.evt.Type {
case event.EventMessage, event.EventSticker:
portal.HandleMatrixMessage(msg.user, msg.evt, timings)
case event.EventReaction:
portal.HandleMatrixReaction(msg.user, msg.evt)
case event.EventRedaction:
portal.HandleMatrixRedaction(msg.user, msg.evt)
default:
portal.zlog.Warn().
Str("event_type", msg.evt.Type.Type).
Msg("Unsupported event type in portal message channel")
}
}()
timer := time.NewTimer(1 * time.Minute)
select {
case <-doneChan:
if !timer.Stop() {
<-timer.C
}
case <-timer.C:
portal.zlog.Error().
Stringer("event_id", msg.evt.ID).
Msg("Matrix event handling is taking over a minute, unblocking loop")
}
}

Expand Down

0 comments on commit 473c9d5

Please sign in to comment.