-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.js
149 lines (127 loc) · 3.09 KB
/
consumer.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
class Consumer {
constructor(stream, id, startNode, timeout) {
this.id = id;
this._backpressure = 0;
this.currentNode = startNode;
this.timeout = timeout;
this.isAlive = true;
this.stream = stream;
this.stream.setConsumer(this.id, this);
}
getStats() {
let stats = {
id: this.id,
backpressure: this._backpressure
};
if (this.timeout != null) {
stats.timeout = this.timeout;
}
return stats;
}
_resetBackpressure() {
this._backpressure = 0;
}
applyBackpressure(packet) {
this._backpressure++;
}
releaseBackpressure(packet) {
this._backpressure--;
}
getBackpressure() {
return this._backpressure;
}
clearActiveTimeout() {
clearTimeout(this._timeoutId);
delete this._timeoutId;
}
write(packet) {
if (this._timeoutId !== undefined) {
this.clearActiveTimeout(packet);
}
this.applyBackpressure(packet);
if (this._resolve) {
this._resolve();
delete this._resolve;
}
}
kill(value) {
this._killPacket = {value, done: true};
if (this._timeoutId !== undefined) {
this.clearActiveTimeout(this._killPacket);
}
this._destroy();
if (this._resolve) {
this._resolve();
delete this._resolve;
}
}
_destroy() {
this.isAlive = false;
this._resetBackpressure();
this.stream.removeConsumer(this.id);
}
async _waitForNextItem(timeout) {
return new Promise((resolve, reject) => {
this._resolve = resolve;
let timeoutId;
if (timeout !== undefined) {
// Create the error object in the outer scope in order
// to get the full stack trace.
let error = new Error('Stream consumer iteration timed out');
(async () => {
let delay = wait(timeout);
timeoutId = delay.timeoutId;
await delay.promise;
error.name = 'TimeoutError';
delete this._resolve;
reject(error);
})();
}
this._timeoutId = timeoutId;
});
}
async next() {
this.stream.setConsumer(this.id, this);
while (true) {
if (!this.currentNode.next) {
try {
await this._waitForNextItem(this.timeout);
} catch (error) {
this._destroy();
throw error;
}
}
if (this._killPacket) {
this._destroy();
let killPacket = this._killPacket;
delete this._killPacket;
return killPacket;
}
this.currentNode = this.currentNode.next;
this.releaseBackpressure(this.currentNode.data);
if (this.currentNode.consumerId && this.currentNode.consumerId !== this.id) {
continue;
}
if (this.currentNode.data.done) {
this._destroy();
}
return this.currentNode.data;
}
}
return() {
delete this.currentNode;
this._destroy();
return {};
}
[Symbol.asyncIterator]() {
return this;
}
}
function wait(timeout) {
let timeoutId;
let promise = new Promise((resolve) => {
timeoutId = setTimeout(resolve, timeout);
});
return {timeoutId, promise};
}
module.exports = Consumer;