Skip to content

Commit e7eaa50

Browse files
authored
perf: limit the number of concurrently open file watchers on macOS (#249)
1 parent 33df387 commit e7eaa50

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/watchEventSource.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ const IS_OSX = require("os").platform() === "darwin";
1313
const IS_WIN = require("os").platform() === "win32";
1414
const SUPPORTS_RECURSIVE_WATCHING = IS_OSX || IS_WIN;
1515

16+
// Use 20 for OSX to make `FSWatcher.close` faster
17+
// https://github.com/nodejs/node/issues/29949
1618
const watcherLimit =
17-
+process.env.WATCHPACK_WATCHER_LIMIT || (IS_OSX ? 2000 : 10000);
19+
+process.env.WATCHPACK_WATCHER_LIMIT || (IS_OSX ? 20 : 10000);
1820

1921
const recursiveWatcherLogging = !!process.env
2022
.WATCHPACK_RECURSIVE_WATCHER_LOGGING;
@@ -366,3 +368,4 @@ exports.getNumberOfWatchers = () => {
366368
};
367369

368370
exports.createHandleChangeEvent = createHandleChangeEvent;
371+
exports.watcherLimit = watcherLimit;

test/ManyWatchers.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const path = require("path");
66
const TestHelper = require("./helpers/TestHelper");
77
const Watchpack = require("../lib/watchpack");
88
const watchEventSource = require("../lib/watchEventSource");
9+
const should = require("should");
910

1011
const fixtures = path.join(__dirname, "fixtures");
1112
const testHelper = new TestHelper(fixtures);
@@ -78,4 +79,11 @@ describe("ManyWatchers", function() {
7879
});
7980
});
8081
});
82+
83+
it("should set the watcher limit based on the platform", () => {
84+
should.equal(
85+
watchEventSource.watcherLimit,
86+
require("os").platform() === "darwin" ? 20 : 10000
87+
);
88+
});
8189
});

0 commit comments

Comments
 (0)