Skip to content

Commit

Permalink
Merge pull request #129 from jason-fox/feature/cluster
Browse files Browse the repository at this point in the history
Add multicore support
  • Loading branch information
apozohue10 authored Jul 22, 2021
2 parents 6d6320c + 799815b commit 38a3742
Showing 1 changed file with 90 additions and 78 deletions.
168 changes: 90 additions & 78 deletions bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -15,102 +15,114 @@ config.https = config.https || {};
const logger = require('morgan');
const debug = require('debug')('pep-proxy:app');
const express = require('express');
const os = require('os');
const cluster = require('cluster');
const clusterWorkerSize = os.cpus().length;

process.on('uncaughtException', function(err) {
process.on('uncaughtException', function (err) {
debug('Caught exception: ' + err);
});
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

const app = express();

// Set logs in development
if (config.debug) {
app.use(logger('dev'));
}

//app.use(express.bodyParser());

app.use(function(req, res, next) {
const bodyChunks = [];
req.on('data', function(chunk) {
bodyChunks.push(chunk);
});

req.on('end', function() {
if (bodyChunks.length > 0) {
req.body = Buffer.concat(bodyChunks);
}
next();
});
});

app.disable('x-powered-by');
app.use(errorhandler({ log: debug}));
app.use(cors(config.cors));

let port = config.pep_port || 80;
if (config.https.enabled) {
port = config.https.port || 443;
}
app.set('port', port);

for (const p in config.public_paths) {
debug('Public paths', config.public_paths[p]);
app.all(config.public_paths[p], Root.public);
}
function start_server() {
const app = express();

app.all('/*', Root.pep);
// Set logs in development
if (config.debug) {
app.use(logger('dev'));
}

app.use(function (req, res, next) {
const bodyChunks = [];
req.on('data', function (chunk) {
bodyChunks.push(chunk);
});

function connectIDM(callback) {
IDM.authenticate(
function(token) {
debug('Success authenticating PEP proxy. Proxy Auth-token: ', token);
callback();
},
function(status, e) {
debug('Error in IDM communication', e);
callback(e);
}
);
}
req.on('end', function () {
if (bodyChunks.length > 0) {
req.body = Buffer.concat(bodyChunks);
}
next();
});
});

if (config.https.enabled === true) {
const options = {
key: fs.readFileSync(config.https.key_file),
cert: fs.readFileSync(config.https.cert_file),
};

https
.createServer(options, function(req, res) {
app.handle(req, res);
})
.listen(app.get('port'));
} else {
app.listen(app.get('port'));
app.disable('x-powered-by');
app.use(errorhandler({ log: debug }));
app.use(cors(config.cors));
app.set('port', port);

for (const p in config.public_paths) {
debug('Public paths', config.public_paths[p]);
app.all(config.public_paths[p], Root.public);
}

app.all('/*', Root.pep);

if (config.https.enabled === true) {
const options = {
key: fs.readFileSync(config.https.key_file),
cert: fs.readFileSync(config.https.cert_file)
};

https
.createServer(options, function (req, res) {
app.handle(req, res);
})
.listen(app.get('port'));
} else {
app.listen(app.get('port'));
}
}

let retry = 20;
function connect() {
const connect_with_retry = () => {
connectIDM((err) => {
if (err) {
retry--;
if (retry === 0) {
debug('Error found after [%d] attempts: %s', 20, error);
process.exit(1);
} else {
console.log('retry after 5 seconds.');
//eslint-disable-next-line snakecase/snakecase
setTimeout(connect_with_retry, 5000);
let retry = 20;
return new Promise((resolve, reject) => {
const connect_with_retry = () => {
IDM.authenticate(
(token) => {
debug('Success authenticating PEP proxy.');
resolve(token);
},
(status, e) => {
debug('Error in IDM communication', e);
retry--;
if (retry === 0) {
reject(e);
} else {
debug('retry after 5 seconds.');
//eslint-disable-next-line snakecase/snakecase
setTimeout(connect_with_retry, 5000);
}
}
} else {
debug('Success authenticating PEP proxy.');
}
});
};
connect_with_retry();
);
};
connect_with_retry();
});
}

debug('Starting PEP proxy in port ' + port + '. IdM authentication ...');
connect();
connect().then(
(token) => {
debug('Success authenticating PEP proxy. Proxy Auth-token: ', token);
if (clusterWorkerSize > 1) {
if (cluster.isMaster) {
for (let i = 0; i < clusterWorkerSize; i++) {
cluster.fork();
}
} else {
start_server();
}
} else {
start_server();
}
},
(err) => {
debug('Error found after [%d] attempts: %s', 20, err);
process.exit(1);
}
);

0 comments on commit 38a3742

Please sign in to comment.