mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-13 03:40:48 +00:00
Add rate limiting to push notification sending process (#7149)
This commit is contained in:
parent
afdbf5aed7
commit
993965df93
@ -38,6 +38,7 @@ import core, {
|
||||
generateId,
|
||||
MeasureContext,
|
||||
MixinUpdate,
|
||||
RateLimiter,
|
||||
Ref,
|
||||
RefTo,
|
||||
SortingOrder,
|
||||
@ -602,9 +603,12 @@ export async function createPushNotification (
|
||||
}
|
||||
|
||||
webpush.setVapidDetails(subject, publicKey, privateKey)
|
||||
const limiter = new RateLimiter(5)
|
||||
|
||||
for (const subscription of userSubscriptions) {
|
||||
void sendPushToSubscription(control, target, subscription, data)
|
||||
await limiter.exec(async () => {
|
||||
await sendPushToSubscription(control, target, subscription, data)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -621,7 +625,7 @@ async function sendPushToSubscription (
|
||||
} catch (err) {
|
||||
control.ctx.info('Cannot send push notification to', { user: targetUser, err })
|
||||
if (err instanceof WebPushError) {
|
||||
if (errorMessages.some((p) => (err as WebPushError).body.includes(p))) {
|
||||
if (errorMessages.some((p) => JSON.stringify((err as WebPushError).body).includes(p))) {
|
||||
const tx = control.txFactory.createTxRemoveDoc(subscription._class, subscription.space, subscription._id)
|
||||
await control.apply(control.ctx, [tx])
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user