mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-04 22:01:15 +00:00
perf(memory): lazy-load matrix bootstrap and probe runtimes
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
import { getMatrixRuntime } from "../runtime.js";
|
||||
import type { CoreConfig } from "../types.js";
|
||||
import { getActiveMatrixClient } from "./active-client.js";
|
||||
import { acquireSharedMatrixClient, isBunRuntime, resolveMatrixAuthContext } from "./client.js";
|
||||
import { releaseSharedClientInstance } from "./client/shared.js";
|
||||
import { isBunRuntime } from "./client/runtime.js";
|
||||
import type { MatrixClient } from "./sdk.js";
|
||||
|
||||
type ResolvedRuntimeMatrixClient = {
|
||||
@@ -19,6 +18,28 @@ type MatrixResolvedClientHook = (
|
||||
context: { preparedByDefault: boolean },
|
||||
) => Promise<void> | void;
|
||||
|
||||
type MatrixSharedClientRuntimeDeps = Pick<
|
||||
typeof import("./client.js"),
|
||||
"acquireSharedMatrixClient"
|
||||
> &
|
||||
Pick<typeof import("./client/shared.js"), "releaseSharedClientInstance"> &
|
||||
Pick<typeof import("./client/config.js"), "resolveMatrixAuthContext">;
|
||||
|
||||
let matrixSharedClientRuntimeDepsPromise: Promise<MatrixSharedClientRuntimeDeps> | undefined;
|
||||
|
||||
async function loadMatrixSharedClientRuntimeDeps(): Promise<MatrixSharedClientRuntimeDeps> {
|
||||
matrixSharedClientRuntimeDepsPromise ??= Promise.all([
|
||||
import("./client.js"),
|
||||
import("./client/shared.js"),
|
||||
import("./client/config.js"),
|
||||
]).then(([clientModule, sharedModule, configModule]) => ({
|
||||
acquireSharedMatrixClient: clientModule.acquireSharedMatrixClient,
|
||||
releaseSharedClientInstance: sharedModule.releaseSharedClientInstance,
|
||||
resolveMatrixAuthContext: configModule.resolveMatrixAuthContext,
|
||||
}));
|
||||
return await matrixSharedClientRuntimeDepsPromise;
|
||||
}
|
||||
|
||||
async function ensureResolvedClientReadiness(params: {
|
||||
client: MatrixClient;
|
||||
readiness?: MatrixRuntimeClientReadiness;
|
||||
@@ -53,6 +74,8 @@ async function resolveRuntimeMatrixClient(opts: {
|
||||
}
|
||||
|
||||
const cfg = opts.cfg ?? (getMatrixRuntime().config.loadConfig() as CoreConfig);
|
||||
const { acquireSharedMatrixClient, releaseSharedClientInstance, resolveMatrixAuthContext } =
|
||||
await loadMatrixSharedClientRuntimeDeps();
|
||||
const authContext = resolveMatrixAuthContext({
|
||||
cfg,
|
||||
accountId: opts.accountId,
|
||||
@@ -62,7 +85,6 @@ async function resolveRuntimeMatrixClient(opts: {
|
||||
await opts.onResolved?.(active, { preparedByDefault: false });
|
||||
return { client: active, stopOnDone: false };
|
||||
}
|
||||
|
||||
const client = await acquireSharedMatrixClient({
|
||||
cfg,
|
||||
timeoutMs: opts.timeoutMs,
|
||||
|
||||
@@ -1,7 +1,18 @@
|
||||
import type { PinnedDispatcherPolicy } from "openclaw/plugin-sdk/infra-runtime";
|
||||
import type { SsrFPolicy } from "../runtime-api.js";
|
||||
import type { BaseProbeResult } from "../runtime-api.js";
|
||||
import { createMatrixClient, isBunRuntime } from "./client.js";
|
||||
import { isBunRuntime } from "./client/runtime.js";
|
||||
|
||||
type MatrixProbeRuntimeDeps = Pick<typeof import("./client.js"), "createMatrixClient">;
|
||||
|
||||
let matrixProbeRuntimeDepsPromise: Promise<MatrixProbeRuntimeDeps> | undefined;
|
||||
|
||||
async function loadMatrixProbeRuntimeDeps(): Promise<MatrixProbeRuntimeDeps> {
|
||||
matrixProbeRuntimeDepsPromise ??= import("./client.js").then((clientModule) => ({
|
||||
createMatrixClient: clientModule.createMatrixClient,
|
||||
}));
|
||||
return await matrixProbeRuntimeDepsPromise;
|
||||
}
|
||||
|
||||
export type MatrixProbe = BaseProbeResult & {
|
||||
status?: number | null;
|
||||
@@ -48,6 +59,7 @@ export async function probeMatrix(params: {
|
||||
};
|
||||
}
|
||||
try {
|
||||
const { createMatrixClient } = await loadMatrixProbeRuntimeDeps();
|
||||
const inputUserId = params.userId?.trim() || undefined;
|
||||
const client = await createMatrixClient({
|
||||
homeserver: params.homeserver,
|
||||
|
||||
Reference in New Issue
Block a user