Skip to content

Commit

Permalink
Change threading model (#7)
Browse files Browse the repository at this point in the history
* Change threading model to use cluster

* increase thread pool size

* Default thread pool size to 2

* default thread pool size 4
  • Loading branch information
JesseCrocker committed Apr 21, 2016
1 parent d6789a1 commit b8a4afd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
24 changes: 23 additions & 1 deletion bin/squirrel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

"use strict";

process.env.UV_THREADPOOL_SIZE = process.env.UV_THREADPOOL_SIZE || 4;

var nomnom = require("nomnom")
.options({
config: {
Expand Down Expand Up @@ -43,5 +45,25 @@ case !opts.config:
return nomnom.print(nomnom.getUsage());

default:
return require("../lib/cacher")(opts);
return run(opts);
}

function run(opts) {
var cluster = require('cluster'),
numCPUs = require('os').cpus().length;

var numworkers = process.env.WORKER_COUNT || numCPUs;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numworkers; i++) {
cluster.fork();
}

cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
console.log("Starting worker");
require("../lib/cacher")(opts);
}
}
4 changes: 0 additions & 4 deletions lib/cacher.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#!/usr/bin/env node
"use strict";

// increase the libuv threadpool size to 1.5x the number of logical CPUs.
process.env.UV_THREADPOOL_SIZE = process.env.UV_THREADPOOL_SIZE ||
Math.ceil(Math.max(4, require('os').cpus().length * 1.5));

var path = require("path"),
debug = require("debug"),
rabbit = require('rabbit.js'),
Expand Down

0 comments on commit b8a4afd

Please sign in to comment.