openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
63 lines (62 loc) • 2.63 kB
JavaScript
import { W as resolveGlobalSingleton } from "./path-guards-quCEQx1i.mjs";
import { c as registerNodeSqliteDisposeCallback } from "./kysely-sync-cache-state-D75yJYFp.mjs";
//#region src/infra/sqlite-busy-timeout.ts
const lockFailureReportingByDatabase = /* @__PURE__ */ new WeakMap();
const { busyTimeoutByDatabase, trackedDatabases } = resolveGlobalSingleton(Symbol.for("openclaw.sqliteBusyTimeout"), () => ({
busyTimeoutByDatabase: /* @__PURE__ */ new WeakMap(),
trackedDatabases: /* @__PURE__ */ new WeakSet()
}));
/** Record policy applied by the native constructor, after schema tracking is installed. */
function rememberSqliteBusyTimeout(e, t) {
if (!trackedDatabases.has(e)) {
trackedDatabases.add(e);
registerNodeSqliteDisposeCallback(e, () => busyTimeoutByDatabase.delete(e));
}
busyTimeoutByDatabase.set(e, t);
}
function normalizeSqliteNonNegativeInteger(e, t) {
if (!Number.isInteger(e) || e < 0) throw new Error(`${t} must be a non-negative integer`);
return e;
}
function readSqliteBusyTimeout(e) {
const t = busyTimeoutByDatabase.get(e);
if (t !== void 0) return t;
const n = e.prepare("PRAGMA busy_timeout").get();
const r = n?.busy_timeout ?? n?.timeout;
const i = typeof r === "bigint" ? Number(r) : Number(r ?? 0);
rememberSqliteBusyTimeout(e, i);
return i;
}
/** All policy changes on a managed connection go through this owner. */
function setSqliteBusyTimeout(e, t) {
const n = normalizeSqliteNonNegativeInteger(t, "busyTimeoutMs");
if (busyTimeoutByDatabase.get(e) === n) return;
e.exec(`PRAGMA busy_timeout = ${n}`);
rememberSqliteBusyTimeout(e, n);
}
function shouldReportSqliteLockFailure(e) {
return lockFailureReportingByDatabase.get(e) !== "suppress";
}
/** Run with a temporary busy policy; restore early when write admission finishes. */
function runWithSqliteBusyTimeout(e, t, n, r = {}) {
const i = normalizeSqliteNonNegativeInteger(t, "busyTimeoutMs");
const a = readSqliteBusyTimeout(e);
const o = lockFailureReportingByDatabase.get(e);
if (r.lockFailureReporting) lockFailureReportingByDatabase.set(e, r.lockFailureReporting);
setSqliteBusyTimeout(e, i);
let s = false;
const restore = () => {
if (s) return;
if (e.isOpen && a !== i) setSqliteBusyTimeout(e, a);
if (o) lockFailureReportingByDatabase.set(e, o);
else lockFailureReportingByDatabase.delete(e);
s = true;
};
try {
return n(restore);
} finally {
restore();
}
}
//#endregion
export { setSqliteBusyTimeout as a, runWithSqliteBusyTimeout as i, readSqliteBusyTimeout as n, shouldReportSqliteLockFailure as o, rememberSqliteBusyTimeout as r, normalizeSqliteNonNegativeInteger as t };