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 | maxExtension?: number;
|
23 | maxMessages?: number;
|
24 | }
|
25 | /**
|
26 | * @typedef {object} FlowControlOptions
|
27 | * @property {boolean} [allowExcessMessages=true] PubSub delivers messages in
|
28 | * batches with no way to configure the batch size. Sometimes this can be
|
29 | * overwhelming if you only want to process a few messages at a time.
|
30 | * Setting this option to false will make the client manage any excess
|
31 | * messages until you're ready for them. This will prevent them from being
|
32 | * redelivered and make the maxMessages option behave more predictably.
|
33 | * @property {number} [maxBytes=104857600] The desired amount of memory to
|
34 | * allow message data to consume. (Default: 100MB) It's possible that this
|
35 | * value will be exceeded, since messages are received in batches.
|
36 | * @property {number} [maxExtension=60] The maximum duration (in seconds)
|
37 | * to extend the message deadline before redelivering.
|
38 | * @property {number} [maxMessages=1000] The desired number of messages to allow
|
39 | * in memory before pausing the message stream. Unless allowExcessMessages
|
40 | * is set to false, it is very likely that this value will be exceeded since
|
41 | * any given message batch could contain a greater number of messages than
|
42 | * the desired amount of messages.
|
43 | */
|
44 | /**
|
45 | * Manages a Subscribers inventory while auto-magically extending the message
|
46 | * deadlines.
|
47 | *
|
48 | * @private
|
49 | * @class
|
50 | *
|
51 | * @param {Subscriber} sub The subscriber to manage leases for.
|
52 | * @param {FlowControlOptions} options Flow control options.
|
53 | */
|
54 | export declare class LeaseManager extends EventEmitter {
|
55 | bytes: number;
|
56 | private _isLeasing;
|
57 | private _messages;
|
58 | private _options;
|
59 | private _pending;
|
60 | private _subscriber;
|
61 | private _timer?;
|
62 | constructor(sub: Subscriber, options?: {});
|
63 | /**
|
64 | * @type {number}
|
65 | *
|
66 | */
|
67 | get pending(): number;
|
68 | /**
|
69 | * @type {number}
|
70 | * @private
|
71 | */
|
72 | get size(): number;
|
73 | /**
|
74 | * Adds a message to the inventory, kicking off the deadline extender if it
|
75 | * isn't already running.
|
76 | *
|
77 | * @param {Message} message The message.
|
78 | * @private
|
79 | */
|
80 | add(message: Message): void;
|
81 | /**
|
82 | * Removes ALL messages from inventory.
|
83 | * @private
|
84 | */
|
85 | clear(): void;
|
86 | /**
|
87 | * Indicates if we're at or over capacity.
|
88 | *
|
89 | * @returns {boolean}
|
90 | * @private
|
91 | */
|
92 | isFull(): boolean;
|
93 | /**
|
94 | * Removes a message from the inventory. Stopping the deadline extender if no
|
95 | * messages are left over.
|
96 | *
|
97 | * @fires LeaseManager#free
|
98 | *
|
99 | * @param {Message} message The message to remove.
|
100 | * @private
|
101 | */
|
102 | remove(message: Message): void;
|
103 | /**
|
104 | * Sets options for the LeaseManager.
|
105 | *
|
106 | * @param {FlowControlOptions} [options] The options.
|
107 | * @private
|
108 | */
|
109 | setOptions(options: FlowControlOptions): void;
|
110 | /**
|
111 | * Stops extending message deadlines.
|
112 | *
|
113 | * @private
|
114 | */
|
115 | private _cancelExtension;
|
116 | /**
|
117 | * Emits the message. Emitting messages is very slow, so to avoid it acting
|
118 | * as a bottleneck, we're wrapping it in nextTick.
|
119 | *
|
120 | * @private
|
121 | *
|
122 | * @fires Subscriber#message
|
123 | *
|
124 | * @param {Message} message The message to emit.
|
125 | */
|
126 | private _dispense;
|
127 | /**
|
128 | * Loops through inventory and extends the deadlines for any messages that
|
129 | * have not hit the max extension option.
|
130 | *
|
131 | * @private
|
132 | */
|
133 | private _extendDeadlines;
|
134 | /**
|
135 | * Creates a timeout(ms) that should allow us to extend any message deadlines
|
136 | * before they would be redelivered.
|
137 | *
|
138 | * @private
|
139 | *
|
140 | * @returns {number}
|
141 | */
|
142 | private _getNextExtensionTimeoutMs;
|
143 | /**
|
144 | * Schedules an deadline extension for all messages.
|
145 | *
|
146 | * @private
|
147 | */
|
148 | private _scheduleExtension;
|
149 | }
|