Prune old Web Push subscriptions

This commit is contained in:
Simon Ser 2023-03-17 00:00:50 +01:00
parent 02ed7aa308
commit 93af53c91d
3 changed files with 20 additions and 4 deletions

View File

@ -3249,8 +3249,6 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
}} }}
} }
// TODO: limit max number of subscriptions, prune old ones
if err := dc.user.srv.db.StoreWebPushSubscription(ctx, dc.user.ID, networkID, &newSub); err != nil { if err := dc.user.srv.db.StoreWebPushSubscription(ctx, dc.user.ID, networkID, &newSub); err != nil {
dc.logger.Printf("failed to store Web push subscription: %v", err) dc.logger.Printf("failed to store Web push subscription: %v", err)
return ircError{&irc.Message{ return ircError{&irc.Message{

View File

@ -38,6 +38,7 @@ var backlogTimeout = 10 * time.Second
var handleDownstreamMessageTimeout = 10 * time.Second var handleDownstreamMessageTimeout = 10 * time.Second
var downstreamRegisterTimeout = 30 * time.Second var downstreamRegisterTimeout = 30 * time.Second
var webpushCheckSubscriptionDelay = 24 * time.Hour var webpushCheckSubscriptionDelay = 24 * time.Hour
var webpushPruneSubscriptionDelay = 30 * 24 * time.Hour
var chatHistoryLimit = 1000 var chatHistoryLimit = 1000
var backlogLimit = 4000 var backlogLimit = 4000

21
user.go
View File

@ -484,7 +484,23 @@ func (net *network) broadcastWebPush(msg *irc.Message) {
return return
} }
// First, prune old subscriptions
var active []database.WebPushSubscription
for _, sub := range subs { for _, sub := range subs {
if time.Since(sub.UpdatedAt) <= webpushPruneSubscriptionDelay {
active = append(active, sub)
continue
}
if err := net.user.srv.db.DeleteWebPushSubscription(ctx, sub.ID); err != nil {
net.logger.Printf("failed to delete pruned Web Push subscription %q: %v", sub.Endpoint, err)
} else {
net.logger.Debugf("deleted pruned Web Push subscription %q", sub.Endpoint)
}
}
// Then broadcast the message
for _, sub := range active {
err := net.user.srv.sendWebPush(ctx, &webpush.Subscription{ err := net.user.srv.sendWebPush(ctx, &webpush.Subscription{
Endpoint: sub.Endpoint, Endpoint: sub.Endpoint,
Keys: webpush.Keys{ Keys: webpush.Keys{
@ -494,9 +510,10 @@ func (net *network) broadcastWebPush(msg *irc.Message) {
}, sub.Keys.VAPID, msg) }, sub.Keys.VAPID, msg)
if err == errWebPushSubscriptionExpired { if err == errWebPushSubscriptionExpired {
if err := net.user.srv.db.DeleteWebPushSubscription(ctx, sub.ID); err != nil { if err := net.user.srv.db.DeleteWebPushSubscription(ctx, sub.ID); err != nil {
net.logger.Printf("failed to delete expired Web Push subscription: %v", err) net.logger.Printf("failed to delete expired Web Push subscription %q: %v", sub.Endpoint, err)
} else {
net.logger.Debugf("deleted expired Web Push subscription %q", sub.Endpoint)
} }
net.logger.Debugf("deleted expired Web Push subscription %q", sub.Endpoint)
} else if err != nil { } else if err != nil {
net.logger.Printf("failed to send Web push notification to endpoint %q: %v", sub.Endpoint, err) net.logger.Printf("failed to send Web push notification to endpoint %q: %v", sub.Endpoint, err)
} }