forked from ezpaarse-project/ezpaarse-middlewares
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
154 lines (124 loc) · 3.37 KB
/
utils.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
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
'use strict';
const co = require('co');
exports.bufferedProcess = function (mw, options) {
options = options || {};
let bufferSize = parseInt(options.bufferSize);
let packetSize = parseInt(options.packetSize);
let filter = options.filter;
let groupBy = options.groupBy;
let onPacket = options.onPacket;
let lastCallback = null;
let draining = false;
let buffer = [];
if (typeof groupBy === 'string') {
const field = groupBy;
groupBy = (ec) => ec[field];
} else if (typeof groupBy !== 'function') {
groupBy = null;
}
if (isNaN(bufferSize) || bufferSize < 1) {
bufferSize = 1000;
}
if (isNaN(packetSize) || packetSize < 1) {
packetSize = 50;
}
return function process (ec, next) {
if (typeof lastCallback === 'function') {
this.logger.error('Received an EC after termination signal');
}
if (!ec) {
lastCallback = next;
if (!draining) {
drainBuffer()
.then(() => { lastCallback(); })
.catch(err => {
mw.logger.error(err.message);
mw.job._stop(err);
});
}
return;
}
buffer.push([ec, next]);
if (buffer.length > bufferSize && !draining) {
mw.saturate();
drainBuffer().then(() => {
mw.drain();
if (typeof lastCallback === 'function') { lastCallback(); }
}).catch(err => {
mw.logger.error(err.message);
mw.job._stop(err);
});
}
};
/**
* Create a packet by pulling ECs from the buffer
*/
function* getPacket () {
const packet = {
ecs: [],
groups: new Map()
};
let maxGroupSize = 0;
function fullPacket () {
if (typeof groupBy === 'function') {
return maxGroupSize >= packetSize;
}
return packet.ecs.length >= packetSize;
}
while (!fullPacket()) {
const [ec, done] = buffer.shift() || [];
if (!ec) { break; }
if (typeof filter === 'function') {
try {
let keep = filter(ec);
if (keep instanceof Promise) {
keep = yield keep;
}
if (!keep) {
done();
continue;
}
} catch (e) {
done(e);
continue;
}
}
if (typeof groupBy === 'function') {
const groupId = groupBy(ec);
const group = packet.groups.get(groupId);
if (group) {
group.push([ec, done]);
maxGroupSize = Math.max(maxGroupSize, group.length);
} else {
packet.groups.set(groupId, [[ec, done]]);
maxGroupSize = Math.max(maxGroupSize, 1);
}
}
packet.ecs.push([ec, done]);
}
return packet;
}
/**
* Create and process packets until the buffer size is low enough or there's nothing left
*/
function drainBuffer () {
draining = true;
return co(function* () {
while (buffer.length >= bufferSize || (lastCallback && buffer.length > 0)) {
const packet = yield getPacket();
const res = onPacket(packet);
if (res instanceof Promise) {
yield res;
}
}
draining = false;
});
}
};
/**
* Wait a given amount of time (used to throttle queries to the API)
* @param {Integer} ms time to wait in milliseconds
*/
exports.wait = function wait(ms) {
return new Promise(resolve => { setTimeout(resolve, ms); });
};