Skip to content

Commit

Permalink
feat(server): add multi-core support via the cluster module
Browse files Browse the repository at this point in the history
  • Loading branch information
missinglink committed Mar 20, 2020
1 parent 2bd95b2 commit dadecb7
Showing 1 changed file with 58 additions and 7 deletions.
65 changes: 58 additions & 7 deletions cmd/server.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
/**
The http server improves performance on multicore machines by using the
node core 'cluster' module to fork worker processes.
The default setting is to use all available CPUs, this will spawn 32 child
processes on a 32 core machine.
If you would like to disable this feature (maybe because you are running
inside a container) then you can do so by setting the env var CPUS=1
You may also specify exactly how many child processes you would like to
spawn by setting the env var to a numeric value >1, eg CPUS=4
If the CPUS env var is set less than 1 or greater than os.cpus().length
then the default setting will be used (using all available cores).
**/

const os = require('os');
const express = require('express');
const cluster = require('cluster');
const polyline = require('@mapbox/polyline');
const search = require('../api/search');
const extract = require('../api/extract');
const street = require('../api/street');
const near = require('../api/near');
const pretty = require('../lib/pretty');
const analyze = require('../lib/analyze');

const morgan = require( 'morgan' );
const logger = require('pelias-logger').get('interpolation');
const through = require( 'through2' );
const _ = require('lodash');

// optionally override port using env var
// select the amount of cpus we will use
const envCpus = parseInt(process.env.CPUS, 10);
const cpus = Math.min(Math.max(envCpus || Infinity, 1), os.cpus().length);

// optionally override port/host using env var
const PORT = process.env.PORT || 3000;
const HOST = process.env.HOST || undefined;

// help text
if( process.argv.length !== 4 ){
Expand Down Expand Up @@ -217,10 +240,38 @@ app.use('/demo', express.static('demo'));
// app.use('/builds', express.static('/data/builds'));
// app.use('/builds', directory('/data/builds', { hidden: false, icons: false, view: 'details' }));

app.listen( PORT, async function() {
// start multi-threaded server
if (cpus > 1) {
if (cluster.isMaster) {
logger.info('[master] using %d cpus', cpus);

// force loading of libpostal
await analyze.street( 'test street' );
// worker exit event
cluster.on('exit', (worker, code, signal) => {
logger.error('[master] worker died', worker.process.pid);
});

console.log( 'server listening on port', PORT );
});
// worker fork event
cluster.on('fork', (worker, code, signal) => {
logger.info('[master] worker forked', worker.process.pid);
});

// fork workers
for (var c = 0; c < cpus; c++) {
cluster.fork();
}

} else {
app.listen(PORT, HOST, () => {
logger.info('[worker %d] listening on %s:%s', process.pid, HOST || '0.0.0.0', PORT);
});
}
}

// start single-threaded server
else {
logger.info('[master] using %d cpus', cpus);

app.listen(PORT, HOST, async () => {
logger.info('[master] listening on %s:%s', HOST || '0.0.0.0', PORT);
});
}

0 comments on commit dadecb7

Please sign in to comment.