From 2c5a5396da57ede2c1bece29db20b8e205cd1360 Mon Sep 17 00:00:00 2001 From: Bossett Date: Wed, 4 Sep 2024 23:21:50 +0000 Subject: [PATCH] added basic limit on number of running events --- src/subscription.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/subscription.ts b/src/subscription.ts index 25947c04..71004d4b 100644 --- a/src/subscription.ts +++ b/src/subscription.ts @@ -48,6 +48,8 @@ export class FirehoseSubscription extends FirehoseSubscriptionBase { public authorList: string[] public intervalId: NodeJS.Timer + private runningEvents = 0 + async handleEvent(evt: RepoEvent) { if (!isCommit(evt)) return @@ -64,6 +66,14 @@ export class FirehoseSubscription extends FirehoseSubscriptionBase { if (!ops) return + const delay = (ms: number) => new Promise((res) => setTimeout(res, ms)) + + while (this.runningEvents > 128) { + await delay(1000) + } + + this.runningEvents++ + const postsToDelete = ops.posts.deletes.map((del) => del.uri) // Transform posts in parallel @@ -124,5 +134,7 @@ export class FirehoseSubscription extends FirehoseSubscriptionBase { await this.db.replaceOneURI('post', to_insert.uri, to_insert) }) } + + this.runningEvents-- } }