From cbdbe3f605d93f8797db094052a798af54398621 Mon Sep 17 00:00:00 2001 From: Alex Hall Date: Mon, 16 May 2022 14:47:34 +0200 Subject: [PATCH] (core) Switch webhook secret cache from LRU to TTL so that unsubscribing can drain the queue Summary: Helps with cases such as https://grist.slack.com/archives/C02EGJ1FUCV/p1652196111066649?thread_ts=1651656433.171889&cid=C02EGJ1FUCV When a user unsubscribes from a webhook, the secret URL is deleted from the database, but as long as the doc was open it would continue retrying pending requests still in the queue for a long time, using the locally cached value without noticing the effect of unsubscribing. This change allows unsubscribing to have an effect more quickly so that problematic events can be removed from the queue. Test Plan: existing tests Reviewers: paulfitz Reviewed By: paulfitz Differential Revision: https://phab.getgrist.com/D3430 --- app/server/lib/Triggers.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/server/lib/Triggers.ts b/app/server/lib/Triggers.ts index 75325493..da6451fe 100644 --- a/app/server/lib/Triggers.ts +++ b/app/server/lib/Triggers.ts @@ -1,5 +1,6 @@ import {LocalActionBundle} from 'app/common/ActionBundle'; import {ActionSummary, TableDelta} from 'app/common/ActionSummary'; +import {MapWithTTL} from 'app/common/AsyncCreate'; import {delay} from 'app/common/delay'; import {fromTableDataAction, RowRecord, TableColValues, TableDataAction} from 'app/common/DocActions'; import {StringUnion} from 'app/common/StringUnion'; @@ -11,7 +12,6 @@ import {makeExceptionalDocSession} from 'app/server/lib/DocSession'; import * as log from 'app/server/lib/log'; import {promisifyAll} from 'bluebird'; import * as _ from 'lodash'; -import * as LRUCache from 'lru-cache'; import fetch from 'node-fetch'; import {createClient, Multi, RedisClient} from 'redis'; @@ -69,6 +69,8 @@ interface Task { const MAX_QUEUE_SIZE = 1000; +const WEBHOOK_CACHE_TTL = 10000; + // Processes triggers for records changed as described in action bundles. // initiating webhooks and automations. // The interesting stuff starts in the handle() method. @@ -87,7 +89,7 @@ export class DocTriggers { private _webHookEventQueue: WebHookEvent[] = []; // DB cache for webhook secrets - private _webhookCache = new LRUCache({max: 1000}); + private _webhookCache = new MapWithTTL(WEBHOOK_CACHE_TTL); // Set to true by shutdown(). // Indicates that loops (especially for sending requests) should stop.