Skip to content

Commit 4f33898

Browse files
committed
fix throttle logic
1 parent 501d631 commit 4f33898

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/worker/tasks/mineTransactionWorker.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,12 @@ const _notifyIfLowBalance = async (transaction: MinedTransaction) => {
280280
return;
281281
}
282282

283+
// Set a key with 5min TTL if it doesn't exist.
284+
// This effectively throttles this check once every 5min.
283285
const throttleKey = `webhook:${WebhooksEventTypes.BACKEND_WALLET_BALANCE}:${chainId}:${from}`;
284-
const isThrottled = (await redis.get(throttleKey)) !== null;
286+
const isThrottled =
287+
(await redis.set(throttleKey, "", "EX", 5 * 60, "NX")) === null;
285288
if (isThrottled) {
286-
// Skip if this wallet was checked recently.
287289
return;
288290
}
289291

@@ -312,9 +314,6 @@ const _notifyIfLowBalance = async (transaction: MinedTransaction) => {
312314
message: `LowBalance: The backend wallet ${from} on chain ${chainId} has ${toTokens(currentBalance, 18)} gas remaining.`,
313315
},
314316
});
315-
316-
// Don't check more than once every 5 minutes.
317-
await redis.setex(throttleKey, "", 5 * 60);
318317
} catch (e) {
319318
logger({
320319
level: "warn",

0 commit comments

Comments
 (0)