From b17c230dad71cbcf1014c47fcee36a02902af77c Mon Sep 17 00:00:00 2001 From: Daniel Meyer <8926560+pubkey@users.noreply.github.com> Date: Fri, 7 Apr 2023 01:18:46 +0200 Subject: [PATCH] Feature/remote modes (#4611) * ADD `mode` option to remote storage * ADD default mode is storage * FIX tests * FIX typo * FIX(remote-storage) one mode --- CHANGELOG.md | 2 +- orga/performance-trackings.md | 30 +++++++++++++------ .../storage-remote/message-channel-cache.ts | 7 +++-- .../storage-remote/rx-storage-remote.ts | 18 +++++++++-- test/unit/rx-storage-remote.test.ts | 18 +++++++++++ 5 files changed, 60 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02fb5a60521..4c084fe616f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # RxDB Changelog - +- FIX(remote-storage) `mode: 'one'` option must reuse the RemoteMessageChannel diff --git a/orga/performance-trackings.md b/orga/performance-trackings.md index a6e1f72f2ba..243c8b7cd4a 100644 --- a/orga/performance-trackings.md +++ b/orga/performance-trackings.md @@ -1259,15 +1259,27 @@ BEFORE: LOG LOG: 'performanceResult: 34.1' -bulkWrite(20000) 1 - 290.4000000022352 -file-system-access.worker.js:9220 bulkWrite(20000) 2 - 352.5 -file-system-access.worker.js:9230 bulkWrite(20000) 3 - 352.6000000014901 -context.js:265 . -file-system-access.worker.js:9236 bulkWrite(20000) 4 - 461.6000000014901 -file-system-access.worker.js:9252 bulkWrite(20000) 5 - 1091.6000000014901 -context.js:265 . -file-system-access.worker.js:9257 bulkWrite(20000) 6 - 1428.9000000022352 -context.js:265 +AFTER: +'performanceResult: 28.6 + + + +FIXED TEST!! + +BEFORE: +performanceResult: 87.94' + +AFTER: +performanceResult: 83.39 + + + + + + +AFTER fixing merging sorted array + +AFTER2: ### Query diff --git a/src/plugins/storage-remote/message-channel-cache.ts b/src/plugins/storage-remote/message-channel-cache.ts index d1dde5c8b7b..2ddb66c68c1 100644 --- a/src/plugins/storage-remote/message-channel-cache.ts +++ b/src/plugins/storage-remote/message-channel-cache.ts @@ -13,6 +13,7 @@ export type RemoteMessageChannelCacheItem = { cacheKey: string; messageChannel: Promise; refCount: number; + keepAlive: boolean; }; export const MESSAGE_CHANNEL_CACHE_BY_IDENTIFIER = new Map>(); @@ -30,7 +31,8 @@ function getMessageChannelCache( export function getMessageChannel( settings: RxStorageRemoteSettings, - cacheKeys: string[] + cacheKeys: string[], + keepAlive: boolean = false ): Promise { const cacheKey = getCacheKey(settings, cacheKeys); const cacheItem = getFromMapOrCreate( @@ -40,6 +42,7 @@ export function getMessageChannel( const newCacheItem: RemoteMessageChannelCacheItem = { identifier: settings.identifier, cacheKey, + keepAlive, refCount: 1, messageChannel: settings.messageChannelCreator() .then((messageChannel) => { @@ -62,7 +65,7 @@ export function closeMessageChannel( ): Promise { const cacheItem = getFromMapOrThrow(CACHE_ITEM_BY_MESSAGE_CHANNEL, messageChannel); cacheItem.refCount = cacheItem.refCount - 1; - if (cacheItem.refCount === 0) { + if (cacheItem.refCount === 0 && !cacheItem.keepAlive) { getMessageChannelCache(cacheItem.identifier).delete(cacheItem.cacheKey); return messageChannel.close(); } else { diff --git a/src/plugins/storage-remote/rx-storage-remote.ts b/src/plugins/storage-remote/rx-storage-remote.ts index 2e2f5c9a5fb..fb3f259d2e7 100644 --- a/src/plugins/storage-remote/rx-storage-remote.ts +++ b/src/plugins/storage-remote/rx-storage-remote.ts @@ -28,6 +28,7 @@ import { import type { MessageFromRemote, MessageToRemote, + RemoteMessageChannel, RxStorageRemoteInternals, RxStorageRemoteSettings } from './storage-remote-types'; @@ -39,10 +40,18 @@ export class RxStorageRemote implements RxStorage public readonly name: string = 'remote'; private seed: string = randomCouchString(10); private lastRequestId: number = 0; + public messageChannelIfOneMode?: Promise; constructor( public readonly settings: RxStorageRemoteSettings ) { this.statics = settings.statics; + if (settings.mode === 'one') { + this.messageChannelIfOneMode = getMessageChannel( + settings, + [], + true + ); + } } public getRequestId() { @@ -68,9 +77,12 @@ export class RxStorageRemote implements RxStorage case 'storage': cacheKeys.push('seed-' + this.seed); } - const messageChannel = await getMessageChannel( - this.settings, - cacheKeys + const messageChannel = await (this.messageChannelIfOneMode ? + this.messageChannelIfOneMode : + getMessageChannel( + this.settings, + cacheKeys + ) ); const requestId = this.getRequestId(); diff --git a/test/unit/rx-storage-remote.test.ts b/test/unit/rx-storage-remote.test.ts index 6cb15b94ec4..95b4d481376 100644 --- a/test/unit/rx-storage-remote.test.ts +++ b/test/unit/rx-storage-remote.test.ts @@ -104,8 +104,26 @@ config.parallel('rx-storage-remote.test.ts', () => { storageInstanceB.internals.messageChannel ); + await storageInstanceA.close(); await storageInstanceB.close(); + + // even after closing all and reopnening a new one, it must be the same instance. + const storageInstanceC = await getStorage(port).createStorageInstance({ + databaseInstanceToken: randomCouchString(10), + databaseName: randomCouchString(10), + collectionName: 'human', + devMode: true, + multiInstance: false, + options: {}, + schema: fillWithDefaultSettings(schemas.human) + }); + assert.strictEqual( + storageInstanceA.internals.messageChannel, + storageInstanceC.internals.messageChannel + ); + + await storageInstanceC.close(); await colServer.database.destroy(); }); it('mode: storage', async () => {