mirror of
https://github.com/aljazceru/ditto.git
synced 2026-01-29 10:04:23 +01:00
stats: add a Semaphore when refreshing author stats
This commit is contained in:
15
src/stats.ts
15
src/stats.ts
@@ -1,3 +1,4 @@
|
||||
import { Semaphore } from '@lambdalisue/async';
|
||||
import { NKinds, NostrEvent, NStore } from '@nostrify/nostrify';
|
||||
import Debug from '@soapbox/stickynotes/debug';
|
||||
import { InsertQueryBuilder, Kysely } from 'kysely';
|
||||
@@ -253,13 +254,19 @@ async function countAuthorStats(
|
||||
};
|
||||
}
|
||||
|
||||
const lru = new LRUCache<string, true>({ max: 1000 });
|
||||
const authorStatsSemaphore = new Semaphore(10);
|
||||
const refreshedAuthors = new LRUCache<string, true>({ max: 1000 });
|
||||
|
||||
/** Calls `refreshAuthorStats` only once per author. */
|
||||
function refreshAuthorStatsDebounced(pubkey: string): void {
|
||||
if (lru.get(pubkey)) return;
|
||||
lru.set(pubkey, true);
|
||||
refreshAuthorStats(pubkey).catch(() => {});
|
||||
if (refreshedAuthors.get(pubkey)) {
|
||||
return;
|
||||
}
|
||||
|
||||
refreshedAuthors.set(pubkey, true);
|
||||
|
||||
authorStatsSemaphore
|
||||
.lock(() => refreshAuthorStats(pubkey).catch(() => {}));
|
||||
}
|
||||
|
||||
export { refreshAuthorStats, refreshAuthorStatsDebounced, updateStats };
|
||||
|
||||
Reference in New Issue
Block a user