Make timeout per request more specific

This commit is contained in:
Adria Navarro 2024-02-08 16:23:27 +01:00
parent 1e101744de
commit 5bc9eb884a
4 changed files with 11 additions and 13 deletions

View File

@ -2072,7 +2072,7 @@ describe.each([
await config.withEnv( await config.withEnv(
{ {
JS_PER_INVOCATION_TIMEOUT_MS: 20, JS_PER_INVOCATION_TIMEOUT_MS: 20,
JS_PER_REQUEST_TIME_LIMIT_MS: 40, JS_PER_REQUEST_TIMEOUT_MS: 40,
}, },
async () => { async () => {
const js = Buffer.from( const js = Buffer.from(

View File

@ -73,8 +73,8 @@ const environment = {
FORKED_PROCESS_NAME: process.env.FORKED_PROCESS_NAME || "main", FORKED_PROCESS_NAME: process.env.FORKED_PROCESS_NAME || "main",
JS_PER_INVOCATION_TIMEOUT_MS: JS_PER_INVOCATION_TIMEOUT_MS:
parseIntSafe(process.env.JS_PER_INVOCATION_TIMEOUT_MS) || 1000, parseIntSafe(process.env.JS_PER_INVOCATION_TIMEOUT_MS) || 1000,
JS_PER_REQUEST_TIME_LIMIT_MS: parseIntSafe( JS_PER_REQUEST_TIMEOUT_MS: parseIntSafe(
process.env.JS_PER_REQUEST_TIME_LIMIT_MS process.env.JS_PER_REQUEST_TIMEOUT_MS
), ),
// old // old
CLIENT_ID: process.env.CLIENT_ID, CLIENT_ID: process.env.CLIENT_ID,

View File

@ -19,7 +19,7 @@ export function init() {
vm = new IsolatedVM({ vm = new IsolatedVM({
memoryLimit: env.JS_RUNNER_MEMORY_LIMIT, memoryLimit: env.JS_RUNNER_MEMORY_LIMIT,
invocationTimeout: env.JS_PER_INVOCATION_TIMEOUT_MS, invocationTimeout: env.JS_PER_INVOCATION_TIMEOUT_MS,
perRequestLimit: env.JS_PER_REQUEST_TIME_LIMIT_MS, isolateAccumulatedTimeout: env.JS_PER_REQUEST_TIMEOUT_MS,
}) })
.withContext(ctxToPass) .withContext(ctxToPass)
.withHelpers() .withHelpers()

View File

@ -48,7 +48,7 @@ export class IsolatedVM implements VM {
private vm: ivm.Context private vm: ivm.Context
private jail: ivm.Reference private jail: ivm.Reference
private invocationTimeout: number private invocationTimeout: number
private perRequestLimit?: number private isolateAccumulatedTimeout?: number
private moduleHandler = new ModuleHandler() private moduleHandler = new ModuleHandler()
@ -57,11 +57,11 @@ export class IsolatedVM implements VM {
constructor({ constructor({
memoryLimit, memoryLimit,
invocationTimeout, invocationTimeout,
perRequestLimit, isolateAccumulatedTimeout,
}: { }: {
memoryLimit: number memoryLimit: number
invocationTimeout: number invocationTimeout: number
perRequestLimit?: number isolateAccumulatedTimeout?: number
}) { }) {
this.isolate = new ivm.Isolate({ memoryLimit }) this.isolate = new ivm.Isolate({ memoryLimit })
this.vm = this.isolate.createContextSync() this.vm = this.isolate.createContextSync()
@ -73,7 +73,7 @@ export class IsolatedVM implements VM {
}) })
this.invocationTimeout = invocationTimeout this.invocationTimeout = invocationTimeout
this.perRequestLimit = perRequestLimit this.isolateAccumulatedTimeout = isolateAccumulatedTimeout
} }
withHelpers() { withHelpers() {
@ -131,13 +131,11 @@ export class IsolatedVM implements VM {
} }
execute(code: string): string { execute(code: string): string {
const perRequestLimit = this.perRequestLimit if (this.isolateAccumulatedTimeout) {
if (perRequestLimit) {
const cpuMs = Number(this.isolate.cpuTime) / 1e6 const cpuMs = Number(this.isolate.cpuTime) / 1e6
if (cpuMs > perRequestLimit) { if (cpuMs > this.isolateAccumulatedTimeout) {
throw new ExecutionTimeoutError( throw new ExecutionTimeoutError(
`CPU time limit exceeded (${cpuMs}ms > ${perRequestLimit}ms)` `CPU time limit exceeded (${cpuMs}ms > ${this.isolateAccumulatedTimeout}ms)`
) )
} }
} }