Skip to content

Commit

Permalink
Cleanup worker cache on delete (#2718)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianjoel authored Aug 31, 2023
1 parent 46992a6 commit f135e6d
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { ModelRequest } from 'src/app/domain/interfaces/model-request';
import { HttpStreamEndpointService } from 'src/app/gateways/http-stream';
import { formatQueryParams } from 'src/app/infrastructure/definitions/http';
import { djb2hash } from 'src/app/infrastructure/utils';
import { fqidFromCollectionAndId } from 'src/app/infrastructure/utils/transform-functions';
import { SharedWorkerService } from 'src/app/openslides-main-module/services/shared-worker.service';
import {
AutoupdateAuthChange,
AutoupdateCleanupCache,
AutoupdateCloseStream,
AutoupdateNewUser,
AutoupdateOpenStream,
Expand Down Expand Up @@ -172,6 +174,31 @@ export class AutoupdateCommunicationService {
} as AutoupdateCloseStream);
}

/**
* Notifies the worker about deleted fqids
*
* @param streamId Id of the stream
* @param deletedData map op collections and ids to be deleted
*/
public cleanupCollections(streamId: Id, deletedData: { [collection: string]: Id[] }): void {
const deletedFqids: string[] = [];
for (const coll of Object.keys(deletedData)) {
for (const id of deletedData[coll]) {
deletedFqids.push(fqidFromCollectionAndId(coll, id));
}
}

if (deletedFqids.length) {
this.sharedWorker.sendMessage(`autoupdate`, {
action: `cleanup-cache`,
params: {
streamId,
deletedFqids
}
} as AutoupdateCleanupCache);
}
}

/**
* @returns Observable containing messages from autoupdate
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ export class AutoupdateService {
changedFullListModels: fullListUpdateCollections,
deletedModels: {}
})
.then(() => {
.then(deletedModels => {
this.communication.cleanupCollections(requestId, deletedModels);

if (this._resolveDataReceived[requestId]) {
this._resolveDataReceived[requestId](modelData);
delete this._resolveDataReceived[requestId];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class ViewModelStoreUpdateService {
changedFullListModels,
deletedModels,
patch
}: UpdatePatch): Promise<void> {
}: UpdatePatch): Promise<DeletedModels> {
const _deletedModels: DeletedModels = {};
const _changedModels: ChangedModels = {};

Expand Down Expand Up @@ -86,6 +86,7 @@ export class ViewModelStoreUpdateService {
}

await this.doCollectionUpdates(_changedModels, _deletedModels);
return _deletedModels;
}

private createCollectionUpdate(
Expand Down
24 changes: 24 additions & 0 deletions client/src/app/worker/autoupdate-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,30 @@ export class AutoupdateStream {
this.clearSubscriptions();
}

/**
* Removes fqids from the cache.
*
* @param fqids list of fqids to delete
*/
public removeFqids(fqids: string[]): void {
let lastHit: string | null = null;
for (const key of Object.keys(this._currentData)) {
if (lastHit === null || !key.startsWith(fqids[lastHit] + `/`)) {
lastHit = null;
for (let i = 0; i < fqids.length; i++) {
if (key.startsWith(fqids[i] + `/`)) {
lastHit = key;
break;
}
}
}

if (lastHit !== null) {
delete this._currentData[key];
}
}
}

public clearSubscriptions(): void {
this._currentData = null;
}
Expand Down
10 changes: 10 additions & 0 deletions client/src/app/worker/interfaces-autoupdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export interface AutoupdateCloseStream extends WorkerMessageContent {
params: AutoupdateCloseStreamParams;
}

export interface AutoupdateCleanupCacheParams {
streamId: number;
deletedFqids: string[];
}

export interface AutoupdateCleanupCache extends WorkerMessageContent {
action: 'cleanup-cache';
params: AutoupdateCleanupCacheParams;
}

export interface AutoupdateSetConnectionStatusParams {
status: 'online' | 'offline';
}
Expand Down
13 changes: 13 additions & 0 deletions client/src/app/worker/sw-autoupdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { environment } from 'src/environments/environment';
import { AutoupdateStreamPool } from './autoupdate-stream-pool';
import { AutoupdateSubscription } from './autoupdate-subscription';
import {
AutoupdateCleanupCacheParams,
AutoupdateCloseStreamParams,
AutoupdateOpenStreamParams,
AutoupdateSetEndpointParams
Expand Down Expand Up @@ -134,6 +135,15 @@ function closeConnection(ctx: MessagePort, params: AutoupdateCloseStreamParams):
subscription.closePort(ctx);
}

function cleanupStream(params: AutoupdateCleanupCacheParams): void {
const subscription = autoupdatePool.getSubscriptionById(params.streamId);
if (!subscription) {
return;
}

subscription.stream.removeFqids(params.deletedFqids);
}

let currentlyOnline = navigator.onLine;
function updateOnlineStatus(): void {
if (currentlyOnline === navigator.onLine) {
Expand Down Expand Up @@ -180,6 +190,9 @@ export function addAutoupdateListener(context: any): void {
case `reconnect-force`:
autoupdatePool.reconnectAll(false);
break;
case `cleanup-cache`:
cleanupStream(params);
break;
case `enable-debug`:
registerDebugCommands();
break;
Expand Down

0 comments on commit f135e6d

Please sign in to comment.