diff options
author | isaacs <i@izs.me> | 2010-08-23 21:48:06 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2010-08-23 21:48:06 +0400 |
commit | 462ef6b22ba13cf91d85f081d7859a44488ce44c (patch) | |
tree | f9f3094fc779619d77335727a0397c5c885f94bf | |
parent | 20f2841ff8a2633695046caf6800e1f4caa783b8 (diff) |
A wrapper around the fs module that is persistent in the face of EMFILE errors.
-rw-r--r-- | lib/utils/graceful-fs.js | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/utils/graceful-fs.js b/lib/utils/graceful-fs.js new file mode 100644 index 000000000..0baa2576a --- /dev/null +++ b/lib/utils/graceful-fs.js @@ -0,0 +1,31 @@ + +// wrapper around the non-sync fs functions to gracefully handle +// having too many file descriptors open. Note that this is +// *only* possible because async patterns let one interject timeouts +// and other cleverness anywhere in the process without disrupting +// anything else. +var fs = require("fs") +Object.keys(fs) + .forEach(function (i) { + exports[i] = (typeof fs[i] !== "function") ? fs[i] + : (i.match(/^[A-Z]|^create|Sync$/)) ? function () { + return fs[i].apply(fs, arguments) + } + : graceful(fs[i]) + }) + + +function graceful (fn) { return function GRACEFUL () { + var args = Array.prototype.slice.call(arguments) + , cb_ = args.pop() + args.push(cb) + function cb (er) { + if (er && er.message.match(/^EMFILE, Too many open files/)) { + return setTimeout(function () { + GRACEFUL.apply(fs, args) + }) + } + cb_.apply(null, arguments) + } + fn.apply(fs, args) +}} |