Add 2 more settings from consts
This commit is contained in:
parent
05a7a1701e
commit
e51fcf296f
5 changed files with 34 additions and 22 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
export const NETWORK_RETRY_INTERVAL_MS = 1000;
|
export const MERGABLE_FILE_TYPES = ["md", "txt"];
|
||||||
export const MINIMUM_SAVE_INTERVAL_MS = 1000;
|
|
||||||
|
export const TIMEOUT_FOR_MERGING_HISTORY_ENTRIES_IN_SECONDS = 60;
|
||||||
export const DIFF_CACHE_SIZE_MB = 2;
|
export const DIFF_CACHE_SIZE_MB = 2;
|
||||||
export const MAX_LOG_MESSAGE_COUNT = 100000;
|
export const MAX_LOG_MESSAGE_COUNT = 100000;
|
||||||
export const MAX_HISTORY_ENTRY_COUNT = 5000;
|
export const MAX_HISTORY_ENTRY_COUNT = 5000;
|
||||||
export const TIMEOUT_FOR_MERGING_HISTORY_ENTRIES_IN_SECONDS = 60;
|
|
||||||
export const MERGABLE_FILE_TYPES = ["md", "txt"];
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@ export interface SyncSettings {
|
||||||
webSocketRetryIntervalMs: number;
|
webSocketRetryIntervalMs: number;
|
||||||
diffCacheSizeMB: number;
|
diffCacheSizeMB: number;
|
||||||
enableTelemetry: boolean;
|
enableTelemetry: boolean;
|
||||||
|
networkRetryIntervalMs: number;
|
||||||
|
minimumSaveIntervalMs: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DEFAULT_SETTINGS: SyncSettings = {
|
export const DEFAULT_SETTINGS: SyncSettings = {
|
||||||
|
|
@ -23,7 +25,9 @@ export const DEFAULT_SETTINGS: SyncSettings = {
|
||||||
ignorePatterns: [],
|
ignorePatterns: [],
|
||||||
webSocketRetryIntervalMs: 3500,
|
webSocketRetryIntervalMs: 3500,
|
||||||
diffCacheSizeMB: 4,
|
diffCacheSizeMB: 4,
|
||||||
enableTelemetry: false
|
enableTelemetry: false,
|
||||||
|
networkRetryIntervalMs: 1000,
|
||||||
|
minimumSaveIntervalMs: 1000
|
||||||
};
|
};
|
||||||
|
|
||||||
export class Settings {
|
export class Settings {
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,6 @@ import type { FetchLatestDocumentsResponse } from "./types/FetchLatestDocumentsR
|
||||||
import type { PingResponse } from "./types/PingResponse";
|
import type { PingResponse } from "./types/PingResponse";
|
||||||
import type { DeleteDocumentVersion } from "./types/DeleteDocumentVersion";
|
import type { DeleteDocumentVersion } from "./types/DeleteDocumentVersion";
|
||||||
import type { UpdateTextDocumentVersion } from "./types/UpdateTextDocumentVersion";
|
import type { UpdateTextDocumentVersion } from "./types/UpdateTextDocumentVersion";
|
||||||
import { NETWORK_RETRY_INTERVAL_MS } from "../consts";
|
|
||||||
|
|
||||||
export class SyncService {
|
export class SyncService {
|
||||||
private readonly client: typeof globalThis.fetch;
|
private readonly client: typeof globalThis.fetch;
|
||||||
|
|
@ -371,10 +370,12 @@ export class SyncService {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const retryInterval =
|
||||||
|
this.settings.getSettings().networkRetryIntervalMs;
|
||||||
this.logger.error(
|
this.logger.error(
|
||||||
`Failed network call (${e}), retrying in ${NETWORK_RETRY_INTERVAL_MS}ms`
|
`Failed network call (${e}), retrying in ${retryInterval}ms`
|
||||||
);
|
);
|
||||||
await sleep(NETWORK_RETRY_INTERVAL_MS);
|
await sleep(retryInterval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ import type { MaybeOutdatedClientCursors } from "./types/maybe-outdated-client-c
|
||||||
import { FileChangeNotifier } from "./sync-operations/file-change-notifier";
|
import { FileChangeNotifier } from "./sync-operations/file-change-notifier";
|
||||||
import { FixedSizeDocumentCache } from "./utils/data-structures/fix-sized-cache";
|
import { FixedSizeDocumentCache } from "./utils/data-structures/fix-sized-cache";
|
||||||
import { setUpTelemetry } from "./utils/set-up-telemetry";
|
import { setUpTelemetry } from "./utils/set-up-telemetry";
|
||||||
import { DIFF_CACHE_SIZE_MB, MINIMUM_SAVE_INTERVAL_MS } from "./consts";
|
import { DIFF_CACHE_SIZE_MB } from "./consts";
|
||||||
|
|
||||||
export class SyncClient {
|
export class SyncClient {
|
||||||
private hasStartedOfflineSync = false;
|
private hasStartedOfflineSync = false;
|
||||||
|
|
@ -157,9 +157,20 @@ export class SyncClient {
|
||||||
database: undefined
|
database: undefined
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const settings = new Settings(
|
||||||
|
logger,
|
||||||
|
state.settings,
|
||||||
|
async (data): Promise<void> => {
|
||||||
|
state = { ...state, settings: data };
|
||||||
|
// we're not rate-limiting settings saves as (1) we need to initialise the settings to know the rate limit
|
||||||
|
// and (2) settings changes are infrequent enough that rate-limiting is not necessary
|
||||||
|
await persistence.save(state);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const rateLimitedSave = rateLimit(
|
const rateLimitedSave = rateLimit(
|
||||||
persistence.save,
|
persistence.save,
|
||||||
MINIMUM_SAVE_INTERVAL_MS
|
() => settings.getSettings().minimumSaveIntervalMs
|
||||||
);
|
);
|
||||||
|
|
||||||
const database = new Database(
|
const database = new Database(
|
||||||
|
|
@ -171,15 +182,6 @@ export class SyncClient {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const settings = new Settings(
|
|
||||||
logger,
|
|
||||||
state.settings,
|
|
||||||
async (data): Promise<void> => {
|
|
||||||
state = { ...state, settings: data };
|
|
||||||
await rateLimitedSave(state);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const fetchController = new FetchController(
|
const fetchController = new FetchController(
|
||||||
settings.getSettings().isSyncEnabled,
|
settings.getSettings().isSyncEnabled,
|
||||||
logger
|
logger
|
||||||
|
|
@ -201,6 +203,7 @@ export class SyncClient {
|
||||||
const fileOperations = new FileOperations(
|
const fileOperations = new FileOperations(
|
||||||
logger,
|
logger,
|
||||||
database,
|
database,
|
||||||
|
settings,
|
||||||
fs,
|
fs,
|
||||||
nativeLineEndings
|
nativeLineEndings
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,8 @@ import { sleep } from "./sleep";
|
||||||
*
|
*
|
||||||
* @template T - Type of the function to be rate limited
|
* @template T - Type of the function to be rate limited
|
||||||
* @param {T} fn - The asynchronous function to rate limit
|
* @param {T} fn - The asynchronous function to rate limit
|
||||||
* @param {number} minIntervalMs - The minimum interval in milliseconds between function calls
|
* @param {number | (() => number)} minIntervalMs - Minimum interval in milliseconds between calls,
|
||||||
|
* or a function that returns the minimum interval
|
||||||
* @returns {(...args: Parameters<T>) => ReturnType<T> | Promise<undefined>} A decorated function that respects the rate limit.
|
* @returns {(...args: Parameters<T>) => ReturnType<T> | Promise<undefined>} A decorated function that respects the rate limit.
|
||||||
* Returns the original function's return type when executed, or undefined if the call was superseded by a newer one.
|
* Returns the original function's return type when executed, or undefined if the call was superseded by a newer one.
|
||||||
*/
|
*/
|
||||||
|
|
@ -21,7 +22,7 @@ export function rateLimit<
|
||||||
) => Promise<R>
|
) => Promise<R>
|
||||||
>(
|
>(
|
||||||
fn: T,
|
fn: T,
|
||||||
minIntervalMs: number
|
minIntervalMs: number | (() => number)
|
||||||
): (...args: Parameters<T>) => Promise<R | undefined> {
|
): (...args: Parameters<T>) => Promise<R | undefined> {
|
||||||
let newArgs: Parameters<T> | undefined = undefined;
|
let newArgs: Parameters<T> | undefined = undefined;
|
||||||
let running: Promise<unknown> | undefined = undefined;
|
let running: Promise<unknown> | undefined = undefined;
|
||||||
|
|
@ -46,7 +47,11 @@ export function rateLimit<
|
||||||
|
|
||||||
const [promise, resolve] = createPromise();
|
const [promise, resolve] = createPromise();
|
||||||
running = promise;
|
running = promise;
|
||||||
sleep(minIntervalMs)
|
sleep(
|
||||||
|
typeof minIntervalMs === "function"
|
||||||
|
? minIntervalMs()
|
||||||
|
: minIntervalMs
|
||||||
|
)
|
||||||
.then(resolve)
|
.then(resolve)
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
// sleep cannot fail
|
// sleep cannot fail
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue