Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/defer-subduction-connection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@inkandswitch/patchwork": patch
"@inkandswitch/patchwork-bootloader": patch
---

Patchwork loads persisted Subduction data before opening its sync connection, with a `syncServers.defer` option to opt out.
45 changes: 40 additions & 5 deletions core/bootloader/src/automerge-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {

import { DEFAULT_CLASSIC_SYNC_SERVER } from "./sync-config.js";
import { keyhiveStorageName, storagePrefix } from "./storage.js";
import { DeferredWebSocketEndpoint } from "./deferred-websocket-endpoint.js";
import {
HANDOFF_CHANNEL,
SYNCSTATE_CHANNEL,
Expand All @@ -56,6 +57,7 @@ import {
declare const __SYNC_SERVER__: {
url: string;
keyhive?: SyncServerSelection;
defer?: boolean;
};

const syncServer =
Expand Down Expand Up @@ -194,15 +196,34 @@ function pushSyncState(message: SyncStateDocMessage): void {
}

const subductionPortProvider = makePortProvider();
let resolveSubductionStorageLoad: (() => void) | undefined;
const subductionStorageLoaded = syncServer.defer
? new Promise<void>((resolve) => {
resolveSubductionStorageLoad = resolve;
})
: undefined;

// Memoized so a construction retry reuses the endpoint instead of leaking one
// per attempt.
let subductionEndpoints: WorkerWebSocketEndpoint[] | null = null;
function getSubductionEndpoints(): WorkerWebSocketEndpoint[] {
type SubductionEndpoint = {
readonly url: string;
connect(): ReturnType<WorkerWebSocketEndpoint["connect"]>;
shutdown?(): void;
};

let subductionEndpoints: SubductionEndpoint[] | null = null;
function getSubductionEndpoints(): SubductionEndpoint[] {
return (subductionEndpoints ??= [
new WorkerWebSocketEndpoint(syncServer.url, {
worker: subductionPortProvider.source,
}),
(subductionStorageLoaded
? new DeferredWebSocketEndpoint(
new WorkerWebSocketEndpoint(syncServer.url, {
worker: subductionPortProvider.source,
}),
subductionStorageLoaded
)
: new WorkerWebSocketEndpoint(syncServer.url, {
worker: subductionPortProvider.source,
})),
]);
}

Expand Down Expand Up @@ -237,6 +258,20 @@ async function setUpRepoHive(): Promise<RepoHive> {
? await buildKeyhiveRepo(syncServer.keyhive)
: await buildPlainRepo();

if (resolveSubductionStorageLoad) {
const release = resolveSubductionStorageLoad;
resolveSubductionStorageLoad = undefined;
void (async () => {
const subduction = await built.repo.subduction;
await Promise.all(
(await subduction.sedimentreeIds()).map((id) => subduction.getBlobs(id))
);
log("subduction storage loaded");
})()
.catch((error) => log("subduction storage load failed", error))
.finally(release);
}

(self as any).repo = built.repo;
if (built.hive) (self as any).hive = built.hive;
if (built.identity) (self as any).syncIdentity = built.identity;
Expand Down
30 changes: 30 additions & 0 deletions core/bootloader/src/deferred-websocket-endpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
type WebSocketEndpoint<Transport> = {
readonly url: string;
connect(): Promise<Transport>;
shutdown?(): void;
};

/**
* Defers opening a WebSocket until the supplied startup work has completed.
*/
export class DeferredWebSocketEndpoint<Transport>
implements WebSocketEndpoint<Transport>
{
constructor(
private endpoint: WebSocketEndpoint<Transport>,
private ready: Promise<void>
) {}

get url(): string {
return this.endpoint.url;
}

async connect(): Promise<Transport> {
await this.ready;
return this.endpoint.connect();
}

shutdown(): void {
this.endpoint.shutdown?.();
}
}
1 change: 1 addition & 0 deletions core/patchwork/src/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const log = debug("patchwork:setup:repo");
declare const __SYNC_SERVER__: {
url: string;
keyhive?: SyncServerSelection;
defer?: boolean;
};
const syncServer =
typeof __SYNC_SERVER__ !== "undefined"
Expand Down
2 changes: 2 additions & 0 deletions core/patchwork/src/site-kit/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export type PatchworkKeyhiveSyncServer =
export type PatchworkSyncServersOptions = {
/** wss:// URL for the legacy automerge-repo sync-server channel (connected on demand via connectClassicSync). Default: wss://sync3.automerge.org. Pass false to skip its preconnect hint. */
classic?: string | false;
/** Wait for persisted Subduction data before opening the primary sync connection. Defaults to true. */
defer?: boolean;
} & PatchworkPrimarySyncServerOptions;

export const DEFAULT_TITLE = "Patchwork";
Expand Down
6 changes: 5 additions & 1 deletion core/patchwork/src/site-kit/sync-servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,27 @@ export const DEFAULT_SYNC_SERVERS = {
export function resolvePrimarySyncServer(options: PatchworkSiteOptions): {
url: string;
keyhive?: SyncServerSelection;
defer?: boolean;
} {
const servers = options.syncServers || undefined;
if (servers?.keyhive) {
if (typeof servers.keyhive === "string") {
return {
keyhive: servers.keyhive,
url: DEFAULT_SYNC_SERVERS[servers.keyhive],
defer: servers.defer ?? true,
};
}
const { url, ...identity } = servers.keyhive;
return {
keyhive: identity,
url,
defer: servers.defer ?? true,
};
}
return {
url: servers?.subduction ?? DEFAULT_SYNC_SERVERS.subduction,
defer: servers?.defer ?? true,
};
}

Expand All @@ -47,7 +51,7 @@ export function resolveSyncServers(options: PatchworkSiteOptions): string[] {
if (options.syncServers === false) return [];
const primary = resolvePrimarySyncServer(options);
const classic = options.syncServers?.classic ?? DEFAULT_SYNC_SERVERS.classic;
const origins = [primary.url];
const origins = primary.defer ? [] : [primary.url];
if (classic) origins.push(classic);
return origins.map(wsToHttpOrigin);
}
Expand Down
Loading