Skip to content

Commit d559e80

Browse files
Delete stale client metadata (#1119)
1 parent 9941072 commit d559e80

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ export class IndexedDbPersistence implements Persistence {
317317
return this.releasePrimaryLeaseIfHeld(txn);
318318
} else if (this.isPrimary) {
319319
return this.acquireOrExtendPrimaryLease(txn).next(() =>
320-
this.maybeGarbageCollectRemoteDocumentChangelog(txn)
320+
this.maybeGarbageCollectMultiClientState(txn)
321321
);
322322
}
323323
});
@@ -333,10 +333,10 @@ export class IndexedDbPersistence implements Persistence {
333333

334334
/**
335335
* If the garbage collection threshold has passed, prunes the
336-
* RemoteDocumentChanges store based on the metadata state of all existing
337-
* clients.
336+
* RemoteDocumentChanges and the ClientMetadata store based on the last update
337+
* time of all clients.
338338
*/
339-
private maybeGarbageCollectRemoteDocumentChangelog(
339+
private maybeGarbageCollectMultiClientState(
340340
txn: SimpleDbTransaction
341341
): PersistencePromise<void> {
342342
assert(this.isPrimary, 'GC should only run in the primary client');
@@ -352,12 +352,25 @@ export class IndexedDbPersistence implements Persistence {
352352
return clientMetadataStore(txn)
353353
.loadAll()
354354
.next(existingClients => {
355-
// TODO(multitab): Remove outdated client metadata
356-
let activeClients = this.filterActiveClients(
355+
const activeClients = this.filterActiveClients(
357356
existingClients,
358357
CLIENT_STATE_GARBAGE_COLLECTION_THRESHOLD_MS
359358
);
360359

360+
// Delete metadata for client that are no longer considered active.
361+
let p = PersistencePromise.resolve();
362+
existingClients
363+
.filter(client => activeClients.indexOf(client) === -1)
364+
.forEach(inactiveClient => {
365+
p = p.next(() =>
366+
clientMetadataStore(txn).delete(inactiveClient.clientId)
367+
);
368+
});
369+
return p.next(() => activeClients);
370+
})
371+
.next(activeClients => {
372+
// Retrieve the minimum change ID from the set of active clients.
373+
361374
// The primary client doesn't read from the document change log,
362375
// and hence we exclude it when we determine the minimum
363376
// `lastProcessedDocumentChangeId`.

0 commit comments

Comments
 (0)