Skip to content

Commit

Permalink
feat: add throttle and delay to connect
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicGBauer committed Jan 22, 2025
1 parent e79ed9b commit d1cf8c9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
11 changes: 9 additions & 2 deletions packages/common/src/client/AbstractPowerSyncDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
protected abstract openDBAdapter(options: PowerSyncDatabaseOptionsWithSettings): DBAdapter;

protected abstract generateSyncStreamImplementation(
connector: PowerSyncBackendConnector
connector: PowerSyncBackendConnector,
options?: {
retryDelayMs?: number;
crudUploadThrottleMs?: number;
}
): StreamingSyncImplementation;

protected abstract generateBucketStorageAdapter(): BucketStorageAdapter;
Expand Down Expand Up @@ -388,7 +392,10 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
throw new Error('Cannot connect using a closed client');
}

this.syncStreamImplementation = this.generateSyncStreamImplementation(connector);
this.syncStreamImplementation = this.generateSyncStreamImplementation(connector, {
crudUploadThrottleMs: options?.crudUploadThrottleMs,
retryDelay: options?.retryDelayMs
});
this.syncStatusListenerDisposer = this.syncStreamImplementation.registerListener({
statusChanged: (status) => {
this.currentStatus = new SyncStatus({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ export interface PowerSyncConnectionOptions {
* These parameters are passed to the sync rules, and will be available under the`user_parameters` object.
*/
params?: Record<string, StreamingSyncRequestParameterType>;
/**
* Delay for retrying sync streaming operations
* from the PowerSync backend after an error occurs.
*/
retryDelayMs?: number;
/**
* Backend Connector CRUD operations are throttled
* to occur at most every `crudUploadThrottleMs`
* milliseconds.
*/
crudUploadThrottleMs?: number;
}

export interface StreamingSyncImplementation extends BaseObserver<StreamingSyncImplementationListener>, Disposable {
Expand All @@ -102,14 +113,15 @@ export interface StreamingSyncImplementation extends BaseObserver<StreamingSyncI
}

export const DEFAULT_CRUD_UPLOAD_THROTTLE_MS = 1000;
export const DEFAULT_RETRY_DELAY_MS = 5000;

export const DEFAULT_STREAMING_SYNC_OPTIONS = {
retryDelayMs: 5000,
retryDelayMs: DEFAULT_RETRY_DELAY_MS,
logger: Logger.get('PowerSyncStream'),
crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS
};

export const DEFAULT_STREAM_CONNECTION_OPTIONS: Required<PowerSyncConnectionOptions> = {
export const DEFAULT_STREAM_CONNECTION_OPTIONS: Required<Omit<PowerSyncConnectionOptions, 'retryDelayMs' | 'crudUploadThrottleMs'>> = {
connectionMethod: SyncStreamConnectionMethod.WEB_SOCKET,
params: {}
};
Expand Down Expand Up @@ -427,7 +439,7 @@ The next upload iteration will be delayed.`);
type: LockType.SYNC,
signal,
callback: async () => {
const resolvedOptions: Required<PowerSyncConnectionOptions> = {
const resolvedOptions: Required<Omit<PowerSyncConnectionOptions, 'retryDelayMs' | 'crudUploadThrottleMs'>> = {
...DEFAULT_STREAM_CONNECTION_OPTIONS,
...(options ?? {})
};
Expand Down
11 changes: 8 additions & 3 deletions packages/react-native/src/db/PowerSyncDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
}

protected generateSyncStreamImplementation(
connector: PowerSyncBackendConnector
connector: PowerSyncBackendConnector,
// This is used to pass in options on connection instead of only during db creation
options?: {
retryDelayMs: number;
crudUploadThrottleMs: number;
}
): AbstractStreamingSyncImplementation {
const remote = new ReactNativeRemote(connector);

Expand All @@ -53,8 +58,8 @@ export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
await this.waitForReady();
await connector.uploadData(this);
},
retryDelayMs: this.options.retryDelay,
crudUploadThrottleMs: this.options.crudUploadThrottleMs,
retryDelayMs: options?.retryDelayMs || this.options.retryDelay,
crudUploadThrottleMs: options?.crudUploadThrottleMs || this.options.crudUploadThrottleMs,
identifier: this.database.name
});
}
Expand Down

0 comments on commit d1cf8c9

Please sign in to comment.