fix(onebot): allow a custom session release deadline
Operators can set SNOWLUMA_SESSION_RELEASE_DEADLINE_MS, in milliseconds. The wait stays at 10 seconds when that variable is absent or not a positive integer.
This commit is contained in:
2 files changed
+120
-6
No files matched your search
@@ -44,11 +44,41 @@ export interface DatabaseMigrationTask {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** A peer that never finishes a close handshake must not keep the next
|
/** A peer that never finishes a close handshake must not keep the next
|
||||||
* account session offline. Ten seconds covers a normal drain. */
|
* account session offline. Ten seconds covers a normal drain.
|
||||||
const SESSION_RELEASE_DEADLINE_MS = 10_000;
|
* SNOWLUMA_SESSION_RELEASE_DEADLINE_MS overrides the configured value. */
|
||||||
|
const DEFAULT_SESSION_RELEASE_DEADLINE_MS = 10_000;
|
||||||
|
const SESSION_RELEASE_DEADLINE_ENV = 'SNOWLUMA_SESSION_RELEASE_DEADLINE_MS';
|
||||||
|
const MAX_SESSION_RELEASE_DEADLINE_MS = 2_147_483_647;
|
||||||
|
|
||||||
|
/** Positive integer milliseconds, or `undefined` when the raw value is absent
|
||||||
|
* or not usable. Callers decide the fallback. */
|
||||||
|
export function parseSessionReleaseDeadlineMs(raw: string | undefined): number | undefined {
|
||||||
|
if (raw === undefined) return undefined;
|
||||||
|
const value = raw.trim();
|
||||||
|
if (!/^[1-9]\d*$/.test(value)) return undefined;
|
||||||
|
const parsed = Number(value);
|
||||||
|
if (!Number.isSafeInteger(parsed) || parsed > MAX_SESSION_RELEASE_DEADLINE_MS) return undefined;
|
||||||
|
return parsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Env wins over an explicit deadline. An unusable env value is ignored. */
|
||||||
|
export function resolveSessionReleaseDeadlineMs(configured?: number, envRaw?: string): number {
|
||||||
|
const fromEnv = parseSessionReleaseDeadlineMs(envRaw);
|
||||||
|
if (fromEnv !== undefined) return fromEnv;
|
||||||
|
if (
|
||||||
|
configured !== undefined
|
||||||
|
&& Number.isSafeInteger(configured)
|
||||||
|
&& configured > 0
|
||||||
|
&& configured <= MAX_SESSION_RELEASE_DEADLINE_MS
|
||||||
|
) {
|
||||||
|
return configured;
|
||||||
|
}
|
||||||
|
return DEFAULT_SESSION_RELEASE_DEADLINE_MS;
|
||||||
|
}
|
||||||
|
|
||||||
export interface OneBotManagerOptions {
|
export interface OneBotManagerOptions {
|
||||||
/** How long a retiring instance may block the replacement session. */
|
/** How long a retiring instance may block the replacement session.
|
||||||
|
* SNOWLUMA_SESSION_RELEASE_DEADLINE_MS overrides this when it is set. */
|
||||||
sessionReleaseDeadlineMs?: number;
|
sessionReleaseDeadlineMs?: number;
|
||||||
createDatabaseMigrationTask?: (uin: string) => DatabaseMigrationTask;
|
createDatabaseMigrationTask?: (uin: string) => DatabaseMigrationTask;
|
||||||
createInstance?: (
|
createInstance?: (
|
||||||
@@ -100,9 +130,18 @@ export class OneBotManager {
|
|||||||
private readonly sessionReleaseDeadlineMs: number;
|
private readonly sessionReleaseDeadlineMs: number;
|
||||||
|
|
||||||
constructor(options: OneBotManagerOptions = {}) {
|
constructor(options: OneBotManagerOptions = {}) {
|
||||||
this.sessionReleaseDeadlineMs = options.sessionReleaseDeadlineMs && options.sessionReleaseDeadlineMs > 0
|
const envRaw = process.env[SESSION_RELEASE_DEADLINE_ENV];
|
||||||
? options.sessionReleaseDeadlineMs
|
if (envRaw !== undefined && envRaw.trim() && parseSessionReleaseDeadlineMs(envRaw) === undefined) {
|
||||||
: SESSION_RELEASE_DEADLINE_MS;
|
log.warn(
|
||||||
|
'%s=%j is ignored; expected a positive integer number of milliseconds',
|
||||||
|
SESSION_RELEASE_DEADLINE_ENV,
|
||||||
|
envRaw,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
this.sessionReleaseDeadlineMs = resolveSessionReleaseDeadlineMs(
|
||||||
|
options.sessionReleaseDeadlineMs,
|
||||||
|
envRaw,
|
||||||
|
);
|
||||||
this.createDatabaseMigrationTask = options.createDatabaseMigrationTask
|
this.createDatabaseMigrationTask = options.createDatabaseMigrationTask
|
||||||
?? createMessageStoreMigrationTask;
|
?? createMessageStoreMigrationTask;
|
||||||
this.createInstance = options.createInstance
|
this.createInstance = options.createInstance
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import type { AdapterStatus } from '../src/network';
|
|||||||
import type { OneBotInstance } from '../src/instance';
|
import type { OneBotInstance } from '../src/instance';
|
||||||
import {
|
import {
|
||||||
OneBotManager,
|
OneBotManager,
|
||||||
|
parseSessionReleaseDeadlineMs,
|
||||||
|
resolveSessionReleaseDeadlineMs,
|
||||||
type DatabaseMigrationCallbacks,
|
type DatabaseMigrationCallbacks,
|
||||||
type DatabaseMigrationTask,
|
type DatabaseMigrationTask,
|
||||||
} from '../src/manager';
|
} from '../src/manager';
|
||||||
@@ -38,6 +40,26 @@ function fakeInstance(
|
|||||||
} as unknown as OneBotInstance;
|
} as unknown as OneBotInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
describe('session release deadline', () => {
|
||||||
|
it('keeps 10 seconds when nothing overrides it', () => {
|
||||||
|
expect(resolveSessionReleaseDeadlineMs(undefined, undefined)).toBe(10_000);
|
||||||
|
expect(resolveSessionReleaseDeadlineMs(undefined, ' ')).toBe(10_000);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('lets SNOWLUMA_SESSION_RELEASE_DEADLINE_MS override a configured deadline', () => {
|
||||||
|
expect(resolveSessionReleaseDeadlineMs(30, '2500')).toBe(2500);
|
||||||
|
expect(parseSessionReleaseDeadlineMs('0')).toBeUndefined();
|
||||||
|
expect(parseSessionReleaseDeadlineMs('-5')).toBeUndefined();
|
||||||
|
expect(parseSessionReleaseDeadlineMs('1.5')).toBeUndefined();
|
||||||
|
expect(parseSessionReleaseDeadlineMs('nope')).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('ignores an unusable environment value and keeps the configured deadline', () => {
|
||||||
|
expect(resolveSessionReleaseDeadlineMs(30, 'nope')).toBe(30);
|
||||||
|
expect(resolveSessionReleaseDeadlineMs(undefined, 'nope')).toBe(10_000);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('database migration estimates', () => {
|
describe('database migration estimates', () => {
|
||||||
it('waits for measured throughput and reaches zero on completion', () => {
|
it('waits for measured throughput and reaches zero on completion', () => {
|
||||||
expect(estimateRemainingSeconds(
|
expect(estimateRemainingSeconds(
|
||||||
@@ -571,6 +593,8 @@ describe('OneBotManager lifecycle failure accounting', () => {
|
|||||||
|
|
||||||
it('starts the replacement account when release never finishes', async () => {
|
it('starts the replacement account when release never finishes', async () => {
|
||||||
const originalCwd = process.cwd();
|
const originalCwd = process.cwd();
|
||||||
|
const savedDeadline = process.env.SNOWLUMA_SESSION_RELEASE_DEADLINE_MS;
|
||||||
|
delete process.env.SNOWLUMA_SESSION_RELEASE_DEADLINE_MS;
|
||||||
const root = mkdtempSync(path.join(tmpdir(), 'snowluma-manager-release-'));
|
const root = mkdtempSync(path.join(tmpdir(), 'snowluma-manager-release-'));
|
||||||
process.chdir(root);
|
process.chdir(root);
|
||||||
const forceRelease = vi.fn();
|
const forceRelease = vi.fn();
|
||||||
@@ -613,6 +637,57 @@ describe('OneBotManager lifecycle failure accounting', () => {
|
|||||||
expect(internals.pendingStarts.has('10001')).toBe(false);
|
expect(internals.pendingStarts.has('10001')).toBe(false);
|
||||||
expect(internals.retiringInstances.has(old)).toBe(false);
|
expect(internals.retiringInstances.has(old)).toBe(false);
|
||||||
} finally {
|
} finally {
|
||||||
|
if (savedDeadline === undefined) delete process.env.SNOWLUMA_SESSION_RELEASE_DEADLINE_MS;
|
||||||
|
else process.env.SNOWLUMA_SESSION_RELEASE_DEADLINE_MS = savedDeadline;
|
||||||
|
process.chdir(originalCwd);
|
||||||
|
rmSync(root, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('uses SNOWLUMA_SESSION_RELEASE_DEADLINE_MS instead of a longer configured deadline', async () => {
|
||||||
|
const originalCwd = process.cwd();
|
||||||
|
const savedDeadline = process.env.SNOWLUMA_SESSION_RELEASE_DEADLINE_MS;
|
||||||
|
process.env.SNOWLUMA_SESSION_RELEASE_DEADLINE_MS = '40';
|
||||||
|
const root = mkdtempSync(path.join(tmpdir(), 'snowluma-manager-release-env-'));
|
||||||
|
process.chdir(root);
|
||||||
|
const forceRelease = vi.fn();
|
||||||
|
const old = {
|
||||||
|
...fakeInstance('10001', () => new Promise(() => undefined)),
|
||||||
|
forceRelease,
|
||||||
|
} as unknown as OneBotInstance;
|
||||||
|
let created = false;
|
||||||
|
const replacement = {
|
||||||
|
...fakeInstance('10001', async () => undefined),
|
||||||
|
waitUntilNetworkReady: vi.fn(async () => ({ applied: true, statuses: [], errors: [] })),
|
||||||
|
startLoginHistorySync: vi.fn(),
|
||||||
|
startGroupRequestPolling: vi.fn(),
|
||||||
|
} as unknown as OneBotInstance;
|
||||||
|
const manager = new OneBotManager({
|
||||||
|
sessionReleaseDeadlineMs: 60_000,
|
||||||
|
createDatabaseMigrationTask: () => ({
|
||||||
|
beginMigration: vi.fn(),
|
||||||
|
cancel: vi.fn(),
|
||||||
|
start: (next) => { next.onReady(); },
|
||||||
|
}),
|
||||||
|
createInstance: () => {
|
||||||
|
created = true;
|
||||||
|
return replacement;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const internals = manager as unknown as {
|
||||||
|
retiringInstances: Set<OneBotInstance>;
|
||||||
|
onSessionStarted(uin: string, bridge: never): void;
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
internals.retiringInstances.add(old);
|
||||||
|
internals.onSessionStarted('10001', fakeBridge() as never);
|
||||||
|
await vi.waitFor(() => {
|
||||||
|
expect(created).toBe(true);
|
||||||
|
});
|
||||||
|
expect(forceRelease).toHaveBeenCalledOnce();
|
||||||
|
} finally {
|
||||||
|
if (savedDeadline === undefined) delete process.env.SNOWLUMA_SESSION_RELEASE_DEADLINE_MS;
|
||||||
|
else process.env.SNOWLUMA_SESSION_RELEASE_DEADLINE_MS = savedDeadline;
|
||||||
process.chdir(originalCwd);
|
process.chdir(originalCwd);
|
||||||
rmSync(root, { recursive: true, force: true });
|
rmSync(root, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user