1 | /*!
|
2 | * Copyright 2018 Google Inc. All Rights Reserved.
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | */
|
16 | /// <reference types="node" />
|
17 | import { EventEmitter } from 'events';
|
18 | import { Message, Subscriber } from './subscriber';
|
19 | export interface FlowControlOptions {
|
20 | allowExcessMessages?: boolean;
|
21 | maxBytes?: number;
|
22 | maxMessages?: number;
|
23 | maxExtensionMinutes?: number;
|
24 | /** @deprecated Use maxExtensionMinutes. */
|
25 | maxExtension?: number;
|
26 | }
|
27 | /**
|
28 | * @typedef {object} FlowControlOptions
|
29 | * @property {boolean} [allowExcessMessages=true] PubSub delivers messages in
|
30 | * batches with no way to configure the batch size. Sometimes this can be
|
31 | * overwhelming if you only want to process a few messages at a time.
|
32 | * Setting this option to false will make the client manage any excess
|
33 | * messages until you're ready for them. This will prevent them from being
|
34 | * redelivered and make the maxMessages option behave more predictably.
|
35 | * @property {number} [maxBytes=104857600] The desired amount of memory to
|
36 | * allow message data to consume. (Default: 100MB) It's possible that this
|
37 | * value will be exceeded, since messages are received in batches.
|
38 | * @property {number} [maxExtensionMinutes=60] The maximum duration (in minutes)
|
39 | * to extend the message deadline before redelivering.
|
40 | * @property {number} [maxMessages=1000] The desired number of messages to allow
|
41 | * in memory before pausing the message stream. Unless allowExcessMessages
|
42 | * is set to false, it is very likely that this value will be exceeded since
|
43 | * any given message batch could contain a greater number of messages than
|
44 | * the desired amount of messages.
|
45 | */
|
46 | /**
|
47 | * Manages a Subscribers inventory while auto-magically extending the message
|
48 | * deadlines.
|
49 | *
|
50 | * @private
|
51 | * @class
|
52 | *
|
53 | * @param {Subscriber} sub The subscriber to manage leases for.
|
54 | * @param {FlowControlOptions} options Flow control options.
|
55 | */
|
56 | export declare class LeaseManager extends EventEmitter {
|
57 | bytes: number;
|
58 | private _isLeasing;
|
59 | private _messages;
|
60 | private _options;
|
61 | private _pending;
|
62 | private _subscriber;
|
63 | private _timer?;
|
64 | constructor(sub: Subscriber, options?: {});
|
65 | /**
|
66 | * @type {number}
|
67 | *
|
68 | */
|
69 | get pending(): number;
|
70 | /**
|
71 | * @type {number}
|
72 | * @private
|
73 | */
|
74 | get size(): number;
|
75 | /**
|
76 | * Adds a message to the inventory, kicking off the deadline extender if it
|
77 | * isn't already running.
|
78 | *
|
79 | * @param {Message} message The message.
|
80 | * @private
|
81 | */
|
82 | add(message: Message): void;
|
83 | /**
|
84 | * Removes ALL messages from inventory.
|
85 | * @private
|
86 | */
|
87 | clear(): void;
|
88 | /**
|
89 | * Indicates if we're at or over capacity.
|
90 | *
|
91 | * @returns {boolean}
|
92 | * @private
|
93 | */
|
94 | isFull(): boolean;
|
95 | /**
|
96 | * Removes a message from the inventory. Stopping the deadline extender if no
|
97 | * messages are left over.
|
98 | *
|
99 | * @fires LeaseManager#free
|
100 | *
|
101 | * @param {Message} message The message to remove.
|
102 | * @private
|
103 | */
|
104 | remove(message: Message): void;
|
105 | /**
|
106 | * Sets options for the LeaseManager.
|
107 | *
|
108 | * @param {FlowControlOptions} [options] The options.
|
109 | *
|
110 | * @throws {RangeError} If both maxExtension and maxExtensionMinutes are set.
|
111 | *
|
112 | * @private
|
113 | */
|
114 | setOptions(options: FlowControlOptions): void;
|
115 | /**
|
116 | * Stops extending message deadlines.
|
117 | *
|
118 | * @private
|
119 | */
|
120 | private _cancelExtension;
|
121 | /**
|
122 | * Emits the message. Emitting messages is very slow, so to avoid it acting
|
123 | * as a bottleneck, we're wrapping it in nextTick.
|
124 | *
|
125 | * @private
|
126 | *
|
127 | * @fires Subscriber#message
|
128 | *
|
129 | * @param {Message} message The message to emit.
|
130 | */
|
131 | private _dispense;
|
132 | /**
|
133 | * Loops through inventory and extends the deadlines for any messages that
|
134 | * have not hit the max extension option.
|
135 | *
|
136 | * @private
|
137 | */
|
138 | private _extendDeadlines;
|
139 | /**
|
140 | * Creates a timeout(ms) that should allow us to extend any message deadlines
|
141 | * before they would be redelivered.
|
142 | *
|
143 | * @private
|
144 | *
|
145 | * @returns {number}
|
146 | */
|
147 | private _getNextExtensionTimeoutMs;
|
148 | /**
|
149 | * Schedules an deadline extension for all messages.
|
150 | *
|
151 | * @private
|
152 | */
|
153 | private _scheduleExtension;
|
154 | }
|