Add 2 more settings from consts

This commit is contained in:
Andras Schmelczer 2025-11-23 14:59:56 +00:00
parent 3cdd2a4387
commit 83c15a77c3
5 changed files with 34 additions and 22 deletions

View file

@ -1,7 +1,6 @@
export const NETWORK_RETRY_INTERVAL_MS = 1000;
export const MINIMUM_SAVE_INTERVAL_MS = 1000;
export const MERGABLE_FILE_TYPES = ["md", "txt"];
export const TIMEOUT_FOR_MERGING_HISTORY_ENTRIES_IN_SECONDS = 60;
export const DIFF_CACHE_SIZE_MB = 2;
export const MAX_LOG_MESSAGE_COUNT = 100000;
export const MAX_HISTORY_ENTRY_COUNT = 5000;
export const TIMEOUT_FOR_MERGING_HISTORY_ENTRIES_IN_SECONDS = 60;
export const MERGABLE_FILE_TYPES = ["md", "txt"];

View file

@ -11,6 +11,8 @@ export interface SyncSettings {
webSocketRetryIntervalMs: number;
diffCacheSizeMB: number;
enableTelemetry: boolean;
networkRetryIntervalMs: number;
minimumSaveIntervalMs: number;
}
export const DEFAULT_SETTINGS: SyncSettings = {
@ -23,7 +25,9 @@ export const DEFAULT_SETTINGS: SyncSettings = {
ignorePatterns: [],
webSocketRetryIntervalMs: 3500,
diffCacheSizeMB: 4,
enableTelemetry: false
enableTelemetry: false,
networkRetryIntervalMs: 1000,
minimumSaveIntervalMs: 1000
};
export class Settings {

View file

@ -17,7 +17,6 @@ import type { FetchLatestDocumentsResponse } from "./types/FetchLatestDocumentsR
import type { PingResponse } from "./types/PingResponse";
import type { DeleteDocumentVersion } from "./types/DeleteDocumentVersion";
import type { UpdateTextDocumentVersion } from "./types/UpdateTextDocumentVersion";
import { NETWORK_RETRY_INTERVAL_MS } from "../consts";
export class SyncService {
private readonly client: typeof globalThis.fetch;
@ -371,10 +370,12 @@ export class SyncService {
throw e;
}
const retryInterval =
this.settings.getSettings().networkRetryIntervalMs;
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);
}
}
}

View file

@ -24,7 +24,7 @@ import type { MaybeOutdatedClientCursors } from "./types/maybe-outdated-client-c
import { FileChangeNotifier } from "./sync-operations/file-change-notifier";
import { FixedSizeDocumentCache } from "./utils/data-structures/fix-sized-cache";
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 {
private hasStartedOfflineSync = false;
@ -157,9 +157,20 @@ export class SyncClient {
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(
persistence.save,
MINIMUM_SAVE_INTERVAL_MS
() => settings.getSettings().minimumSaveIntervalMs
);
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(
settings.getSettings().isSyncEnabled,
logger
@ -201,6 +203,7 @@ export class SyncClient {
const fileOperations = new FileOperations(
logger,
database,
settings,
fs,
nativeLineEndings
);

View file

@ -10,7 +10,8 @@ import { sleep } from "./sleep";
*
* @template T - Type of the function to be rate limited
* @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 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>
>(
fn: T,
minIntervalMs: number
minIntervalMs: number | (() => number)
): (...args: Parameters<T>) => Promise<R | undefined> {
let newArgs: Parameters<T> | undefined = undefined;
let running: Promise<unknown> | undefined = undefined;
@ -46,7 +47,11 @@ export function rateLimit<
const [promise, resolve] = createPromise();
running = promise;
sleep(minIntervalMs)
sleep(
typeof minIntervalMs === "function"
? minIntervalMs()
: minIntervalMs
)
.then(resolve)
.catch(() => {
// sleep cannot fail