deno.land / x / replicache@v10.0.0-beta.0 / persist / client-gc.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import type {LogContext} from '@rocicorp/logger';import type {ClientID} from '../sync/client-id';import type * as dag from '../dag/mod';import {ClientMap, noUpdates, updateClients} from './clients';import {initBgIntervalProcess} from './bg-interval';
const CLIENT_MAX_INACTIVE_IN_MS = 7 * 24 * 60 * 60 * 1000; // 7 daysconst GC_INTERVAL_MS = 5 * 60 * 1000; // 5 minutes
let latestGCUpdate: Promise<ClientMap> | undefined;export function getLatestGCUpdate(): Promise<ClientMap> | undefined { return latestGCUpdate;}
export function initClientGC( clientID: ClientID, dagStore: dag.Store, lc: LogContext, signal: AbortSignal,): void { initBgIntervalProcess( 'ClientGC', () => { latestGCUpdate = gcClients(clientID, dagStore); return latestGCUpdate; }, GC_INTERVAL_MS, lc, signal, );}
export async function gcClients( clientID: ClientID, dagStore: dag.Store,): Promise<ClientMap> { return updateClients(clients => { const now = Date.now(); const clientsAfterGC = Array.from(clients).filter( ([id, client]) => id === clientID /* never collect ourself */ || now - client.heartbeatTimestampMs <= CLIENT_MAX_INACTIVE_IN_MS, ); if (clientsAfterGC.length === clients.size) { return noUpdates; } return { clients: new Map(clientsAfterGC), }; }, dagStore);}
replicache

Version Info

Tagged at
2 years ago