Skip to content

Commit 799815b

Browse files
committed
Add multicore support
1 parent 6d6320c commit 799815b

File tree

1 file changed

+90
-78
lines changed

1 file changed

+90
-78
lines changed

bin/www

Lines changed: 90 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -15,102 +15,114 @@ config.https = config.https || {};
1515
const logger = require('morgan');
1616
const debug = require('debug')('pep-proxy:app');
1717
const express = require('express');
18+
const os = require('os');
19+
const cluster = require('cluster');
20+
const clusterWorkerSize = os.cpus().length;
1821

19-
process.on('uncaughtException', function(err) {
22+
process.on('uncaughtException', function (err) {
2023
debug('Caught exception: ' + err);
2124
});
2225
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
2326

24-
const app = express();
25-
26-
// Set logs in development
27-
if (config.debug) {
28-
app.use(logger('dev'));
29-
}
30-
31-
//app.use(express.bodyParser());
32-
33-
app.use(function(req, res, next) {
34-
const bodyChunks = [];
35-
req.on('data', function(chunk) {
36-
bodyChunks.push(chunk);
37-
});
38-
39-
req.on('end', function() {
40-
if (bodyChunks.length > 0) {
41-
req.body = Buffer.concat(bodyChunks);
42-
}
43-
next();
44-
});
45-
});
46-
47-
app.disable('x-powered-by');
48-
app.use(errorhandler({ log: debug}));
49-
app.use(cors(config.cors));
50-
5127
let port = config.pep_port || 80;
5228
if (config.https.enabled) {
5329
port = config.https.port || 443;
5430
}
55-
app.set('port', port);
5631

57-
for (const p in config.public_paths) {
58-
debug('Public paths', config.public_paths[p]);
59-
app.all(config.public_paths[p], Root.public);
60-
}
32+
function start_server() {
33+
const app = express();
6134

62-
app.all('/*', Root.pep);
35+
// Set logs in development
36+
if (config.debug) {
37+
app.use(logger('dev'));
38+
}
6339

40+
app.use(function (req, res, next) {
41+
const bodyChunks = [];
42+
req.on('data', function (chunk) {
43+
bodyChunks.push(chunk);
44+
});
6445

65-
function connectIDM(callback) {
66-
IDM.authenticate(
67-
function(token) {
68-
debug('Success authenticating PEP proxy. Proxy Auth-token: ', token);
69-
callback();
70-
},
71-
function(status, e) {
72-
debug('Error in IDM communication', e);
73-
callback(e);
74-
}
75-
);
76-
}
46+
req.on('end', function () {
47+
if (bodyChunks.length > 0) {
48+
req.body = Buffer.concat(bodyChunks);
49+
}
50+
next();
51+
});
52+
});
7753

78-
if (config.https.enabled === true) {
79-
const options = {
80-
key: fs.readFileSync(config.https.key_file),
81-
cert: fs.readFileSync(config.https.cert_file),
82-
};
83-
84-
https
85-
.createServer(options, function(req, res) {
86-
app.handle(req, res);
87-
})
88-
.listen(app.get('port'));
89-
} else {
90-
app.listen(app.get('port'));
54+
app.disable('x-powered-by');
55+
app.use(errorhandler({ log: debug }));
56+
app.use(cors(config.cors));
57+
app.set('port', port);
58+
59+
for (const p in config.public_paths) {
60+
debug('Public paths', config.public_paths[p]);
61+
app.all(config.public_paths[p], Root.public);
62+
}
63+
64+
app.all('/*', Root.pep);
65+
66+
if (config.https.enabled === true) {
67+
const options = {
68+
key: fs.readFileSync(config.https.key_file),
69+
cert: fs.readFileSync(config.https.cert_file)
70+
};
71+
72+
https
73+
.createServer(options, function (req, res) {
74+
app.handle(req, res);
75+
})
76+
.listen(app.get('port'));
77+
} else {
78+
app.listen(app.get('port'));
79+
}
9180
}
9281

93-
let retry = 20;
9482
function connect() {
95-
const connect_with_retry = () => {
96-
connectIDM((err) => {
97-
if (err) {
98-
retry--;
99-
if (retry === 0) {
100-
debug('Error found after [%d] attempts: %s', 20, error);
101-
process.exit(1);
102-
} else {
103-
console.log('retry after 5 seconds.');
104-
//eslint-disable-next-line snakecase/snakecase
105-
setTimeout(connect_with_retry, 5000);
83+
let retry = 20;
84+
return new Promise((resolve, reject) => {
85+
const connect_with_retry = () => {
86+
IDM.authenticate(
87+
(token) => {
88+
debug('Success authenticating PEP proxy.');
89+
resolve(token);
90+
},
91+
(status, e) => {
92+
debug('Error in IDM communication', e);
93+
retry--;
94+
if (retry === 0) {
95+
reject(e);
96+
} else {
97+
debug('retry after 5 seconds.');
98+
//eslint-disable-next-line snakecase/snakecase
99+
setTimeout(connect_with_retry, 5000);
100+
}
106101
}
107-
} else {
108-
debug('Success authenticating PEP proxy.');
109-
}
110-
});
111-
};
112-
connect_with_retry();
102+
);
103+
};
104+
connect_with_retry();
105+
});
113106
}
114107

115108
debug('Starting PEP proxy in port ' + port + '. IdM authentication ...');
116-
connect();
109+
connect().then(
110+
(token) => {
111+
debug('Success authenticating PEP proxy. Proxy Auth-token: ', token);
112+
if (clusterWorkerSize > 1) {
113+
if (cluster.isMaster) {
114+
for (let i = 0; i < clusterWorkerSize; i++) {
115+
cluster.fork();
116+
}
117+
} else {
118+
start_server();
119+
}
120+
} else {
121+
start_server();
122+
}
123+
},
124+
(err) => {
125+
debug('Error found after [%d] attempts: %s', 20, err);
126+
process.exit(1);
127+
}
128+
);

0 commit comments

Comments
 (0)