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
{ "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" | |
} |
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
#!/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); |
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
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