UNPKG

9.54 kBTypeScriptView Raw
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/// <reference types="node" />
18import { PreciseDate } from '@google-cloud/precise-date';
19import { EventEmitter } from 'events';
20import { google } from '../protos/protos';
21import { FlowControlOptions } from './lease-manager';
22import { BatchOptions } from './message-queues';
23import { MessageStreamOptions } from './message-stream';
24import { Subscription } from './subscription';
25import { SubscriberClient } from './v1';
26import { Duration } from './temporal';
27export declare type PullResponse = google.pubsub.v1.IStreamingPullResponse;
28export declare type SubscriptionProperties = google.pubsub.v1.StreamingPullResponse.ISubscriptionProperties;
29/**
30 * Date object with nanosecond precision. Supports all standard Date arguments
31 * in addition to several custom types.
32 *
33 * @external PreciseDate
34 * @see {@link https://github.com/googleapis/nodejs-precise-date|PreciseDate}
35 */
36/**
37 * Message objects provide a simple interface for users to get message data and
38 * acknowledge the message.
39 *
40 * @example
41 * ```
42 * subscription.on('message', message => {
43 * // {
44 * // ackId: 'RUFeQBJMJAxESVMrQwsqWBFOBCEhPjA',
45 * // attributes: {key: 'value'},
46 * // data: Buffer.from('Hello, world!'),
47 * // id: '1551297743043',
48 * // orderingKey: 'ordering-key',
49 * // publishTime: new PreciseDate('2019-02-27T20:02:19.029534186Z'),
50 * // received: 1551297743043,
51 * // length: 13
52 * // }
53 * });
54 * ```
55 */
56export declare class Message {
57 ackId: string;
58 attributes: {
59 [key: string]: string;
60 };
61 data: Buffer;
62 deliveryAttempt: number;
63 id: string;
64 orderingKey?: string;
65 publishTime: PreciseDate;
66 received: number;
67 private _handled;
68 private _length;
69 private _subscriber;
70 /**
71 * @hideconstructor
72 *
73 * @param {Subscriber} sub The parent subscriber.
74 * @param {object} message The raw message response.
75 */
76 constructor(sub: Subscriber, { ackId, message, deliveryAttempt }: google.pubsub.v1.IReceivedMessage);
77 /**
78 * The length of the message data.
79 *
80 * @type {number}
81 */
82 get length(): number;
83 /**
84 * Acknowledges the message.
85 *
86 * @example
87 * ```
88 * subscription.on('message', message => {
89 * message.ack();
90 * });
91 * ```
92 */
93 ack(): void;
94 /**
95 * Modifies the ack deadline.
96 *
97 * @param {number} deadline The number of seconds to extend the deadline.
98 * @private
99 */
100 modAck(deadline: number): void;
101 /**
102 * Removes the message from our inventory and schedules it to be redelivered.
103 *
104 * @example
105 * ```
106 * subscription.on('message', message => {
107 * message.nack();
108 * });
109 * ```
110 */
111 nack(): void;
112}
113/**
114 * @typedef {object} SubscriberOptions
115 * @property {number} [ackDeadline=10] Acknowledge deadline in seconds. If left
116 * unset, the initial value will be 10 seconds, but it will evolve into the
117 * 99th percentile time it takes to acknowledge a message, subject to the
118 * limitations of minAckDeadline and maxAckDeadline. If ackDeadline is set
119 * by the user, then the min/max values will be set to match it. New code
120 * should prefer setting minAckDeadline and maxAckDeadline directly.
121 * @property {Duration} [minAckDeadline] The minimum time that ackDeadline should
122 * ever have, while it's under library control.
123 * @property {Duration} [maxAckDeadline] The maximum time that ackDeadline should
124 * ever have, while it's under library control.
125 * @property {BatchOptions} [batching] Request batching options.
126 * @property {FlowControlOptions} [flowControl] Flow control options.
127 * @property {boolean} [useLegacyFlowControl] Disables enforcing flow control
128 * settings at the Cloud PubSub server and uses the less accurate method
129 * of only enforcing flow control at the client side.
130 * @property {MessageStreamOptions} [streamingOptions] Streaming options.
131 */
132export interface SubscriberOptions {
133 /** @deprecated Use minAckDeadline and maxAckDeadline. */
134 ackDeadline?: number;
135 minAckDeadline?: Duration;
136 maxAckDeadline?: Duration;
137 batching?: BatchOptions;
138 flowControl?: FlowControlOptions;
139 useLegacyFlowControl?: boolean;
140 streamingOptions?: MessageStreamOptions;
141 enableOpenTelemetryTracing?: boolean;
142}
143/**
144 * Subscriber class is used to manage all message related functionality.
145 *
146 * @private
147 * @class
148 *
149 * @param {Subscription} subscription The corresponding subscription.
150 * @param {SubscriberOptions} options The subscriber options.
151 */
152export declare class Subscriber extends EventEmitter {
153 ackDeadline: number;
154 maxMessages: number;
155 maxBytes: number;
156 useLegacyFlowControl: boolean;
157 isOpen: boolean;
158 private _acks;
159 private _histogram;
160 private _inventory;
161 private _useOpentelemetry;
162 private _latencies;
163 private _modAcks;
164 private _name;
165 private _options;
166 private _stream;
167 private _subscription;
168 private _errorLog;
169 subscriptionProperties?: SubscriptionProperties;
170 constructor(subscription: Subscription, options?: {});
171 /**
172 * Update our ack extension time that will be used by the lease manager
173 * for sending modAcks.
174 *
175 * Should not be called from outside this class, except for unit tests.
176 *
177 * @param {number} [ackTimeSeconds] The number of seconds that the last
178 * ack took after the message was received. If this is undefined, then
179 * we won't update the histogram, but we will still recalculate the
180 * ackDeadline based on the situation.
181 *
182 * @private
183 */
184 updateAckDeadline(ackTimeSeconds?: number): void;
185 private getMinMaxDeadlines;
186 /**
187 * Returns true if an exactly once subscription has been detected.
188 *
189 * @private
190 */
191 get isExactlyOnce(): boolean;
192 /**
193 * Sets our subscription properties from incoming messages.
194 *
195 * @param {SubscriptionProperties} subscriptionProperties The new properties.
196 * @private
197 */
198 setSubscriptionProperties(subscriptionProperties: SubscriptionProperties): void;
199 /**
200 * The 99th percentile of request latencies.
201 *
202 * @type {number}
203 * @private
204 */
205 get modAckLatency(): number;
206 /**
207 * The full name of the Subscription.
208 *
209 * @type {string}
210 * @private
211 */
212 get name(): string;
213 /**
214 * Acknowledges the supplied message.
215 *
216 * @param {Message} message The message to acknowledge.
217 * @returns {Promise}
218 * @private
219 */
220 ack(message: Message): Promise<void>;
221 /**
222 * Closes the subscriber. The returned promise will resolve once any pending
223 * acks/modAcks are finished.
224 *
225 * @returns {Promise}
226 * @private
227 */
228 close(): Promise<void>;
229 /**
230 * Gets the subscriber client instance.
231 *
232 * @returns {Promise<object>}
233 * @private
234 */
235 getClient(): Promise<SubscriberClient>;
236 /**
237 * Modifies the acknowledge deadline for the provided message.
238 *
239 * @param {Message} message The message to modify.
240 * @param {number} deadline The deadline.
241 * @returns {Promise}
242 * @private
243 */
244 modAck(message: Message, deadline: number): Promise<void>;
245 /**
246 * Modfies the acknowledge deadline for the provided message and then removes
247 * it from our inventory.
248 *
249 * @param {Message} message The message.
250 * @return {Promise}
251 * @private
252 */
253 nack(message: Message): Promise<void>;
254 /**
255 * Starts pulling messages.
256 * @private
257 */
258 open(): void;
259 /**
260 * Sets subscriber options.
261 *
262 * @param {SubscriberOptions} options The options.
263 * @private
264 */
265 setOptions(options: SubscriberOptions): void;
266 /**
267 * Constructs an OpenTelemetry span from the incoming message.
268 *
269 * @param {Message} message One of the received messages
270 * @private
271 */
272 private _constructSpan;
273 /**
274 * Callback to be invoked when a new message is available.
275 *
276 * New messages will be added to the subscribers inventory, which in turn will
277 * automatically extend the messages ack deadline until either:
278 * a. the user acks/nacks it
279 * b. the maxExtension option is hit
280 *
281 * If the message puts us at/over capacity, then we'll pause our message
282 * stream until we've freed up some inventory space.
283 *
284 * New messages must immediately issue a ModifyAckDeadline request
285 * (aka receipt) to confirm with the backend that we did infact receive the
286 * message and its ok to start ticking down on the deadline.
287 *
288 * @private
289 */
290 private _onData;
291 /**
292 * Returns a promise that will resolve once all pending requests have settled.
293 *
294 * @private
295 *
296 * @returns {Promise}
297 */
298 private _waitForFlush;
299}