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:
parent
b6c0841291
commit
8f1f67f1f0
@ -257,6 +257,8 @@ type WebPushConfig struct {
|
|||||||
type WebPushSubscription struct {
|
type WebPushSubscription struct {
|
||||||
ID int64
|
ID int64
|
||||||
Endpoint string
|
Endpoint string
|
||||||
|
CreatedAt, UpdatedAt time.Time // read-only
|
||||||
|
|
||||||
Keys struct {
|
Keys struct {
|
||||||
Auth string
|
Auth string
|
||||||
P256DH string
|
P256DH string
|
||||||
|
@ -849,7 +849,7 @@ func (db *PostgresDB) ListWebPushSubscriptions(ctx context.Context, userID, netw
|
|||||||
}
|
}
|
||||||
|
|
||||||
rows, err := db.db.QueryContext(ctx, `
|
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"
|
FROM "WebPushSubscription"
|
||||||
WHERE "user" = $1 AND network IS NOT DISTINCT FROM $2`, userID, nullNetworkID)
|
WHERE "user" = $1 AND network IS NOT DISTINCT FROM $2`, userID, nullNetworkID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -860,7 +860,7 @@ func (db *PostgresDB) ListWebPushSubscriptions(ctx context.Context, userID, netw
|
|||||||
var subs []WebPushSubscription
|
var subs []WebPushSubscription
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var sub WebPushSubscription
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
subs = append(subs, sub)
|
subs = append(subs, sub)
|
||||||
|
@ -1067,7 +1067,7 @@ func (db *SqliteDB) ListWebPushSubscriptions(ctx context.Context, userID, networ
|
|||||||
}
|
}
|
||||||
|
|
||||||
rows, err := db.db.QueryContext(ctx, `
|
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
|
FROM WebPushSubscription
|
||||||
WHERE user = ? AND network IS ?`, userID, nullNetworkID)
|
WHERE user = ? AND network IS ?`, userID, nullNetworkID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1078,9 +1078,12 @@ func (db *SqliteDB) ListWebPushSubscriptions(ctx context.Context, userID, networ
|
|||||||
var subs []WebPushSubscription
|
var subs []WebPushSubscription
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var sub WebPushSubscription
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
sub.CreatedAt = createdAt.Time
|
||||||
|
sub.UpdatedAt = updatedAt.Time
|
||||||
subs = append(subs, sub)
|
subs = append(subs, sub)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3230,10 +3230,13 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
|
|||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateSub := true
|
||||||
oldSub := findWebPushSubscription(subs, endpoint)
|
oldSub := findWebPushSubscription(subs, endpoint)
|
||||||
if oldSub != nil {
|
if oldSub != nil {
|
||||||
// Update the old subscription instead of creating a new one
|
// Update the old subscription instead of creating a new one
|
||||||
newSub.ID = oldSub.ID
|
newSub.ID = oldSub.ID
|
||||||
|
// Rate-limit subscription checks
|
||||||
|
updateSub = time.Since(oldSub.UpdatedAt) > webpushCheckSubscriptionDelay || oldSub.Keys != newSub.Keys
|
||||||
}
|
}
|
||||||
|
|
||||||
var networkID int64
|
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
|
// Send a test Web Push message, to make sure the endpoint is valid
|
||||||
|
if updateSub {
|
||||||
err = dc.srv.sendWebPush(ctx, &webpush.Subscription{
|
err = dc.srv.sendWebPush(ctx, &webpush.Subscription{
|
||||||
Endpoint: newSub.Endpoint,
|
Endpoint: newSub.Endpoint,
|
||||||
Keys: webpush.Keys{
|
Keys: webpush.Keys{
|
||||||
@ -3269,6 +3273,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
|
|||||||
Params: []string{"WEBPUSH", "INTERNAL_ERROR", subcommand, "Internal error"},
|
Params: []string{"WEBPUSH", "INTERNAL_ERROR", subcommand, "Internal error"},
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dc.SendMessage(&irc.Message{
|
dc.SendMessage(&irc.Message{
|
||||||
Prefix: dc.srv.prefix(),
|
Prefix: dc.srv.prefix(),
|
||||||
|
@ -39,6 +39,7 @@ var upstreamMessageBurst = 10
|
|||||||
var backlogTimeout = 10 * time.Second
|
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 chatHistoryLimit = 1000
|
var chatHistoryLimit = 1000
|
||||||
var backlogLimit = 4000
|
var backlogLimit = 4000
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user