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(
{
JS_PER_INVOCATION_TIMEOUT_MS: 20,
JS_PER_REQUEST_TIME_LIMIT_MS: 40,
JS_PER_REQUEST_TIMEOUT_MS: 40,
},
async () => {
const js = Buffer.from(

View File

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

View File

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

View File

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