-
Notifications
You must be signed in to change notification settings - Fork 30
Dynamically load connection modules for reduced memory usage #418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rkistner
merged 10 commits into
powersync-ja:main
from
najamansari:410-async-module-loading
Dec 4, 2025
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ee174ae
Dynamically load connection modules for reduced memory usage
najamansari aebb65c
Merge branch 'main' into 410-async-module-loading
najamansari a9b9e8d
Incorporating PR feedback (cleanups)
najamansari b209cbe
Refactoring module loader and its test cases
najamansari 3ab5ef2
Merge branch 'main' into 410-async-module-loading
najamansari 24950bb
Removing MongoDB exclusions
najamansari 2a7df8b
Some cleanup.
rkistner 9059015
Merge remote-tracking branch 'origin/main' into najamansari-410-async…
rkistner b4b1923
Move core module loading logic to service-core.
rkistner 1d7d913
Skip docker login on PRs from forks.
rkistner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@powersync/service-image': minor | ||
| --- | ||
|
|
||
| Dynamically load connection modules for reduced memory usage |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { container, logger } from '@powersync/lib-services-framework'; | ||
| import * as core from '@powersync/service-core'; | ||
|
|
||
| interface DynamicModuleMap { | ||
| [key: string]: () => Promise<any>; | ||
| } | ||
|
|
||
| const ModuleMap: DynamicModuleMap = { | ||
| mysql: () => import('@powersync/service-module-mysql').then((module) => module.MySQLModule), | ||
| postgresql: () => import('@powersync/service-module-postgres').then((module) => module.PostgresModule), | ||
| 'postgresql-storage': () => | ||
| import('@powersync/service-module-postgres-storage').then((module) => module.PostgresStorageModule) | ||
| }; | ||
|
|
||
| /** | ||
| * Utility function to dynamically load and instantiate modules. | ||
| * This function can optionally be moved to its own file (e.g., module-loader.ts) | ||
rkistner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * for better separation of concerns if the file gets much larger. | ||
| */ | ||
| export async function loadModules(config: core.ResolvedPowerSyncConfig) { | ||
| // 1. Determine required connections: Unique types from connections + storage type | ||
| const requiredConnections = [ | ||
| ...new Set(config.connections?.map((connection) => connection.type) || []), | ||
| `${config.storage.type}-storage` // Using template literal is clear | ||
| ]; | ||
|
|
||
| // 2. Map connection types to their module loading promises | ||
| const modulePromises = requiredConnections.map(async (connectionType) => { | ||
| // Exclude 'mongo' connections explicitly early | ||
| if (connectionType.startsWith('mongo')) { | ||
| return null; // Return null for filtering later | ||
| } | ||
rkistner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const modulePromise = ModuleMap[connectionType]; | ||
|
|
||
| // Check if a module is defined for the connection type | ||
| if (!modulePromise) { | ||
| logger.warn(`No module defined in ModuleMap for connection type: ${connectionType}`); | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| // Dynamically import and instantiate the class | ||
| const ModuleClass = await modulePromise(); | ||
| return new ModuleClass(); | ||
| } catch (error) { | ||
| // Log an error if the dynamic import fails (e.g., module not installed) | ||
| logger.error(`Failed to load module for ${connectionType}:`, error); | ||
| return null; | ||
| } | ||
rkistner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| // 3. Resolve all promises and filter out nulls/undefineds | ||
| const moduleInstances = await Promise.all(modulePromises); | ||
| return moduleInstances.filter((instance) => instance !== null); // Filter out nulls from excluded or failed imports | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { describe, test, expect, it } from 'vitest'; | ||
|
|
||
| import { logger } from '@powersync/lib-services-framework'; | ||
|
|
||
| import { MySQLModule } from '@powersync/service-module-mysql'; | ||
| import { PostgresModule } from '@powersync/service-module-postgres'; | ||
| import { PostgresStorageModule } from '@powersync/service-module-postgres-storage'; | ||
| import { loadModules } from '../../../src/util/module-loader.js'; | ||
|
|
||
| interface MockConfig { | ||
| connections?: MockConnection[]; | ||
| storage: { type: string }; | ||
| } | ||
|
|
||
| describe('module loader', () => { | ||
| it('should load all modules defined in connections and storage', async () => { | ||
| const config: MockConfig = { | ||
| connections: [{ type: 'mysql' }, { type: 'postgresql' }], | ||
| storage: { type: 'postgresql' } // This should result in 'postgresql-storage' | ||
| }; | ||
|
|
||
| const modules = await loadModules(config); | ||
|
|
||
| expect(modules.length).toBe(3); | ||
| expect(modules[0]).toBeInstanceOf(MySQLModule); | ||
| expect(modules[1]).toBeInstanceOf(PostgresModule); | ||
| expect(modules[2]).toBeInstanceOf(PostgresStorageModule); | ||
| }); | ||
|
|
||
| it('should handle duplicate connection types (e.g., mysql used twice)', async () => { | ||
| const config: MockConfig = { | ||
| connections: [{ type: 'mysql' }, { type: 'postgresql' }, { type: 'mysql' }], // mysql duplicated | ||
| storage: { type: 'postgresql' } | ||
| }; | ||
|
|
||
| const modules = await loadModules(config); | ||
|
|
||
| // Expect 3 modules: mysql, postgresql, postgresql-storage | ||
| expect(modules.length).toBe(3); | ||
| expect(modules.filter((m) => m instanceof MySQLModule).length).toBe(1); | ||
| expect(modules.filter((m) => m instanceof PostgresModule).length).toBe(1); | ||
| expect(modules.filter((m) => m instanceof PostgresStorageModule).length).toBe(1); | ||
| }); | ||
|
|
||
| it('should exclude connections starting with "mongo"', async () => { | ||
| const config: MockConfig = { | ||
| connections: [{ type: 'mysql' }, { type: 'mongodb' }], // mongodb should be ignored | ||
| storage: { type: 'postgresql' } | ||
| }; | ||
|
|
||
| const modules = await loadModules(config); | ||
|
|
||
| // Expect 2 modules: mysql and postgresql-storage | ||
| expect(modules.length).toBe(2); | ||
| expect(modules[0]).toBeInstanceOf(MySQLModule); | ||
| expect(modules[1]).toBeInstanceOf(PostgresStorageModule); | ||
| expect(modules.filter((m) => m instanceof PostgresModule).length).toBe(0); | ||
| }); | ||
|
|
||
| it('should filter out modules not found in ModuleMap', async () => { | ||
| const config: MockConfig = { | ||
| connections: [{ type: 'mysql' }, { type: 'redis' }], // unknown-db is missing | ||
| storage: { type: 'postgresql' } | ||
| }; | ||
|
|
||
| const modules = await loadModules(config); | ||
|
|
||
| // Expect 2 modules: mysql and postgresql-storage | ||
| expect(modules.length).toBe(2); | ||
| expect(modules.every((m) => m instanceof MySQLModule || m instanceof PostgresStorageModule)).toBe(true); | ||
| }); | ||
|
|
||
| it('should filter out modules that fail to import and log an error', async () => { | ||
| const config: MockConfig = { | ||
| connections: [{ type: 'mysql' }, { type: 'failing-module' }], // failing-module rejects promise | ||
| storage: { type: 'postgresql' } | ||
| }; | ||
|
|
||
| const modules = await loadModules(config); | ||
|
|
||
| // Expect 2 modules: mysql and postgresql-storage | ||
| expect(modules.length).toBe(2); | ||
| expect(modules.filter((m) => m instanceof MySQLModule).length).toBe(1); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.