Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit parallel file reads to prevent "EMFILE: too many open files" er…
…ror (#4170) * Added maxParallelFileReads. * Added some test for maxParallelFileReads setting. * Added tests for queue. * Updated tests * Added docs. * Fix test on Windows Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
- Loading branch information
1 parent
b3d5f7d
commit ce95197
Showing
32 changed files
with
207 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import * as fs from 'fs'; | ||
import fs from 'fs'; | ||
import { dirname } from './path'; | ||
|
||
export * from 'fs'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
export class Queue { | ||
private queue = new Array<{ | ||
reject: (reason?: any) => void; | ||
resolve: (value: any) => void; | ||
task: () => any; | ||
}>(); | ||
private workerCount = 0; | ||
|
||
constructor(public maxParallel = 1) {} | ||
|
||
run<T>(task: () => T | Promise<T>): Promise<T> { | ||
return new Promise((resolve, reject) => { | ||
this.queue.push({ reject, resolve, task }); | ||
this.work(); | ||
}); | ||
} | ||
|
||
private async work() { | ||
if (this.workerCount >= this.maxParallel) return; | ||
this.workerCount++; | ||
|
||
let entry; | ||
while ((entry = this.queue.shift())) { | ||
const { reject, resolve, task } = entry; | ||
|
||
try { | ||
const result = await task(); | ||
resolve(result); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
} | ||
|
||
this.workerCount--; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x1 = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x2 = 2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x3 = 3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x4 = 4; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x5 = 5; |
24 changes: 24 additions & 0 deletions
24
test/function/samples/max-parallel-file-reads-default/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
|
||
const fsReadFile = fs.readFile; | ||
let currentReads = 0; | ||
let maxReads = 0; | ||
|
||
module.exports = { | ||
description: 'maxParallelFileReads not set', | ||
before() { | ||
fs.readFile = (path, options, callback) => { | ||
currentReads++; | ||
maxReads = Math.max(maxReads, currentReads); | ||
fsReadFile(path, options, (err, data) => { | ||
currentReads--; | ||
callback(err, data); | ||
}); | ||
}; | ||
}, | ||
after() { | ||
fs.readFile = fsReadFile; | ||
assert.strictEqual(maxReads, 5, 'Wrong number of parallel file reads: ' + maxReads); | ||
} | ||
}; |
5 changes: 5 additions & 0 deletions
5
test/function/samples/max-parallel-file-reads-default/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './1'; | ||
export * from './2'; | ||
export * from './3'; | ||
export * from './4'; | ||
export * from './5'; |
31 changes: 31 additions & 0 deletions
31
test/function/samples/max-parallel-file-reads-error/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { loader } = require('../../../utils.js'); | ||
|
||
const fsReadFile = fs.readFile; | ||
|
||
module.exports = { | ||
description: 'maxParallelFileReads: fileRead error is forwarded', | ||
options: { | ||
input: 'main', | ||
plugins: loader({ | ||
main: `import {foo} from './dep';` | ||
}) | ||
}, | ||
before() { | ||
fs.readFile = (path, options, callback) => { | ||
if (path.endsWith('dep.js')) { | ||
return callback(new Error('broken')); | ||
} | ||
|
||
fsReadFile(path, options, callback); | ||
}; | ||
}, | ||
after() { | ||
fs.readFile = fsReadFile; | ||
}, | ||
error: { | ||
message: `Could not load ${path.join(__dirname, 'dep.js')} (imported by main): broken`, | ||
watchFiles: ['main', path.join(__dirname, 'dep.js')] | ||
} | ||
}; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x1 = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x2 = 2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x3 = 3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x4 = 4; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x5 = 5; |
27 changes: 27 additions & 0 deletions
27
test/function/samples/max-parallel-file-reads-infinity/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
|
||
const fsReadFile = fs.readFile; | ||
let currentReads = 0; | ||
let maxReads = 0; | ||
|
||
module.exports = { | ||
description: 'maxParallelFileReads set to infinity', | ||
options: { | ||
maxParallelFileReads: 0 | ||
}, | ||
before() { | ||
fs.readFile = (path, options, callback) => { | ||
currentReads++; | ||
maxReads = Math.max(maxReads, currentReads); | ||
fsReadFile(path, options, (err, data) => { | ||
currentReads--; | ||
callback(err, data); | ||
}); | ||
}; | ||
}, | ||
after() { | ||
fs.readFile = fsReadFile; | ||
assert.strictEqual(maxReads, 5, 'Wrong number of parallel file reads: ' + maxReads); | ||
} | ||
}; |
5 changes: 5 additions & 0 deletions
5
test/function/samples/max-parallel-file-reads-infinity/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './1'; | ||
export * from './2'; | ||
export * from './3'; | ||
export * from './4'; | ||
export * from './5'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x1 = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x2 = 2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x3 = 3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x4 = 4; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const x5 = 5; |
27 changes: 27 additions & 0 deletions
27
test/function/samples/max-parallel-file-reads-set/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
|
||
const fsReadFile = fs.readFile; | ||
let currentReads = 0; | ||
let maxReads = 0; | ||
|
||
module.exports = { | ||
description: 'maxParallelFileReads set to 3', | ||
options: { | ||
maxParallelFileReads: 3 | ||
}, | ||
before() { | ||
fs.readFile = (path, options, callback) => { | ||
currentReads++; | ||
maxReads = Math.max(maxReads, currentReads); | ||
fsReadFile(path, options, (err, data) => { | ||
currentReads--; | ||
callback(err, data); | ||
}); | ||
}; | ||
}, | ||
after() { | ||
fs.readFile = fsReadFile; | ||
assert.strictEqual(maxReads, 3, 'Wrong number of parallel file reads: ' + maxReads); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './1'; | ||
export * from './2'; | ||
export * from './3'; | ||
export * from './4'; | ||
export * from './5'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.