Skip to content

Commit c5af49f

Browse files
committed
worker: pass resource limits to child.
preparing for: nodejs/node#26628 (comment)
1 parent 3947a98 commit c5af49f

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

lib/process/parent.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ const {
5050
const {
5151
BTHREADS_WORKER_ID: WORKER_ID,
5252
BTHREADS_WORKER_DATA: WORKER_DATA,
53-
BTHREADS_WORKER_STDIN: WORKER_STDIN
53+
BTHREADS_WORKER_STDIN: WORKER_STDIN,
54+
BTHREADS_WORKER_LIMITS: WORKER_LIMITS
5455
} = process.env;
5556

5657
/**
@@ -63,6 +64,7 @@ class Parent extends MessagePortBase {
6364

6465
this._workerId = WORKER_ID >>> 0;
6566
this._workerData = encoding.parse(WORKER_DATA);
67+
this._workerLimits = encoding.parse(WORKER_LIMITS);
6668
this._parser = new Parser(this);
6769
this._ports = new Map();
6870
this._closed = false;

lib/process/worker.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ class Worker extends EventEmitter {
108108

109109
_init(file, options) {
110110
const bin = process.execPath || process.argv[0];
111-
const limits = options.resourceLimits;
112111
const args = [];
113112

114113
// Validate filename.
@@ -187,18 +186,29 @@ class Worker extends EventEmitter {
187186
}
188187

189188
// Enforce resource limits.
190-
if (limits) {
189+
const limits = new Uint32Array(3);
190+
191+
if (options.resourceLimits) {
191192
const argsLen = args.length;
192-
const maxOld = limits.maxOldSpaceSizeMb;
193-
const maxSemi = limits.maxSemiSpaceSizeMb;
193+
const { maxOldSpaceSizeMb,
194+
maxSemiSpaceSizeMb,
195+
codeRangeSizeMb } = options.resourceLimits;
196+
197+
if (typeof maxOldSpaceSizeMb === 'number')
198+
limits[0] = Math.max(maxOldSpaceSizeMb, 2);
199+
200+
if (typeof maxSemiSpaceSizeMb === 'number')
201+
limits[1] = maxSemiSpaceSizeMb;
202+
203+
if (typeof codeRangeSizeMb === 'number')
204+
limits[2] = codeRangeSizeMb;
194205

195-
if (typeof maxOld === 'number')
196-
args.push(`--max-old-space-size=${Math.max(maxOld, 2)}`);
206+
if (limits[0] > 0)
207+
args.push(`--max-old-space-size=${limits[0]}`);
197208

198-
if (typeof maxSemi === 'number')
199-
args.push(`--max-semi-space-size=${maxSemi}`);
209+
if (limits[1] > 0)
210+
args.push(`--max-semi-space-size=${limits[1]}`);
200211

201-
// Todo: figure out how to do codeRangeSizeMb.
202212
this._limits = args.length > argsLen;
203213
}
204214

@@ -226,7 +236,8 @@ class Worker extends EventEmitter {
226236
BTHREADS_WORKER_ID: this.threadId.toString(10),
227237
BTHREADS_WORKER_DATA: encoding.stringify(options.workerData),
228238
BTHREADS_WORKER_STDIN: options.stdin ? '1' : '0',
229-
BTHREADS_WORKER_EVAL: options.eval ? '1' : '0'
239+
BTHREADS_WORKER_EVAL: options.eval ? '1' : '0',
240+
BTHREADS_WORKER_LIMITS: encoding.stringify(limits)
230241
})
231242
};
232243

0 commit comments

Comments
 (0)