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