wrap workers in a function scope and add a validateMessage method to HK's Env

pull/1/head
ansuz 5 years ago
parent 5467e1ffac
commit 019e5e708b

@ -5,9 +5,7 @@ const Nacl = require('tweetnacl/nacl-fast');
// XXX npm "os" and "child_process" // 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);
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('In process', process.pid);
//console.log(+new Date(), "Message received by subprocess"); //console.log(+new Date(), "Message received by subprocess");

@ -76,6 +76,8 @@ module.exports.create = function (config, cb) {
domain: config.domain domain: config.domain
}; };
HK.initializeValidationWorkers(Env);
(function () { (function () {
var pes = config.premiumUploadSize; var pes = config.premiumUploadSize;
if (!isNaN(pes) && pes >= Env.maxUploadSize) { if (!isNaN(pes) && pes >= Env.maxUploadSize) {

@ -924,17 +924,20 @@ HK.onDirectMessage = function (Env, Server, seq, userId, json) {
* writes messages to the store * writes messages to the store
*/ */
HK.initializeValidationWorkers = function (Env) {
if (typeof(Env.validateMessage) !== 'undefined') {
return void console.error("validation workers are already initialized");
}
// Create our workers // Create our workers
const workers = []; const workers = [];
for (let i = 0; i < numCPUs; i++) { for (let i = 0; i < numCPUs; i++) {
workers.push(fork('lib/check-signature.js')); workers.push(fork('lib/check-signature.js'));
} }
var nextWorker = 0;
const response = Util.response(); const response = Util.response();
var initWorker = function (worker) { var initWorker = function (worker) {
worker.on('message', function (res) { worker.on('message', function (res) {
if (!res || !res.txid) { return; } if (!res || !res.txid) { return; }
//console.log(+new Date(), "Received verification response"); //console.log(+new Date(), "Received verification response");
@ -952,11 +955,11 @@ var initWorker = function (worker) {
workers.push(w); workers.push(w);
initWorker(w); initWorker(w);
}); });
}; };
workers.forEach(initWorker); workers.forEach(initWorker);
const validateMessage = function (signedMsg, key, _cb) { var nextWorker = 0;
Env.validateMessage = function (signedMsg, key, _cb) {
// let's be paranoid about asynchrony and only calling back once.. // let's be paranoid about asynchrony and only calling back once..
var cb = Util.once(Util.mkAsync(_cb)); var cb = Util.once(Util.mkAsync(_cb));
@ -977,6 +980,7 @@ const validateMessage = function (signedMsg, key, _cb) {
msg: signedMsg, msg: signedMsg,
key: key, key: key,
}); });
};
}; };
HK.onChannelMessage = function (Env, Server, channel, msgStruct) { HK.onChannelMessage = function (Env, Server, channel, msgStruct) {
@ -1025,7 +1029,7 @@ HK.onChannelMessage = function (Env, Server, channel, msgStruct) {
// Listen for messages // Listen for messages
//console.log(+new Date(), "Send verification request"); //console.log(+new Date(), "Send verification request");
validateMessage(signedMsg, metadata.validateKey, w(function (err) { Env.validateMessage(signedMsg, metadata.validateKey, w(function (err) {
if (err) { if (err) {
// validation can fail in multiple ways // validation can fail in multiple ways
if (err === 'FAILED') { if (err === 'FAILED') {

Loading…
Cancel
Save