From 65f0b2367ef9c6cc2856524e919ef415cc147e95 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 17 Aug 2022 16:09:12 +0200 Subject: [PATCH] Broadcast Web Push subscriptions in a new goroutine --- downstream.go | 2 +- upstream.go | 4 ++-- user.go | 9 ++++++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/downstream.go b/downstream.go index 6336eed..1c95e0c 100644 --- a/downstream.go +++ b/downstream.go @@ -3060,7 +3060,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. // TODO: only broadcast if draft/read-marker has been negotiated // TODO: use lower priority network.pushTargets.Del(entity) - network.broadcastWebPush(ctx, &irc.Message{ + go network.broadcastWebPush(&irc.Message{ Command: "MARKREAD", Params: []string{entity, timestampStr}, }) diff --git a/upstream.go b/upstream.go index d731b3d..3c22173 100644 --- a/upstream.go +++ b/upstream.go @@ -547,7 +547,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err } if highlight || uc.isOurNick(target) { - uc.network.broadcastWebPush(ctx, msg) + go uc.network.broadcastWebPush(msg) uc.network.pushTargets.Add(bufferName) } @@ -1551,7 +1551,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err }) if weAreInvited { - uc.network.broadcastWebPush(ctx, msg) + go uc.network.broadcastWebPush(msg) } case irc.RPL_INVITING: var nick, channel string diff --git a/user.go b/user.go index 8c072f5..6d93bc2 100644 --- a/user.go +++ b/user.go @@ -456,7 +456,14 @@ func (net *network) autoSaveSASLPlain(ctx context.Context, username, password st } } -func (net *network) broadcastWebPush(ctx context.Context, msg *irc.Message) { +// broadcastWebPush broadcasts a Web Push message for the given IRC message. +// +// Broadcasting the message to all Web Push endpoints might take a while, so +// callers should call this function in a new goroutine. +func (net *network) broadcastWebPush(msg *irc.Message) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) + defer cancel() + subs, err := net.user.srv.db.ListWebPushSubscriptions(ctx, net.user.ID, net.ID) if err != nil { net.logger.Printf("failed to list Web push subscriptions: %v", err)