SLow down requests for development
This commit is contained in:
parent
6da107ff3a
commit
a2cbcf0519
4 changed files with 99 additions and 0 deletions
|
|
@ -0,0 +1,71 @@
|
||||||
|
import { helpers, Logger } from "sync-client";
|
||||||
|
|
||||||
|
export function flakyWebSocketFactory(
|
||||||
|
jitterScaleInSeconds: number,
|
||||||
|
logger: Logger
|
||||||
|
): typeof WebSocket {
|
||||||
|
// eslint-disable-next-line
|
||||||
|
return class FlakyWebSocket extends WebSocket {
|
||||||
|
private static readonly RECEIVE_KEY = "websocket-receive";
|
||||||
|
private static readonly SEND_KEY = "websocket-send";
|
||||||
|
|
||||||
|
private readonly locks = new helpers.Locks(logger);
|
||||||
|
|
||||||
|
public set onopen(callback: (event: Event) => void) {
|
||||||
|
super.onopen = async (event: Event): Promise<void> => {
|
||||||
|
if (jitterScaleInSeconds > 0) {
|
||||||
|
await sleep(Math.random() * jitterScaleInSeconds * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(event);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public set onmessage(callback: (event: MessageEvent) => void) {
|
||||||
|
super.onmessage = async (event: MessageEvent): Promise<void> => {
|
||||||
|
await this.locks.waitForLock(FlakyWebSocket.RECEIVE_KEY);
|
||||||
|
|
||||||
|
if (jitterScaleInSeconds > 0) {
|
||||||
|
await sleep(Math.random() * jitterScaleInSeconds * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(event);
|
||||||
|
|
||||||
|
this.locks.unlock(FlakyWebSocket.RECEIVE_KEY);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public set onclose(callback: (event: CloseEvent) => void) {
|
||||||
|
super.onclose = async (event: CloseEvent): Promise<void> => {
|
||||||
|
if (jitterScaleInSeconds > 0) {
|
||||||
|
await sleep(Math.random() * jitterScaleInSeconds * 1000);
|
||||||
|
}
|
||||||
|
callback(event);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public set onerror(callback: (event: Event) => void) {
|
||||||
|
super.onerror = async (event: Event): Promise<void> => {
|
||||||
|
if (jitterScaleInSeconds > 0) {
|
||||||
|
await sleep(Math.random() * jitterScaleInSeconds * 1000);
|
||||||
|
}
|
||||||
|
callback(event);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public async send(
|
||||||
|
data: string | ArrayBufferLike | Blob | ArrayBufferView
|
||||||
|
): Promise<void> {
|
||||||
|
// maintain message order
|
||||||
|
await this.locks.waitForLock(FlakyWebSocket.SEND_KEY);
|
||||||
|
|
||||||
|
if (jitterScaleInSeconds > 0) {
|
||||||
|
await sleep(Math.random() * jitterScaleInSeconds * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
super.send(data);
|
||||||
|
|
||||||
|
this.locks.unlock(FlakyWebSocket.SEND_KEY);
|
||||||
|
}
|
||||||
|
} as unknown as typeof WebSocket;
|
||||||
|
}
|
||||||
14
frontend/obsidian-plugin/src/debugging/slow-fetch-factory.ts
Normal file
14
frontend/obsidian-plugin/src/debugging/slow-fetch-factory.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
export const slowFetchFactory =
|
||||||
|
(jitterScaleInSeconds: number) =>
|
||||||
|
async (
|
||||||
|
input: string | URL | globalThis.Request,
|
||||||
|
init?: RequestInit
|
||||||
|
): Promise<Response> => {
|
||||||
|
if (jitterScaleInSeconds > 0) {
|
||||||
|
await sleep(Math.random() * jitterScaleInSeconds * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(input, init);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
};
|
||||||
3
frontend/obsidian-plugin/src/utils/sleep.ts
Normal file
3
frontend/obsidian-plugin/src/utils/sleep.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
export async function sleep(ms: number): Promise<void> {
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
@ -22,6 +22,8 @@ import {
|
||||||
setCursors
|
setCursors
|
||||||
} from "./views/cursors/remote-cursors-plugin";
|
} from "./views/cursors/remote-cursors-plugin";
|
||||||
import { LocalCursorUpdateListener } from "./views/cursors/local-cursor-update-listener";
|
import { LocalCursorUpdateListener } from "./views/cursors/local-cursor-update-listener";
|
||||||
|
import { slowFetchFactory } from "./debugging/slow-fetch-factory";
|
||||||
|
import { flakyWebSocketFactory } from "./debugging/flaky-websocket-factory";
|
||||||
|
|
||||||
const MIN_WAIT_BETWEEN_UPDATES_IN_MS = 250;
|
const MIN_WAIT_BETWEEN_UPDATES_IN_MS = 250;
|
||||||
export default class VaultLinkPlugin extends Plugin {
|
export default class VaultLinkPlugin extends Plugin {
|
||||||
|
|
@ -41,6 +43,15 @@ export default class VaultLinkPlugin extends Plugin {
|
||||||
".trash/**"
|
".trash/**"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const isDebugBuild = process.env.NODE_ENV === "development";
|
||||||
|
|
||||||
|
const debugOptions = isDebugBuild
|
||||||
|
? {
|
||||||
|
fetch: slowFetchFactory(1),
|
||||||
|
webSocket: flakyWebSocketFactory(1, new Logger())
|
||||||
|
}
|
||||||
|
: {};
|
||||||
|
|
||||||
this.client = await SyncClient.create({
|
this.client = await SyncClient.create({
|
||||||
fs: new ObsidianFileSystemOperations(
|
fs: new ObsidianFileSystemOperations(
|
||||||
this.app.vault,
|
this.app.vault,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue