Rate limit Web Push checks

No need to re-check that a Web Push subscription is valid every
time a downstream connects. Mobile devices may reconnect pretty
frequently.

Check at most once a day.
This commit is contained in:
Simon Ser 2023-02-18 13:23:02 +01:00
parent b6c0841291
commit 8f1f67f1f0
5 changed files with 42 additions and 31 deletions

View File

@ -257,6 +257,8 @@ type WebPushConfig struct {
type WebPushSubscription struct {
ID int64
Endpoint string
CreatedAt, UpdatedAt time.Time // read-only
Keys struct {
Auth string
P256DH string

View File

@ -849,7 +849,7 @@ func (db *PostgresDB) ListWebPushSubscriptions(ctx context.Context, userID, netw
}
rows, err := db.db.QueryContext(ctx, `
SELECT id, endpoint, key_auth, key_p256dh, key_vapid
SELECT id, endpoint, created_at, updated_at, key_auth, key_p256dh, key_vapid
FROM "WebPushSubscription"
WHERE "user" = $1 AND network IS NOT DISTINCT FROM $2`, userID, nullNetworkID)
if err != nil {
@ -860,7 +860,7 @@ func (db *PostgresDB) ListWebPushSubscriptions(ctx context.Context, userID, netw
var subs []WebPushSubscription
for rows.Next() {
var sub WebPushSubscription
if err := rows.Scan(&sub.ID, &sub.Endpoint, &sub.Keys.Auth, &sub.Keys.P256DH, &sub.Keys.VAPID); err != nil {
if err := rows.Scan(&sub.ID, &sub.Endpoint, &sub.CreatedAt, &sub.UpdatedAt, &sub.Keys.Auth, &sub.Keys.P256DH, &sub.Keys.VAPID); err != nil {
return nil, err
}
subs = append(subs, sub)

View File

@ -1067,7 +1067,7 @@ func (db *SqliteDB) ListWebPushSubscriptions(ctx context.Context, userID, networ
}
rows, err := db.db.QueryContext(ctx, `
SELECT id, endpoint, key_auth, key_p256dh, key_vapid
SELECT id, endpoint, created_at, updated_at, key_auth, key_p256dh, key_vapid
FROM WebPushSubscription
WHERE user = ? AND network IS ?`, userID, nullNetworkID)
if err != nil {
@ -1078,9 +1078,12 @@ func (db *SqliteDB) ListWebPushSubscriptions(ctx context.Context, userID, networ
var subs []WebPushSubscription
for rows.Next() {
var sub WebPushSubscription
if err := rows.Scan(&sub.ID, &sub.Endpoint, &sub.Keys.Auth, &sub.Keys.P256DH, &sub.Keys.VAPID); err != nil {
var createdAt, updatedAt sqliteTime
if err := rows.Scan(&sub.ID, &sub.Endpoint, &createdAt, &updatedAt, &sub.Keys.Auth, &sub.Keys.P256DH, &sub.Keys.VAPID); err != nil {
return nil, err
}
sub.CreatedAt = createdAt.Time
sub.UpdatedAt = updatedAt.Time
subs = append(subs, sub)
}

View File

@ -3230,10 +3230,13 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
}}
}
updateSub := true
oldSub := findWebPushSubscription(subs, endpoint)
if oldSub != nil {
// Update the old subscription instead of creating a new one
newSub.ID = oldSub.ID
// Rate-limit subscription checks
updateSub = time.Since(oldSub.UpdatedAt) > webpushCheckSubscriptionDelay || oldSub.Keys != newSub.Keys
}
var networkID int64
@ -3242,6 +3245,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
}
// Send a test Web Push message, to make sure the endpoint is valid
if updateSub {
err = dc.srv.sendWebPush(ctx, &webpush.Subscription{
Endpoint: newSub.Endpoint,
Keys: webpush.Keys{
@ -3269,6 +3273,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
Params: []string{"WEBPUSH", "INTERNAL_ERROR", subcommand, "Internal error"},
}}
}
}
dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(),

View File

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