-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.js
More file actions
200 lines (173 loc) · 6.26 KB
/
consumer.js
File metadata and controls
200 lines (173 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"use strict";
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
var http = require('http');
var path = require('path');
var fs = require('fs');
var amqplib = require('amqplib');
var env = process.env.NODE_ENV || 'production';
var config = require(path.join(__dirname, 'config', 'config.json'))[env];
var debug, debugModule = require('debug');
// Increase the number of available sockets
http.globalAgent.maxSockets = Infinity;
var posix = require('posix');
posix.setrlimit('nofile', { soft: 1048000 });
if (cluster.isMaster) {
config.logging && debugModule.enable('cluster');
debug = debugModule('cluster');
cluster.setupMaster({
exec: __filename,
silent: !config.logging
});
// Fork workers.
// Make sure that one CPU is available for master and other stuff.
for (var j = 0; j < Math.max(numCPUs - 1, 1); j++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
if (worker.suicide) {
return;
}
debug('Worker %d died (%s). Restarting...', worker.process.pid, signal || code);
cluster.fork();
});
cluster.on('fork', function(worker) {
debug('Worker %d started.', worker.process.pid);
});
process.on('SIGINT',function() {
// Prevent process from exiting immediately
setTimeout(function() {
debug('Exiting...');
process.exit(0);
}, 1000);
for (var id in cluster.workers) {
if (cluster.workers.hasOwnProperty(id)) {
cluster.workers[id].kill();
}
}
});
return;
}
config.logging && debugModule.enable('worker');
debug = debugModule('worker');
var opts = {
cert: fs.readFileSync(path.join(__dirname, 'config', 'certs', config.rabbitClientCert)),
key: fs.readFileSync(path.join(__dirname, 'config', 'certs', config.rabbitClientKey)),
passphrase: config.rabbitClientKeyPassPhrase,
ca: [fs.readFileSync(path.join(__dirname, 'config', 'certs', config.rabbitCaCert))],
noDelay: true,
heartbeat: config.rabbitConnectionHeartbeat
};
// Open several connections to the RabbitMQ in order to increase throughput
var currentChannelNumber = 0;
var rabbitChannels = [];
for (var i = 0; i < config.rabbitConnectionsPerWorker; i++) {
rabbitChannels.push(
createRabbitChannel()
);
}
function createRabbitChannel() {
return amqplib.connect(
'amqps://' + encodeURIComponent(config.rabbitUser) + ':' + encodeURIComponent(config.rabbitPassword) +
'@' + config.rabbitServer + ':' + config.rabbitPort + '/' + encodeURIComponent(config.rabbitVhost),
opts
).then(function(connection) {
return connection.createChannel().then(function(channel) {
return channel.assertQueue('messages', {
exclusive: false,
durable: true
}).then(function(queue) {
return new Promise(function(resolve) {
resolve(channel);
});
}, function(error) {
debug('RabbitMQ queue erorr: %s', error);
process.exit(1);
});
}, function(error) {
debug('RabbitMQ channel erorr: %s', error);
process.exit(1);
});
}, function(error) {
debug('RabbitMQ connection error: %s', error);
process.exit(1);
});
}
function closerRabbitChannels() {
rabbitChannels.forEach(function(channelPromise) {
channelPromise.then(function(channel) {
channel.close();
channel.connection.close();
});
});
}
process.on('SIGINT', function() {
// Prevent process from exiting immediately
setTimeout(function() {
debug('Exiting...');
process.exit(0);
}, 1000);
closerRabbitChannels();
});
cluster.worker.on('exit', closerRabbitChannels);
process.on('exit', closerRabbitChannels);
process.on('uncaughtException', function (error) {
debug('Uncaught exception: %s', error);
console.log(error.stack);
process.exit(1);
});
// Workers can share any TCP connection
// In our case its a HTTP server
var httpPort = parseInt(config.port, 10);
var server = http.createServer(function (request, response) {
request.on('error', function(error) {
debug('Problem with request: %s', error);
});
response.on('error', function(error) {
debug('Problem with response: %s', error);
});
request.on('socket', function (socket) {
// disable connection timeout
socket.setTimeout(0);
// disable Nagle algorithm
socket.setNoDelay(true);
socket.on('timeout', function (exception, socket) {
debug('Socket timeout: %s', exception);
});
});
if ('POST' == request.method) {
var body = null;
request.on('data', function (data) {
body = (null === body) ? data : Buffer.concat([body, data]);
// Too much POST data, kill the connection!
if (body.length > 1e6) {
request.connection.destroy();
}
});
request.on('end', function () {
if (null === body) {
return;
}
currentChannelNumber = (currentChannelNumber + 1) % config.rabbitConnectionsPerWorker;
var channelPromise = rabbitChannels[currentChannelNumber];
channelPromise.then(function(channel) {
if (!channel) {
throw 'Channel does not exist';
}
channel.sendToQueue('messages', body, {
persistent: true // or deliveryMode: 2
});
}, function(error) {
debug('RabbitMQ channel error: %s. Try to reconnect...', error);
rabbitChannels[currentChannelNumber] = createRabbitChannel();
});
});
}
response.writeHead(204);
response.end();
});
server.listen(httpPort);
console.log('Listening on port ' + httpPort);
server.on('clientError', function (exception, socket) {
debug('Problem with client: %s', exception);
});