Use more subprocesses
This commit is contained in:
parent
0d636dabc9
commit
fb0eb1b20c
@ -1,8 +1,13 @@
|
|||||||
const Nacl = require('tweetnacl/nacl-fast');
|
const Nacl = require('tweetnacl/nacl-fast');
|
||||||
|
|
||||||
|
// XXX npm "os" and "child_process"
|
||||||
|
|
||||||
// TODO if this process is using too much CPU, we can use "cluster" to add load balancing to this code
|
// TODO if this process is using too much CPU, we can use "cluster" to add load balancing to this code
|
||||||
|
|
||||||
|
console.log('New child process', process.pid);
|
||||||
|
|
||||||
process.on('message', function (data) {
|
process.on('message', function (data) {
|
||||||
|
console.log('In process', process.pid)
|
||||||
console.log(+new Date(), "Message received by subprocess");
|
console.log(+new Date(), "Message received by subprocess");
|
||||||
if (!data || !data.key || !data.msg || !data.txid) {
|
if (!data || !data.key || !data.msg || !data.txid) {
|
||||||
process.send({
|
process.send({
|
||||||
|
|||||||
@ -7,6 +7,7 @@ const Util = require("./common-util");
|
|||||||
const MetaRPC = require("./commands/metadata");
|
const MetaRPC = require("./commands/metadata");
|
||||||
const Nacl = require('tweetnacl/nacl-fast');
|
const Nacl = require('tweetnacl/nacl-fast');
|
||||||
const { fork } = require('child_process');
|
const { fork } = require('child_process');
|
||||||
|
const numCPUs = require('os').cpus().length;
|
||||||
|
|
||||||
const now = function () { return (new Date()).getTime(); };
|
const now = function () { return (new Date()).getTime(); };
|
||||||
const ONE_DAY = 1000 * 60 * 60 * 24; // one day in milliseconds
|
const ONE_DAY = 1000 * 60 * 60 * 24; // one day in milliseconds
|
||||||
@ -922,11 +923,32 @@ HK.onDirectMessage = function (Env, Server, seq, userId, json) {
|
|||||||
* adds timestamps to incoming messages
|
* adds timestamps to incoming messages
|
||||||
* writes messages to the store
|
* writes messages to the store
|
||||||
*/
|
*/
|
||||||
const check = fork('lib/check-signature.js');
|
|
||||||
|
|
||||||
const onChecked = Util.mkEvent();
|
const onChecked = Util.mkEvent();
|
||||||
check.on('message', function (res) {
|
// Create our workers
|
||||||
|
const workers = [];
|
||||||
|
for (let i = 0; i < numCPUs; i++) {
|
||||||
|
workers.push(fork('lib/check-signature.js'));
|
||||||
|
}
|
||||||
|
var initWorker = function (worker) {
|
||||||
|
worker.on('message', function (res) {
|
||||||
onChecked.fire(res);
|
onChecked.fire(res);
|
||||||
});
|
});
|
||||||
|
// Spawn a new process in one ends
|
||||||
|
worker.on('exit', function () {
|
||||||
|
// XXX make sure it's dead?
|
||||||
|
var idx = workers.indexOf(worker);
|
||||||
|
if (idx !== -1) {
|
||||||
|
workers.splice(idx, 1);
|
||||||
|
}
|
||||||
|
// Spawn a new one
|
||||||
|
var w = fork('lib/check-signature.js');
|
||||||
|
workers.push(w);
|
||||||
|
initWorker(w);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
workers.forEach(initWorker);
|
||||||
|
|
||||||
HK.onChannelMessage = function (Env, Server, channel, msgStruct) {
|
HK.onChannelMessage = function (Env, Server, channel, msgStruct) {
|
||||||
console.log(+new Date(), "onChannelMessage");
|
console.log(+new Date(), "onChannelMessage");
|
||||||
@ -996,7 +1018,8 @@ HK.onChannelMessage = function (Env, Server, channel, msgStruct) {
|
|||||||
|
|
||||||
console.log(+new Date(), "Send verification request");
|
console.log(+new Date(), "Send verification request");
|
||||||
// Send the request
|
// Send the request
|
||||||
check.send({
|
const random = Math.floor(Math.random() * 4);
|
||||||
|
workers[random].send({
|
||||||
txid: txid,
|
txid: txid,
|
||||||
msg: signedMsg,
|
msg: signedMsg,
|
||||||
key: metadata.validateKey
|
key: metadata.validateKey
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user