Added max_memory_usage parameter (#238)

* Added max_memory_usage parameter

* Added max_memory_usage description to readme
This commit is contained in:
Felix 2021-05-01 11:10:53 +02:00 committed by GitHub
parent f2973f0536
commit a49acc7dbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 7 deletions

View File

@ -1,13 +1,14 @@
const express = require('express');
const router = express.Router();
const config = require('../config');
const runtime = require('../runtime');
const {Job} = require("../job");
const package = require('../package')
const logger = require('logplease').create('api/v1');
router.post('/execute', async function(req, res){
const {language, version, files, stdin, args, run_timeout, compile_timeout} = req.body;
const {language, version, files, stdin, args, run_timeout, compile_timeout, max_memory_usage} = req.body;
if(!language || typeof language !== "string")
{
@ -46,6 +47,21 @@ router.post('/execute', async function(req, res){
}
}
if (max_memory_usage) {
if (typeof max_memory_usage !== "number" || max_memory_usage < 0) {
return res
.status(400)
.send({
message: "if specified, max_memory_usage must be a non-negative number"
})
} else if (max_memory_usage > config.max_memory_usage) {
return res
.status(400)
.send({
message: "max_memory_usage cannot exceed the configured limit of " + config.max_memory_usage
})
}
}
@ -68,7 +84,8 @@ router.post('/execute', async function(req, res){
timeouts: {
run: run_timeout || 3000,
compile: compile_timeout || 10000
}
},
max_memory_usage: max_memory_usage || config.max_memory_usage
});
await job.prime();

View File

@ -108,6 +108,12 @@ const options = [
default: 1000000, //1MB
validators: []
},
{
key: 'max_memory_usage',
desc: 'Max memory usage in bytes',
default: 256000000, //256MB
validators: []
},
{
key: 'repo_url',
desc: 'URL of repo index',

View File

@ -19,7 +19,7 @@ let gid = 0;
class Job {
constructor({ runtime, files, args, stdin, timeouts }) {
constructor({ runtime, files, args, stdin, timeouts, max_memory_usage }) {
this.uuid = uuidv4();
this.runtime = runtime;
this.files = files.map((file,i) => ({
@ -30,6 +30,7 @@ class Job {
this.args = args;
this.stdin = stdin;
this.timeouts = timeouts;
this.max_memory_usage = max_memory_usage;
this.uid = config.runner_uid_min + uid;
this.gid = config.runner_gid_min + gid;
@ -75,7 +76,8 @@ class Job {
'prlimit',
'--nproc=' + config.max_process_count,
'--nofile=' + config.max_open_files,
'--fsize=' + config.max_file_size
'--fsize=' + config.max_file_size,
'--as=' + this.max_memory_usage
];
const proc_call = [
@ -161,7 +163,8 @@ class Job {
compile = await this.safe_call(
path.join(this.runtime.pkgdir, 'compile'),
this.files.map(x => x.name),
this.timeouts.compile
this.timeouts.compile,
config.max_memory_usage
);
}
@ -170,7 +173,8 @@ class Job {
const run = await this.safe_call(
path.join(this.runtime.pkgdir, 'run'),
[this.files[0].name, ...this.args],
this.timeouts.run
this.timeouts.run,
this.max_memory_usage
);
this.state = job_states.EXECUTED;

View File

@ -210,6 +210,7 @@ This endpoint requests execution of some arbitrary code.
- `args` (*optional*) The arguments to pass to the program. Must be an array or left out. Defaults to `[]`.
- `compile_timeout` (*optional*) The maximum time allowed for the compile stage to finish before bailing out in milliseconds. Must be a number or left out. Defaults to `10000` (10 seconds).
- `run_timeout` (*optional*) The maximum time allowed for the run stage to finish before bailing out in milliseconds. Must be a number or left out. Defaults to `3000` (3 seconds).
- `max_memory_usage` (*optional*) The maximum amount of memory the run stage is allowed to use. Must be a number or left out. Defaults to `256000000` (256 MB)
```json
{
@ -228,7 +229,8 @@ This endpoint requests execution of some arbitrary code.
"3"
],
"compile_timeout": 10000,
"run_timeout": 3000
"run_timeout": 3000,
"max_memory_usage": 256000000
}
```
A typical response upon successful execution will contain 1 or 2 keys `run` and `compile`.