Skip to content

🎥 Make videos programmatically with React

`@remotion/renderer`: Use `nproc` to only get the available cores to avoid too much concurrency in Kubernetes

Jonny Burger9e4c48f87d91
diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml
index e840b3ecec..409b2c61bf 100644
--- a/.github/workflows/push.yml
+++ b/.github/workflows/push.yml
@@ -139,7 +139,7 @@ jobs:
- os: macos-latest
node_version: 16
- os: ubuntu-latest
- node_version: 20
+ node_version: 20.5
steps:
- name: Uninstall Chrome on Windows because it does not include proprietary codecs
run: |
diff --git a/packages/renderer/src/get-concurrency.ts b/packages/renderer/src/get-concurrency.ts
index d9f1cb6969..73fa8f6e10 100644
--- a/packages/renderer/src/get-concurrency.ts
+++ b/packages/renderer/src/get-concurrency.ts
@@ -1,26 +1,50 @@
+import {execSync} from 'node:child_process';
import os from 'node:os';
+// Kubernetes uses the following command to spawn Docker containers:
+// docker run --cpuset-cpus="0,1" to assign only 2 CPUs.
+// However, Node.js returns the core count of the host system (up to 96!)
+// We also get it from nproc and use the minimum of the two.
+const getConcurrencyFromNProc = (): number | null => {
+ try {
+ return parseInt(execSync('nproc').toString().trim(), 10);
+ } catch (error) {
+ return null;
+ }
+};
+
+const getMaxCpus = () => {
+ const node = os.cpus().length;
+ const nproc = getConcurrencyFromNProc();
+ if (nproc === null) {
+ return node;
+ }
+
+ return Math.min(nproc, node);
+};
+
export const getActualConcurrency = (
userPreference: number | string | null,
) => {
+ const maxCpus = getMaxCpus();
+
if (userPreference === null) {
- return Math.round(Math.min(8, Math.max(1, os.cpus().length / 2)));
+ return Math.round(Math.min(8, Math.max(1, maxCpus / 2)));
}
- const max = os.cpus().length;
const min = 1;
let rounded;
if (typeof userPreference === 'string') {
const percentage = parseInt(userPreference.slice(0, -1), 10);
- rounded = Math.floor((percentage / 100) * max);
+ rounded = Math.floor((percentage / 100) * maxCpus);
} else {
rounded = Math.floor(userPreference);
}
- if (rounded > max) {
+ if (rounded > maxCpus) {
throw new Error(
- `Maximum for --concurrency is ${max} (number of cores on this system)`,
+ `Maximum for --concurrency is ${maxCpus} (number of cores on this system)`,
);
}