-
Notifications
You must be signed in to change notification settings - Fork 0
/
queues.ts
199 lines (166 loc) · 4.96 KB
/
queues.ts
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
/**
* The function that will be called when an item is processed.
* If the function returns false, the processing will stop.
*/
type QueueItemCallback<T> = (message: T) => boolean | Promise<boolean>;
type QueueArgs<T> = {
/**
* See {@link QueueItemCallback}
*/
itemCallback: QueueItemCallback<T>;
/**
* If true, the queue will not process any items, even when {@link BaseQueue.process} is called.
* To enable processing, call {@link BaseQueue.enable} or {@link BaseQueue.enableAndProcess}.
* @default false
*/
isDisabled?: boolean;
}
export class BaseQueue<T> {
private _queue: T[] = [];
private _itemCallback: QueueItemCallback<T>;
private _canProcess = true;
private _isProcessing = false;
constructor(args: QueueArgs<T>) {
if (!args.itemCallback) {
throw new Error("itemCallback is required");
}
this._itemCallback = args.itemCallback;
if (args.isDisabled) {
this.disable();
}
}
public enable() {
this._canProcess = true;
}
public disable() {
this._canProcess = false;
}
public add(item: T) {
this._queue.push(item);
}
public addAndProcess(item: T) {
this.add(item);
this.process();
}
public enableAndProcess() {
this.enable();
this.process();
}
public process() {
if (!this._canProcess) {
return;
}
if (this._isProcessing) {
return;
}
this._isProcessing = true;
this._processNext();
}
private _processNext() {
if (!this._canProcess) {
return;
}
if (this._queue.length === 0) {
this._isProcessing = false;
return;
}
const item = this._queue.shift();
// process the item, making sure we wait for the result before processing the next item
Promise.resolve(this._itemCallback(item!)).then((shouldContinue) => {
if (shouldContinue) {
this._processNext();
} else {
this._isProcessing = false;
}
}).catch(() => {
this._isProcessing = false;
});
}
}
type AckMessagesQueueArgs = {
expirationMs: number;
timeoutExpiredCallback: AckTimeoutExpiredCallback;
}
type AckMessage = {
sequenceNumber: bigint;
addedAt: number;
}
type AckTimeoutExpiredCallback = (notReceivedAck: AckMessage['sequenceNumber'][]) => void;
export class AckMessagesQueue {
private _queue: AckMessage[] = [];
private _expirationMs: number;
private _timeoutExpiredCallback: AckTimeoutExpiredCallback;
private _lastAckTimeout: NodeJS.Timeout | null = null;
constructor(args: AckMessagesQueueArgs) {
if (!args.expirationMs) {
throw new Error("checkTimeoutMs is required");
}
this._expirationMs = Math.floor(args.expirationMs); // make sure it's an integer
if (!args.timeoutExpiredCallback) {
throw new Error("timeoutExpiredCallback is required");
}
this._timeoutExpiredCallback = args.timeoutExpiredCallback;
}
private _startLastAckTimeout() {
this._lastAckTimeout = setTimeout(() => {
this._onTimeoutExpired(this._queue);
}, this._expirationMs);
}
private _restartLastAckTimeout() {
if (this._lastAckTimeout) {
clearTimeout(this._lastAckTimeout);
}
this._startLastAckTimeout();
}
private _onTimeoutExpired(items: AckMessage[]) {
this._timeoutExpiredCallback(items.map((item) => item.sequenceNumber));
this._queue = [];
}
public add(sequenceNumber: bigint) {
const last = this.last();
if (last && sequenceNumber <= last.sequenceNumber) {
throw new Error(`Sequence number ${sequenceNumber} is not greater than last: ${last.sequenceNumber}`);
}
this._queue.push({
sequenceNumber,
addedAt: Date.now(),
});
if (!this._lastAckTimeout) {
this._startLastAckTimeout();
}
}
public ack(sequenceNumber: bigint) {
const index = this._queue.findIndex((item) => item.sequenceNumber === sequenceNumber);
if (index >= 0) {
// remove all items up to and including the acked item
this._queue.splice(0, index + 1);
} else {
const last = this.last();
// we throw an error only if the received sequence number is not in the queue
// and is greater than the last sequence number in the queue
if (last && sequenceNumber > last.sequenceNumber) {
throw new Error(`Sequence number ${sequenceNumber} is greater than last: ${last.sequenceNumber}`);
}
}
// for the remaining items in the queue, check if they have expired
// if yes, call the callback for the expired items
const expiredItems = this._queue.filter((item) => Date.now() - item.addedAt >= this._expirationMs);
if (expiredItems.length > 0) {
return this._onTimeoutExpired(expiredItems);
}
this._restartLastAckTimeout();
}
public last(): AckMessage | null {
if (this._queue.length === 0) {
return null;
}
return this._queue[this._queue.length - 1];
}
public clear() {
this._queue = [];
if (this._lastAckTimeout) {
clearTimeout(this._lastAckTimeout);
this._lastAckTimeout = null;
}
}
}