-
Notifications
You must be signed in to change notification settings - Fork 227
/
index.ts
363 lines (321 loc) · 10.7 KB
/
index.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*!
* Copyright 2019 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 * as extend from 'extend';
import {CallOptions} from 'google-gax';
import {isSpanContextValid, Span} from '@opentelemetry/api';
import {BatchPublishOptions} from './message-batch';
import {Queue, OrderedQueue} from './message-queues';
import {Topic} from '../topic';
import {RequestCallback, EmptyCallback} from '../pubsub';
import {defaultOptions} from '../default-options';
import * as tracing from '../telemetry-tracing';
import {FlowControl, FlowControlOptions} from './flow-control';
import {promisifySome} from '../util';
import {PubsubMessage, Attributes} from './pubsub-message';
export {PubsubMessage, Attributes} from './pubsub-message';
export type PublishCallback = RequestCallback<string>;
export interface PublishOptions {
batching?: BatchPublishOptions;
flowControlOptions?: FlowControlOptions;
gaxOpts?: CallOptions;
messageOrdering?: boolean;
/** @deprecated Unset and use context propagation. */
enableOpenTelemetryTracing?: boolean;
}
/**
* @typedef PublishOptions
* @property {BatchPublishOptions} [batching] The maximum number of bytes to
* buffer before sending a payload.
* @property {FlowControlOptions} [publisherFlowControl] Publisher-side flow
* control settings. If this is undefined, Ignore will be the assumed action.
* @property {object} [gaxOpts] Request configuration options, outlined
* {@link https://googleapis.github.io/gax-nodejs/interfaces/CallOptions.html|here.}
* @property {boolean} [messageOrdering] If true, messages published with the
* same order key in Message will be delivered to the subscribers in the order in which they
* are received by the Pub/Sub system. Otherwise, they may be delivered in
* any order.
*/
export const BATCH_LIMITS: BatchPublishOptions = {
maxBytes: Math.pow(1024, 2) * 9,
maxMessages: 1000,
};
export const flowControlDefaults: FlowControlOptions = {
maxOutstandingBytes: undefined,
maxOutstandingMessages: undefined,
};
/**
* A Publisher object allows you to publish messages to a specific topic.
*
* @private
* @class
*
* @see [Topics: publish API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/publish}
*
* @param {Topic} topic The topic associated with this publisher.
* @param {PublishOptions} [options] Configuration object.
*/
export class Publisher {
topic: Topic;
settings!: PublishOptions;
queue: Queue;
orderedQueues: Map<string, OrderedQueue>;
flowControl: FlowControl;
constructor(topic: Topic, options?: PublishOptions) {
this.flowControl = new FlowControl(
options?.flowControlOptions || flowControlDefaults
);
this.setOptions(options);
this.topic = topic;
this.queue = new Queue(this);
this.orderedQueues = new Map();
}
/**
* Immediately sends all remaining queued data. This is mostly useful
* if you are planning to call close() on the PubSub object that holds
* the server connections.
*
* @private
*
* @param {EmptyCallback} [callback] Callback function.
* @returns {Promise<EmptyResponse>}
*/
flush(): Promise<void>;
flush(callback: EmptyCallback): void;
flush(callback?: EmptyCallback): Promise<void> | void {
const definedCallback = callback ? callback : () => {};
const toDrain = [this.queue, ...Array.from(this.orderedQueues.values())];
const allDrains = Promise.all(
toDrain.map(
q =>
new Promise<void>(resolve => {
const flushResolver = () => {
resolve();
// flush() may be called more than once, so remove these
// event listeners after we've completed flush().
q.removeListener('drain', flushResolver);
};
q.on('drain', flushResolver);
})
)
);
const allPublishes = Promise.all(toDrain.map(q => q.publishDrain()));
allPublishes
.then(() => allDrains)
.then(() => {
definedCallback(null);
})
.catch(definedCallback);
}
/**
* Publish the provided message.
*
* @deprecated use {@link Publisher#publishMessage} instead.
*
* @private
* @see Publisher#publishMessage
*
* @param {buffer} data The message data. This must come in the form of a
* Buffer object.
* @param {object.<string, string>} [attributes] Attributes for this message.
* @param {PublishCallback} [callback] Callback function.
* @returns {Promise<PublishResponse>}
*/
publish(data: Buffer, attributes?: Attributes): Promise<string>;
publish(data: Buffer, callback: PublishCallback): void;
publish(
data: Buffer,
attributes: Attributes,
callback: PublishCallback
): void;
publish(
data: Buffer,
attrsOrCb?: Attributes | PublishCallback,
callback?: PublishCallback
): Promise<string> | void {
const attributes = typeof attrsOrCb === 'object' ? attrsOrCb : {};
callback = typeof attrsOrCb === 'function' ? attrsOrCb : callback;
return this.publishMessage({data, attributes}, callback!);
}
/**
* Publish the provided message.
*
* @private
*
* @throws {TypeError} If data is not a Buffer object.
* @throws {TypeError} If any value in `attributes` object is not a string.
*
* @param {PubsubMessage} [message] Options for this message.
* @param {PublishCallback} [callback] Callback function.
*/
publishMessage(message: PubsubMessage): Promise<string>;
publishMessage(message: PubsubMessage, callback: PublishCallback): void;
publishMessage(
message: PubsubMessage,
callback?: PublishCallback
): Promise<string> | void {
const {data, attributes = {}} = message;
// We must have at least one of:
// - `data` as a Buffer
// - `attributes` that are not empty
if (data && !(data instanceof Buffer)) {
throw new TypeError('Data must be in the form of a Buffer.');
}
const keys = Object.keys(attributes!);
if (!data && keys.length === 0) {
throw new TypeError(
'If data is undefined, at least one attribute must be present.'
);
}
for (const key of keys) {
const value = attributes![key];
if (typeof value !== 'string') {
throw new TypeError(`All attributes must be in the form of a string.
\nInvalid value of type "${typeof value}" provided for "${key}".`);
}
}
// Ensure that there's a parent span for subsequent publishes
// to hang off of.
this.getParentSpan(message, 'Publisher.publishMessage');
if (!message.orderingKey) {
this.queue.add(message, callback!);
} else {
const key = message.orderingKey;
if (!this.orderedQueues.has(key)) {
const queue = new OrderedQueue(this, key);
this.orderedQueues.set(key, queue);
queue.once('drain', () => this.orderedQueues.delete(key));
}
const queue = this.orderedQueues.get(key)!;
queue.add(message, callback!);
}
}
/**
* Indicates to the publisher that it is safe to continue publishing for the
* supplied ordering key.
*
* @private
*
* @param {string} key The ordering key to continue publishing for.
*/
resumePublishing(key: string) {
const queue = this.orderedQueues.get(key);
if (queue) {
queue.resumePublishing();
}
}
/**
* Returns the set of default options used for {@link Publisher}. The
* returned value is a copy, and editing it will have no effect elsehwere.
*
* This is a non-static method to make it easier to access/stub.
*
* @private
*
* @returns {PublishOptions}
*/
getOptionDefaults(): PublishOptions {
// Return a unique copy to avoid shenanigans.
const defaults: PublishOptions = {
batching: {
maxBytes: defaultOptions.publish.maxOutstandingBytes,
maxMessages: defaultOptions.publish.maxOutstandingMessages,
maxMilliseconds: defaultOptions.publish.maxDelayMillis,
},
messageOrdering: false,
gaxOpts: {
isBundling: false,
},
enableOpenTelemetryTracing: false,
flowControlOptions: Object.assign(
{},
flowControlDefaults
) as FlowControlOptions,
};
return defaults;
}
/**
* Sets the Publisher options.
*
* @private
*
* @param {PublishOptions} options The publisher options.
*/
setOptions(options = {} as PublishOptions): void {
const defaults = this.getOptionDefaults();
const {
batching,
gaxOpts,
messageOrdering,
enableOpenTelemetryTracing,
flowControlOptions,
} = extend(true, defaults, options);
this.settings = {
batching: {
maxBytes: Math.min(batching!.maxBytes!, BATCH_LIMITS.maxBytes!),
maxMessages: Math.min(
batching!.maxMessages!,
BATCH_LIMITS.maxMessages!
),
maxMilliseconds: batching!.maxMilliseconds,
},
gaxOpts,
messageOrdering,
enableOpenTelemetryTracing,
flowControlOptions,
};
// We also need to let all of our queues know that they need to update their options.
// Note that these might be undefined, because setOptions() is called in the constructor.
if (this.queue) {
this.queue.updateOptions();
}
if (this.orderedQueues) {
for (const q of this.orderedQueues.values()) {
q.updateOptions();
}
}
// This will always be filled in by our defaults if nothing else.
this.flowControl.setOptions(this.settings.flowControlOptions!);
}
/**
* Finds or constructs an telemetry publish/parent span for a message.
*
* @private
*
* @param {PubsubMessage} message The message to create a span for
*/
getParentSpan(message: PubsubMessage, caller: string): Span | undefined {
const enabled = tracing.isEnabled(this.settings);
if (!enabled) {
return undefined;
}
if (message.parentSpan) {
return message.parentSpan;
}
const span = tracing.PubsubSpans.createPublisherSpan(
message,
this.topic.name,
caller
);
// If the span's context is valid we should inject the propagation trace context.
if (span && isSpanContextValid(span.spanContext())) {
tracing.injectSpan(span, message, enabled);
}
return span;
}
}
promisifySome(Publisher, Publisher.prototype, ['flush', 'publishMessage'], {
singular: true,
});