Move cpulimits responsability
This commit is contained in:
parent
3b8b60aa03
commit
0d0171fa08
|
@ -5,13 +5,6 @@ import tracer from "dd-trace"
|
||||||
import { IsolatedVM } from "./vm"
|
import { IsolatedVM } from "./vm"
|
||||||
import { context } from "@budibase/backend-core"
|
import { context } from "@budibase/backend-core"
|
||||||
|
|
||||||
class ExecutionTimeoutError extends Error {
|
|
||||||
constructor(message: string) {
|
|
||||||
super(message)
|
|
||||||
this.name = "ExecutionTimeoutError"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function init() {
|
export function init() {
|
||||||
setJSRunner((js: string, ctx: Record<string, any>) => {
|
setJSRunner((js: string, ctx: Record<string, any>) => {
|
||||||
return tracer.trace("runJS", {}, span => {
|
return tracer.trace("runJS", {}, span => {
|
||||||
|
@ -28,16 +21,6 @@ export function init() {
|
||||||
bbCtx.vm = vm
|
bbCtx.vm = vm
|
||||||
}
|
}
|
||||||
|
|
||||||
const perRequestLimit = env.JS_PER_REQUEST_TIME_LIMIT_MS
|
|
||||||
if (perRequestLimit) {
|
|
||||||
const cpuMs = Number(vm.cpuTime) / 1e6
|
|
||||||
if (cpuMs > perRequestLimit) {
|
|
||||||
throw new ExecutionTimeoutError(
|
|
||||||
`CPU time limit exceeded (${cpuMs}ms > ${perRequestLimit}ms)`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = vm.execute(js)
|
const result = vm.execute(js)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -7,11 +7,19 @@ import querystring from "querystring"
|
||||||
import { BundleType, loadBundle } from "../bundles"
|
import { BundleType, loadBundle } from "../bundles"
|
||||||
import { VM } from "@budibase/types"
|
import { VM } from "@budibase/types"
|
||||||
|
|
||||||
|
class ExecutionTimeoutError extends Error {
|
||||||
|
constructor(message: string) {
|
||||||
|
super(message)
|
||||||
|
this.name = "ExecutionTimeoutError"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class IsolatedVM implements VM {
|
export class IsolatedVM implements VM {
|
||||||
#isolate: ivm.Isolate
|
#isolate: ivm.Isolate
|
||||||
#vm: ivm.Context
|
#vm: ivm.Context
|
||||||
#jail: ivm.Reference
|
#jail: ivm.Reference
|
||||||
#timeout: number
|
#timeout: number
|
||||||
|
#perRequestLimit?: number
|
||||||
|
|
||||||
#modules: Record<
|
#modules: Record<
|
||||||
string,
|
string,
|
||||||
|
@ -26,9 +34,11 @@ export class IsolatedVM implements VM {
|
||||||
constructor({
|
constructor({
|
||||||
memoryLimit,
|
memoryLimit,
|
||||||
timeout,
|
timeout,
|
||||||
|
perRequestLimit,
|
||||||
}: {
|
}: {
|
||||||
memoryLimit: number
|
memoryLimit: number
|
||||||
timeout: number
|
timeout: number
|
||||||
|
perRequestLimit?: number
|
||||||
}) {
|
}) {
|
||||||
this.#isolate = new ivm.Isolate({ memoryLimit })
|
this.#isolate = new ivm.Isolate({ memoryLimit })
|
||||||
this.#vm = this.#isolate.createContextSync()
|
this.#vm = this.#isolate.createContextSync()
|
||||||
|
@ -40,10 +50,7 @@ export class IsolatedVM implements VM {
|
||||||
})
|
})
|
||||||
|
|
||||||
this.#timeout = timeout
|
this.#timeout = timeout
|
||||||
}
|
this.#perRequestLimit = perRequestLimit
|
||||||
|
|
||||||
get cpuTime() {
|
|
||||||
return this.#isolate.cpuTime
|
|
||||||
}
|
}
|
||||||
|
|
||||||
withHelpers() {
|
withHelpers() {
|
||||||
|
@ -115,6 +122,17 @@ export class IsolatedVM implements VM {
|
||||||
}
|
}
|
||||||
|
|
||||||
execute(code: string): string {
|
execute(code: string): string {
|
||||||
|
const perRequestLimit = this.#perRequestLimit
|
||||||
|
|
||||||
|
if (perRequestLimit) {
|
||||||
|
const cpuMs = Number(this.#isolate.cpuTime) / 1e6
|
||||||
|
if (cpuMs > perRequestLimit) {
|
||||||
|
throw new ExecutionTimeoutError(
|
||||||
|
`CPU time limit exceeded (${cpuMs}ms > ${perRequestLimit}ms)`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
code = [
|
code = [
|
||||||
...Object.values(this.#modules).map(m => m.headCode),
|
...Object.values(this.#modules).map(m => m.headCode),
|
||||||
`results.out=${code};`,
|
`results.out=${code};`,
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
export interface VM {
|
export interface VM {
|
||||||
cpuTime: bigint
|
|
||||||
execute(code: string): string
|
execute(code: string): string
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue