-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumerBPSolvedExample.js
100 lines (89 loc) · 2.51 KB
/
consumerBPSolvedExample.js
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
const kafka = require('kafka-node');
const config = require('./config');
const axios = require('axios');
const async = require('async')
//BENCHMARK STUFF
var isFirstMessage = true
var initialTimestamp
var nrConsumedMessages = 0
var maxMemory = 0
//ASYNC QUEUE VARS
var paused
const MAX_CONCURRENCY = 30
const PAUSE_CONSUMPTION_THRESHOLD = 2000
try {
const client = new kafka.KafkaClient({ kafkaHost: config.kafkaServer });
var consumer = new kafka.Consumer(
client,
[{ topic: config.kafkaTopic }],
{
groupId: 'bpsolved', //c onsumer group id,
autoCommit: false,
fetchMaxBytes: 1024 * 512, //0.5MB per fetch
encoding: 'utf8',
}
);
var consumerQueue = async.queue((payload, done) => {
setImmediate(async () => {
nrConsumedMessages = nrConsumedMessages + 1
await expensiveComputation(payload)
done()
})
}, MAX_CONCURRENCY);
// FUNCTION TO CALL WHEN CONSUMER QUEUE FINISHES ALL OUTSTANDING TASKS
consumerQueue.drain(() => {
resumeConsumer()
//IF MAX_CONCURRENCY > 1, OFFSETS NEED TO BE COMMITTED MANUALLY HERE AS OPPOSED TO AUTO COMMIT BECAUSE THERE IS NO GUARANTEE OF ORDER OF EXECUTION
/*consumer.commit((err, data) => {
resumeConsumer()
});*/
})
consumer.on('message', (message) => {
if(message.value){
const parsedEvent = JSON.parse(message.value)
consumerQueue.push(parsedEvent);
}
if(isFirstMessage){
initialTimestamp = Date.now()
isFirstMessage = false
setInterval(() => printBenchmark(), 500)
}
if (consumerQueue.length() >= PAUSE_CONSUMPTION_THRESHOLD && !paused) {
consumer.pause()
paused = true;
}
})
consumer.on('error', function(err) {
console.log('error', err)
});
}
catch(e) {
console.log(e);
}
const printBenchmark = () => {
let currMemoryUsage = process.memoryUsage().heapUsed / (1024 * 1024)
if( currMemoryUsage > maxMemory){
maxMemory = currMemoryUsage
}
console.info(
'Execution time(s): %ds | Average time per message (ms): %dms | Number Consumed Messages: %d | Queued events: %d | Max Mem Usage: %d MB',
(Date.now()-initialTimestamp)/1000,
(Date.now()-initialTimestamp)/nrConsumedMessages,
nrConsumedMessages,
consumerQueue.length(),
maxMemory
)
}
const resumeConsumer = () => {
consumer.resume()
paused = false;
}
const expensiveComputation = async (parsedEvent) => {
try {
if (parsedEvent) {
await axios.get('http://www.mocky.io/v2/5ebc25942e000060009f4247')
}
}
catch(err){
}
}