-
Notifications
You must be signed in to change notification settings - Fork 227
/
lease-manager.ts
303 lines (279 loc) · 8.56 KB
/
lease-manager.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*!
* Copyright 2018 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {EventEmitter} from 'events';
import {AckError, Message, Subscriber} from './subscriber';
import {defaultOptions} from './default-options';
export interface FlowControlOptions {
allowExcessMessages?: boolean;
maxBytes?: number;
maxMessages?: number;
maxExtensionMinutes?: number;
/** @deprecated Use maxExtensionMinutes. */
maxExtension?: number;
}
/**
* @typedef {object} FlowControlOptions
* @property {boolean} [allowExcessMessages=true] PubSub delivers messages in
* batches with no way to configure the batch size. Sometimes this can be
* overwhelming if you only want to process a few messages at a time.
* Setting this option to false will make the client manage any excess
* messages until you're ready for them. This will prevent them from being
* redelivered and make the maxMessages option behave more predictably.
* @property {number} [maxBytes=104857600] The desired amount of memory to
* allow message data to consume. (Default: 100MB) It's possible that this
* value will be exceeded, since messages are received in batches.
* @property {number} [maxExtensionMinutes=60] The maximum duration (in minutes)
* to extend the message deadline before redelivering.
* @property {number} [maxMessages=1000] The desired number of messages to allow
* in memory before pausing the message stream. Unless allowExcessMessages
* is set to false, it is very likely that this value will be exceeded since
* any given message batch could contain a greater number of messages than
* the desired amount of messages.
*/
/**
* Manages a Subscribers inventory while auto-magically extending the message
* deadlines.
*
* @private
* @class
*
* @param {Subscriber} sub The subscriber to manage leases for.
* @param {FlowControlOptions} options Flow control options.
*/
export class LeaseManager extends EventEmitter {
bytes: number;
private _isLeasing: boolean;
private _messages: Set<Message>;
private _options!: FlowControlOptions;
private _pending: Message[];
private _subscriber: Subscriber;
private _timer?: NodeJS.Timeout;
constructor(sub: Subscriber, options = {}) {
super();
this.bytes = 0;
this._isLeasing = false;
this._messages = new Set();
this._pending = [];
this._subscriber = sub;
this.setOptions(options);
}
/**
* @type {number}
* @private
*/
get pending(): number {
return this._pending.length;
}
/**
* @type {number}
* @private
*/
get size(): number {
return this._messages.size;
}
/**
* Adds a message to the inventory, kicking off the deadline extender if it
* isn't already running.
*
* @param {Message} message The message.
* @private
*/
add(message: Message): void {
const {allowExcessMessages} = this._options;
const wasFull = this.isFull();
this._messages.add(message);
this.bytes += message.length;
if (allowExcessMessages! || !wasFull) {
this._dispense(message);
} else {
this._pending.push(message);
}
if (!this._isLeasing) {
this._isLeasing = true;
this._scheduleExtension();
}
if (!wasFull && this.isFull()) {
this.emit('full');
}
}
/**
* Removes ALL messages from inventory.
* @private
*/
clear(): void {
const wasFull = this.isFull();
this._pending = [];
this._messages.clear();
this.bytes = 0;
if (wasFull) {
process.nextTick(() => this.emit('free'));
}
this._cancelExtension();
}
/**
* Indicates if we're at or over capacity.
*
* @returns {boolean}
* @private
*/
isFull(): boolean {
const {maxBytes, maxMessages} = this._options;
return this.size >= maxMessages! || this.bytes >= maxBytes!;
}
/**
* Removes a message from the inventory. Stopping the deadline extender if no
* messages are left over.
*
* @fires LeaseManager#free
*
* @param {Message} message The message to remove.
* @private
*/
remove(message: Message): void {
if (!this._messages.has(message)) {
return;
}
const wasFull = this.isFull();
this._messages.delete(message);
this.bytes -= message.length;
if (wasFull && !this.isFull()) {
process.nextTick(() => this.emit('free'));
} else if (this._pending.includes(message)) {
const index = this._pending.indexOf(message);
this._pending.splice(index, 1);
} else if (this.pending > 0) {
this._dispense(this._pending.shift()!);
}
if (this.size === 0 && this._isLeasing) {
this._cancelExtension();
}
}
/**
* Sets options for the LeaseManager.
*
* @param {FlowControlOptions} [options] The options.
*
* @throws {RangeError} If both maxExtension and maxExtensionMinutes are set.
*
* @private
*/
setOptions(options: FlowControlOptions): void {
// Convert the old deprecated maxExtension to avoid breaking clients,
// but allow only one.
if (
options.maxExtension !== undefined &&
options.maxExtensionMinutes !== undefined
) {
throw new RangeError(
'Only one of "maxExtension" or "maxExtensionMinutes" may be set for subscriber lease management options'
);
}
if (
options.maxExtension !== undefined &&
options.maxExtensionMinutes === undefined
) {
options.maxExtensionMinutes = options.maxExtension / 60;
delete options.maxExtension;
}
const defaults: FlowControlOptions = {
allowExcessMessages: true,
maxBytes: defaultOptions.subscription.maxOutstandingBytes,
maxExtensionMinutes: defaultOptions.subscription.maxExtensionMinutes,
maxMessages: defaultOptions.subscription.maxOutstandingMessages,
};
this._options = Object.assign(defaults, options);
}
/**
* Stops extending message deadlines.
*
* @private
*/
private _cancelExtension(): void {
this._isLeasing = false;
if (this._timer) {
clearTimeout(this._timer);
delete this._timer;
}
}
/**
* Emits the message. Emitting messages is very slow, so to avoid it acting
* as a bottleneck, we're wrapping it in nextTick.
*
* @private
*
* @fires Subscriber#message
*
* @param {Message} message The message to emit.
*/
private _dispense(message: Message): void {
if (this._subscriber.isOpen) {
process.nextTick(() => this._subscriber.emit('message', message));
}
}
/**
* Loops through inventory and extends the deadlines for any messages that
* have not hit the max extension option.
*
* @private
*/
private _extendDeadlines(): void {
const deadline = this._subscriber.ackDeadline;
for (const message of this._messages) {
// Lifespan here is in minutes.
const lifespan = (Date.now() - message.received) / (60 * 1000);
if (lifespan < this._options.maxExtensionMinutes!) {
if (this._subscriber.isExactlyOnceDelivery) {
message.modAckWithResponse(deadline).catch(e => {
// In the case of a permanent failure (temporary failures are retried),
// we need to stop trying to lease-manage the message.
message.ackFailed(e as AckError);
this.remove(message);
});
} else {
message.modAck(deadline);
}
} else {
this.remove(message);
}
}
if (this._isLeasing) {
this._scheduleExtension();
}
}
/**
* Creates a timeout(ms) that should allow us to extend any message deadlines
* before they would be redelivered.
*
* @private
*
* @returns {number}
*/
private _getNextExtensionTimeoutMs(): number {
const jitter = Math.random();
const deadline = this._subscriber.ackDeadline * 1000;
const latency = this._subscriber.modAckLatency;
return (deadline * 0.9 - latency) * jitter;
}
/**
* Schedules an deadline extension for all messages.
*
* @private
*/
private _scheduleExtension(): void {
const timeout = this._getNextExtensionTimeoutMs();
this._timer = setTimeout(() => this._extendDeadlines(), timeout);
}
}