Skip to content

@DukeyToo /fib.json
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
{ "exports": "fib",
"signature": [
{
"name": "fib",
"synopsis": "runs a fibonacci sequence",
"verb": "POST",
"parameters": [
{"name": "number", "required":true, "description":"which fibonacci to calculate"}
]
},
{
"name": "randomfib2",
"synopsis": "runs a random fibonacci sequence, 1 to 30, using a long-lived worker process"
},
{
"name": "randomfib3",
"synopsis": "runs a random fibonacci sequence, 1 to 30, using multiple long-lived worker processes"
}
],
"path": "fib"
}
#!/usr/bin/env node
var perfectapi = require('perfectapi.js');
var path = require('path');
var configPath = path.resolve(__dirname, 'fib.json');
var parser = new perfectapi.Parser();
var worker = require('child_process').fork(__dirname + '/fibworker.js');
var callbacks = {};
var requestNum = 0;
worker.on('message', function(m) {
var callback = callbacks[m.id];
delete callbacks[m.id];
callback(null, m.result);
})
var numWorkers = require('os').cpus().length;
var nextWorker = 0;
var workers = [];
for (var i=0;i<numWorkers;i++) {
var worker = require('child_process').fork(__dirname + '/fibworker.js');
workers.push(worker);
worker.on('message', function(m) {
var callback = callbacks[m.id];
delete callbacks[m.id];
callback(null, m.result);
})
}
//handle the commands
parser.on('fib', function(config, callback) {
var n = config.number;
if (n<1 || n>40) {
callback('n must be between 1 and 40');
} else {
callback(null, fibonacci(n))
}
})
parser.on('randomfib2', function(config, callback) {
requestNum += 1;
callbacks[requestNum] = callback;
var n = Math.ceil(Math.random()*30)
worker.send({n: n, id: requestNum});
})
parser.on('randomfib3', function(config, callback) {
requestNum += 1;
callbacks[requestNum] = callback;
var n = Math.ceil(Math.random()*30);
//round robin workers
var worker = workers[nextWorker];
nextWorker += 1;
if (nextWorker == workers.length) nextWorker = 0;
worker.send({n: n, id: requestNum});
})
//expose the api
module.exports = parser.parse(configPath);
module.exports = fibonacci;
process.on('message', function(m) {
var n = m.n;
var id = m.id;
var result;
if (n < 2) {
result = 1
} else {
result = fibonacci(n-2) + fibonacci(n-1)
}
process.send({id: id, result: result});
})
function fibonacci(n) {
if (n < 2)
return 1;
else
return fibonacci(n-2) + fibonacci(n-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.