@google-cloud/pubsub 2.17.0 → 2.18.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/README.md +1 -0
  3. package/build/protos/protos.d.ts +2 -1
  4. package/build/protos/protos.js +7 -0
  5. package/build/protos/protos.json +14 -1
  6. package/build/src/iam.d.ts +180 -0
  7. package/build/src/iam.js +11 -176
  8. package/build/src/iam.js.map +1 -1
  9. package/build/src/index.d.ts +13 -4
  10. package/build/src/index.js +12 -4
  11. package/build/src/index.js.map +1 -1
  12. package/build/src/lease-manager.d.ts +7 -2
  13. package/build/src/lease-manager.js +19 -4
  14. package/build/src/lease-manager.js.map +1 -1
  15. package/build/src/publisher/flow-control.d.ts +90 -0
  16. package/build/src/publisher/flow-control.js +145 -0
  17. package/build/src/publisher/flow-control.js.map +1 -0
  18. package/build/src/publisher/flow-publisher.d.ts +95 -0
  19. package/build/src/publisher/flow-publisher.js +133 -0
  20. package/build/src/publisher/flow-publisher.js.map +1 -0
  21. package/build/src/publisher/index.d.ts +33 -5
  22. package/build/src/publisher/index.js +16 -39
  23. package/build/src/publisher/index.js.map +1 -1
  24. package/build/src/publisher/message-batch.d.ts +1 -1
  25. package/build/src/publisher/message-batch.js +4 -3
  26. package/build/src/publisher/message-batch.js.map +1 -1
  27. package/build/src/publisher/message-queues.js.map +1 -1
  28. package/build/src/publisher/pubsub-message.d.ts +52 -0
  29. package/build/src/publisher/pubsub-message.js +56 -0
  30. package/build/src/publisher/pubsub-message.js.map +1 -0
  31. package/build/src/pubsub.d.ts +354 -4
  32. package/build/src/pubsub.js +28 -322
  33. package/build/src/pubsub.js.map +1 -1
  34. package/build/src/schema.d.ts +9 -4
  35. package/build/src/schema.js +9 -4
  36. package/build/src/schema.js.map +1 -1
  37. package/build/src/snapshot.d.ts +87 -0
  38. package/build/src/snapshot.js +7 -83
  39. package/build/src/snapshot.js.map +1 -1
  40. package/build/src/subscriber.d.ts +6 -0
  41. package/build/src/subscriber.js +6 -0
  42. package/build/src/subscriber.js.map +1 -1
  43. package/build/src/subscription.d.ts +439 -9
  44. package/build/src/subscription.js +38 -404
  45. package/build/src/subscription.js.map +1 -1
  46. package/build/src/topic.d.ts +481 -1
  47. package/build/src/topic.js +51 -430
  48. package/build/src/topic.js.map +1 -1
  49. package/build/src/util.d.ts +2 -1
  50. package/build/src/util.js +2 -2
  51. package/build/src/util.js.map +1 -1
  52. package/build/src/v1/publisher_client.d.ts +246 -15
  53. package/build/src/v1/publisher_client.js +18 -264
  54. package/build/src/v1/publisher_client.js.map +1 -1
  55. package/build/src/v1/publisher_client_config.json +1 -1
  56. package/build/src/v1/schema_service_client.d.ts +151 -5
  57. package/build/src/v1/schema_service_client.js +6 -157
  58. package/build/src/v1/schema_service_client.js.map +1 -1
  59. package/build/src/v1/subscriber_client.d.ts +536 -16
  60. package/build/src/v1/subscriber_client.js +14 -552
  61. package/build/src/v1/subscriber_client.js.map +1 -1
  62. package/package.json +3 -3
@@ -19,8 +19,10 @@ import { Message, Subscriber } from './subscriber';
19
19
  export interface FlowControlOptions {
20
20
  allowExcessMessages?: boolean;
21
21
  maxBytes?: number;
22
- maxExtension?: number;
23
22
  maxMessages?: number;
23
+ maxExtensionMinutes?: number;
24
+ /** @deprecated Use maxExtensionMinutes. */
25
+ maxExtension?: number;
24
26
  }
25
27
  /**
26
28
  * @typedef {object} FlowControlOptions
@@ -33,7 +35,7 @@ export interface FlowControlOptions {
33
35
  * @property {number} [maxBytes=104857600] The desired amount of memory to
34
36
  * allow message data to consume. (Default: 100MB) It's possible that this
35
37
  * value will be exceeded, since messages are received in batches.
36
- * @property {number} [maxExtension=60] The maximum duration (in seconds)
38
+ * @property {number} [maxExtensionMinutes=60] The maximum duration (in minutes)
37
39
  * to extend the message deadline before redelivering.
38
40
  * @property {number} [maxMessages=1000] The desired number of messages to allow
39
41
  * in memory before pausing the message stream. Unless allowExcessMessages
@@ -104,6 +106,9 @@ export declare class LeaseManager extends EventEmitter {
104
106
  * Sets options for the LeaseManager.
105
107
  *
106
108
  * @param {FlowControlOptions} [options] The options.
109
+ *
110
+ * @throws {RangeError} If both maxExtension and maxExtensionMinutes are set.
111
+ *
107
112
  * @private
108
113
  */
109
114
  setOptions(options: FlowControlOptions): void;
@@ -29,7 +29,7 @@ const default_options_1 = require("./default-options");
29
29
  * @property {number} [maxBytes=104857600] The desired amount of memory to
30
30
  * allow message data to consume. (Default: 100MB) It's possible that this
31
31
  * value will be exceeded, since messages are received in batches.
32
- * @property {number} [maxExtension=60] The maximum duration (in seconds)
32
+ * @property {number} [maxExtensionMinutes=60] The maximum duration (in minutes)
33
33
  * to extend the message deadline before redelivering.
34
34
  * @property {number} [maxMessages=1000] The desired number of messages to allow
35
35
  * in memory before pausing the message stream. Unless allowExcessMessages
@@ -155,13 +155,27 @@ class LeaseManager extends events_1.EventEmitter {
155
155
  * Sets options for the LeaseManager.
156
156
  *
157
157
  * @param {FlowControlOptions} [options] The options.
158
+ *
159
+ * @throws {RangeError} If both maxExtension and maxExtensionMinutes are set.
160
+ *
158
161
  * @private
159
162
  */
160
163
  setOptions(options) {
164
+ // Convert the old deprecated maxExtension to avoid breaking clients,
165
+ // but allow only one.
166
+ if (options.maxExtension !== undefined &&
167
+ options.maxExtensionMinutes !== undefined) {
168
+ throw new RangeError('Only one of "maxExtension" or "maxExtensionMinutes" may be set for subscriber lease management options');
169
+ }
170
+ if (options.maxExtension !== undefined &&
171
+ options.maxExtensionMinutes === undefined) {
172
+ options.maxExtensionMinutes = options.maxExtension / 60;
173
+ delete options.maxExtension;
174
+ }
161
175
  const defaults = {
162
176
  allowExcessMessages: true,
163
177
  maxBytes: default_options_1.defaultOptions.subscription.maxOutstandingBytes,
164
- maxExtension: default_options_1.defaultOptions.subscription.maxExtensionMinutes,
178
+ maxExtensionMinutes: default_options_1.defaultOptions.subscription.maxExtensionMinutes,
165
179
  maxMessages: default_options_1.defaultOptions.subscription.maxOutstandingMessages,
166
180
  };
167
181
  this._options = Object.assign(defaults, options);
@@ -202,8 +216,9 @@ class LeaseManager extends events_1.EventEmitter {
202
216
  _extendDeadlines() {
203
217
  const deadline = this._subscriber.ackDeadline;
204
218
  for (const message of this._messages) {
205
- const lifespan = (Date.now() - message.received) / 1000;
206
- if (lifespan < this._options.maxExtension) {
219
+ // Lifespan here is in minutes.
220
+ const lifespan = (Date.now() - message.received) / (60 * 1000);
221
+ if (lifespan < this._options.maxExtensionMinutes) {
207
222
  message.modAck(deadline);
208
223
  }
209
224
  else {
@@ -1 +1 @@
1
- {"version":3,"file":"lease-manager.js","sourceRoot":"","sources":["../../src/lease-manager.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,mCAAoC;AAEpC,uDAAiD;AASjD;;;;;;;;;;;;;;;;;;GAkBG;AACH;;;;;;;;;GASG;AACH,MAAa,YAAa,SAAQ,qBAAY;IAQ5C,YAAY,GAAe,EAAE,OAAO,GAAG,EAAE;QACvC,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;QACf,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;QACxB,IAAI,CAAC,SAAS,GAAG,IAAI,GAAG,EAAE,CAAC;QAC3B,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;QACnB,IAAI,CAAC,WAAW,GAAG,GAAG,CAAC;QAEvB,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IAC3B,CAAC;IACD;;;OAGG;IACH,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;IAC9B,CAAC;IACD;;;OAGG;IACH,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;IAC7B,CAAC;IACD;;;;;;OAMG;IACH,GAAG,CAAC,OAAgB;QAClB,MAAM,EAAC,mBAAmB,EAAC,GAAG,IAAI,CAAC,QAAQ,CAAC;QAC5C,MAAM,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AAO,CAAC,CAAC;aACtB;SACF;QAED,IAAI,IAAI,CAAC,UAAU,EAAE;YACnB,IAAI,CAAC,kBAAkB,EAAE,CAAC;SAC3B;IACH,CAAC;IACD;;;;;;;OAOG;IACK,0BAA0B;QAChC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;QAC7B,MAAM,QAAQ,GAAG,IAAI,CAAC,WAAW,CAAC,WAAW,GAAG,IAAI,CAAC;QACrD,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,aAAa,CAAC;QAE/C,OAAO,CAAC,QAAQ,GAAG,GAAG,GAAG,OAAO,CAAC,GAAG,MAAM,CAAC;IAC7C,CAAC;IACD;;;;OAIG;IACK,kBAAkB;QACxB,MAAM,OAAO,GAAG,IAAI,CAAC,0BAA0B,EAAE,CAAC;QAClD,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,gBAAgB,EAAE,EAAE,OAAO,CAAC,CAAC;IACnE,CAAC;CACF;AA1OD,oCA0OC"}
@@ -0,0 +1,90 @@
1
+ /*!
2
+ * Copyright 2021 Google LLC
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
+ /**
17
+ * @typedef FlowControlOptions
18
+ * @property {number} [maxOutstandingMessages] The maximum number of messages to
19
+ * buffer before publisher flow control kicks in.
20
+ * @property {number} [maxOutstandingBytes] The maximum number of bytes to buffer
21
+ * before publisher flow control kicks in.
22
+ */
23
+ export interface FlowControlOptions {
24
+ maxOutstandingMessages?: number;
25
+ maxOutstandingBytes?: number;
26
+ }
27
+ /**
28
+ * Manages flow control handling for max bytes and messages.
29
+ *
30
+ * Do not use this class externally, it may change without warning.
31
+ * @private
32
+ *
33
+ */
34
+ export declare class FlowControl {
35
+ options: FlowControlOptions;
36
+ private bytes;
37
+ private messages;
38
+ private requests;
39
+ constructor(options: FlowControlOptions);
40
+ /**
41
+ * Update our options after the fact.
42
+ *
43
+ * Do not use externally, it may change without warning.
44
+ * @private
45
+ */
46
+ setOptions(options: FlowControlOptions): void;
47
+ /**
48
+ * @returns {number} The number of bytes that are queued up.
49
+ */
50
+ get currentByteCount(): number;
51
+ /**
52
+ * @returns {number} The number of messages that are queued up.
53
+ */
54
+ get currentMessageCount(): number;
55
+ /**
56
+ * Adds the specified number of bytes or messages to our count. We'll
57
+ * assume that this is end running around our queueing mechanisms.
58
+ *
59
+ * @param {number} bytes The number of bytes to add to the count.
60
+ * @param {number} messages The number of messages to add to the count.
61
+ */
62
+ addToCount(bytes: number, messages: number): void;
63
+ /**
64
+ * Attempts to queue the specified number of bytes and messages. If
65
+ * there are too many things in the publisher flow control queue
66
+ * already, we will defer and come back to it.
67
+ *
68
+ * Do not use externally, it may change without warning.
69
+ * @private
70
+ */
71
+ willSend(bytes: number, messages: number): Promise<void>;
72
+ /**
73
+ * Removes the specified number of bytes and messages from our queued
74
+ * counts, after a deferred request was released. If there is enough
75
+ * space.
76
+ *
77
+ * Do not use externally, it may change without warning.
78
+ * @private
79
+ */
80
+ sent(bytes: number, messages: number): void;
81
+ private exceeded;
82
+ /**
83
+ * Returns true if adding the specified number of bytes or messages
84
+ * would exceed limits imposed by configuration.
85
+ *
86
+ * Do not use externally, it may change without warning.
87
+ * @private
88
+ */
89
+ wouldExceed(bytes: number, messages: number): boolean;
90
+ }
@@ -0,0 +1,145 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright 2021 Google LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.FlowControl = void 0;
19
+ const defer = require("p-defer");
20
+ /**
21
+ * Manages flow control handling for max bytes and messages.
22
+ *
23
+ * Do not use this class externally, it may change without warning.
24
+ * @private
25
+ *
26
+ */
27
+ class FlowControl {
28
+ constructor(options) {
29
+ this.options = {};
30
+ this.setOptions(options);
31
+ this.bytes = this.messages = 0;
32
+ this.requests = [];
33
+ }
34
+ /**
35
+ * Update our options after the fact.
36
+ *
37
+ * Do not use externally, it may change without warning.
38
+ * @private
39
+ */
40
+ setOptions(options) {
41
+ this.options = options;
42
+ if (this.options.maxOutstandingBytes === 0 ||
43
+ this.options.maxOutstandingMessages === 0) {
44
+ // Undefined is okay, but if either is zero, no publishes ever happen.
45
+ throw new Error('When using publisher flow control, maxOutstandingBytes and maxOutstandingMessages must not be zero');
46
+ }
47
+ }
48
+ /**
49
+ * @returns {number} The number of bytes that are queued up.
50
+ */
51
+ get currentByteCount() {
52
+ return this.bytes;
53
+ }
54
+ /**
55
+ * @returns {number} The number of messages that are queued up.
56
+ */
57
+ get currentMessageCount() {
58
+ return this.messages;
59
+ }
60
+ /**
61
+ * Adds the specified number of bytes or messages to our count. We'll
62
+ * assume that this is end running around our queueing mechanisms.
63
+ *
64
+ * @param {number} bytes The number of bytes to add to the count.
65
+ * @param {number} messages The number of messages to add to the count.
66
+ */
67
+ addToCount(bytes, messages) {
68
+ this.bytes += bytes;
69
+ this.messages += messages;
70
+ }
71
+ /**
72
+ * Attempts to queue the specified number of bytes and messages. If
73
+ * there are too many things in the publisher flow control queue
74
+ * already, we will defer and come back to it.
75
+ *
76
+ * Do not use externally, it may change without warning.
77
+ * @private
78
+ */
79
+ async willSend(bytes, messages) {
80
+ // Add this to our queue size.
81
+ this.bytes += bytes;
82
+ this.messages += messages;
83
+ // If this request won't fit, we have to put it in the queue.
84
+ if (this.exceeded()) {
85
+ const promise = defer();
86
+ this.requests.push({
87
+ promise: promise.promise,
88
+ resolve: promise.resolve,
89
+ reject: promise.reject,
90
+ bytes,
91
+ messageCount: messages,
92
+ });
93
+ // This will pass through when someone else's this.sent() completes.
94
+ await promise.promise;
95
+ }
96
+ }
97
+ /**
98
+ * Removes the specified number of bytes and messages from our queued
99
+ * counts, after a deferred request was released. If there is enough
100
+ * space.
101
+ *
102
+ * Do not use externally, it may change without warning.
103
+ * @private
104
+ */
105
+ sent(bytes, messages) {
106
+ this.bytes -= bytes;
107
+ this.messages -= messages;
108
+ // This shouldn't happen, but just be sure.
109
+ if (this.bytes < 0)
110
+ this.bytes = 0;
111
+ if (this.messages < 0)
112
+ this.messages = 0;
113
+ // Let things waiting on willSend() have a go, if there's space.
114
+ if (this.requests.length > 0 && !this.exceeded()) {
115
+ const next = this.requests.shift();
116
+ next.resolve();
117
+ }
118
+ }
119
+ // Just uses wouldExceed() to see if we've already exceeded the limits.
120
+ exceeded() {
121
+ return this.wouldExceed(0, 0);
122
+ }
123
+ /**
124
+ * Returns true if adding the specified number of bytes or messages
125
+ * would exceed limits imposed by configuration.
126
+ *
127
+ * Do not use externally, it may change without warning.
128
+ * @private
129
+ */
130
+ wouldExceed(bytes, messages) {
131
+ const totalBytes = this.bytes + bytes;
132
+ const totalMessages = this.messages + messages;
133
+ if (this.options.maxOutstandingBytes !== undefined &&
134
+ totalBytes > this.options.maxOutstandingBytes) {
135
+ return true;
136
+ }
137
+ if (this.options.maxOutstandingMessages !== undefined &&
138
+ totalMessages > this.options.maxOutstandingMessages) {
139
+ return true;
140
+ }
141
+ return false;
142
+ }
143
+ }
144
+ exports.FlowControl = FlowControl;
145
+ //# sourceMappingURL=flow-control.js.map
@@ -0,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OAAO,CAAC,mBAAmB,EAC7C;YACA,OAAO,IAAI,CAAC;SACb;QAED,IACE,IAAI,CAAC,OAAO,CAAC,sBAAsB,KAAK,SAAS;YACjD,aAAa,GAAG,IAAI,CAAC,OAAO,CAAC,sBAAsB,EACnD;YACA,OAAO,IAAI,CAAC;SACb;QAED,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AA9ID,kCA8IC"}
@@ -0,0 +1,95 @@
1
+ /*!
2
+ * Copyright 2021 Google LLC
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
+ import { Publisher } from '.';
17
+ import { PubsubMessage } from './pubsub-message';
18
+ /**
19
+ * Encapsulates a series of message publishes from a rapid loop (or similar
20
+ * circumstance).
21
+ *
22
+ * This class is not meant to be instantiated outside of the `@google-cloud/pubsub`
23
+ * package. It is returned from {@link Topic#flowControlled}. Messages sent
24
+ * through an instance of this class will obey publisher flow control
25
+ * settings set through {@link PublisherOptions} on {@link Topic}, across
26
+ * all instances returned by {@link Topic#flowControlled} on that {@link Topic}.
27
+ */
28
+ export declare class FlowControlledPublisher {
29
+ private publisher;
30
+ private flowControl;
31
+ private idPromises;
32
+ constructor(publisher: Publisher);
33
+ /**
34
+ * Returns true if sending the specified Buffer would result in exceeding the
35
+ * limits of the flow control settings.
36
+ *
37
+ * @param {PubsubMessage} message The data buffer with the message's contents.
38
+ * @returns {boolean} True if the message would exceed flow control limits.
39
+ */
40
+ wouldExceed(message: PubsubMessage): boolean;
41
+ /**
42
+ * Publishes a message, subject to flow control restrictions.
43
+ *
44
+ * If the message can be sent immediately, this will return `null`. Otherwise,
45
+ * it will return a Promise<void> that resolves after it's okay to resume
46
+ * calling the method.
47
+ *
48
+ * @param {Buffer} [data] The message contents to be sent.
49
+ * @param {Attributes} [attributes] Optional attributes.
50
+ * @returns null, or a Promise that resolves when sending may resume.
51
+ *
52
+ * @example
53
+ * ```
54
+ * const wait = flowControlled.publish({data});
55
+ * if (wait) {
56
+ * await wait;
57
+ * }
58
+ *
59
+ * ```
60
+ * @example
61
+ * ```
62
+ * // It's okay to await unconditionally, it's equivalent to nextTick().
63
+ * await flowControlled.publish(data);
64
+ * ```
65
+ */
66
+ publish(message: PubsubMessage): Promise<void> | null;
67
+ /**
68
+ * Publishes a message unconditionally, updating flow control counters.
69
+ *
70
+ * You'll generally only want to use this if you want to deal with timing the
71
+ * flow control yourself, but you'd like the library to do the bean counting.
72
+ *
73
+ * @param {Buffer} [data] The message contents to be sent.
74
+ * @param {Attributes} [attributes] Optional attributes.
75
+ *
76
+ * @example
77
+ * ```
78
+ * if (!flowControlled.wouldExceed(data)) {
79
+ * flowControlled.publishNow(data);
80
+ * }
81
+ * ```
82
+ */
83
+ publishNow(message: PubsubMessage): void;
84
+ private doPublish;
85
+ /**
86
+ * Returns a Promise that will resolve to all of the currently sent
87
+ * message IDs (or reject if there is an error). This also clears
88
+ * out any currently sent messages, so the next call to `all()` will
89
+ * be a clean slate.
90
+ *
91
+ * @returns {Promise<string[]>} A Promise that resolves when all current
92
+ * messages are sent.
93
+ */
94
+ all(): Promise<string[]>;
95
+ }
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright 2021 Google LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.FlowControlledPublisher = void 0;
19
+ const pubsub_message_1 = require("./pubsub-message");
20
+ /**
21
+ * Encapsulates a series of message publishes from a rapid loop (or similar
22
+ * circumstance).
23
+ *
24
+ * This class is not meant to be instantiated outside of the `@google-cloud/pubsub`
25
+ * package. It is returned from {@link Topic#flowControlled}. Messages sent
26
+ * through an instance of this class will obey publisher flow control
27
+ * settings set through {@link PublisherOptions} on {@link Topic}, across
28
+ * all instances returned by {@link Topic#flowControlled} on that {@link Topic}.
29
+ */
30
+ class FlowControlledPublisher {
31
+ constructor(publisher) {
32
+ this.publisher = publisher;
33
+ this.flowControl = this.publisher.flowControl;
34
+ this.idPromises = [];
35
+ }
36
+ /**
37
+ * Returns true if sending the specified Buffer would result in exceeding the
38
+ * limits of the flow control settings.
39
+ *
40
+ * @param {PubsubMessage} message The data buffer with the message's contents.
41
+ * @returns {boolean} True if the message would exceed flow control limits.
42
+ */
43
+ wouldExceed(message) {
44
+ return this.flowControl.wouldExceed(pubsub_message_1.calculateMessageSize(message), 1);
45
+ }
46
+ /**
47
+ * Publishes a message, subject to flow control restrictions.
48
+ *
49
+ * If the message can be sent immediately, this will return `null`. Otherwise,
50
+ * it will return a Promise<void> that resolves after it's okay to resume
51
+ * calling the method.
52
+ *
53
+ * @param {Buffer} [data] The message contents to be sent.
54
+ * @param {Attributes} [attributes] Optional attributes.
55
+ * @returns null, or a Promise that resolves when sending may resume.
56
+ *
57
+ * @example
58
+ * ```
59
+ * const wait = flowControlled.publish({data});
60
+ * if (wait) {
61
+ * await wait;
62
+ * }
63
+ *
64
+ * ```
65
+ * @example
66
+ * ```
67
+ * // It's okay to await unconditionally, it's equivalent to nextTick().
68
+ * await flowControlled.publish(data);
69
+ * ```
70
+ */
71
+ publish(message) {
72
+ const doPublish = () => {
73
+ this.doPublish(message);
74
+ };
75
+ const size = pubsub_message_1.calculateMessageSize(message);
76
+ if (this.flowControl.wouldExceed(size, 1)) {
77
+ const waitPromise = this.flowControl.willSend(size, 1);
78
+ return waitPromise.then(doPublish);
79
+ }
80
+ else {
81
+ this.flowControl.willSend(size, 1).then(() => { });
82
+ doPublish();
83
+ return null;
84
+ }
85
+ }
86
+ /**
87
+ * Publishes a message unconditionally, updating flow control counters.
88
+ *
89
+ * You'll generally only want to use this if you want to deal with timing the
90
+ * flow control yourself, but you'd like the library to do the bean counting.
91
+ *
92
+ * @param {Buffer} [data] The message contents to be sent.
93
+ * @param {Attributes} [attributes] Optional attributes.
94
+ *
95
+ * @example
96
+ * ```
97
+ * if (!flowControlled.wouldExceed(data)) {
98
+ * flowControlled.publishNow(data);
99
+ * }
100
+ * ```
101
+ */
102
+ publishNow(message) {
103
+ this.flowControl.addToCount(pubsub_message_1.calculateMessageSize(message), 1);
104
+ this.doPublish(message);
105
+ }
106
+ doPublish(message) {
107
+ let idPromise = this.publisher.publishMessage(message);
108
+ // This will defer but not eat any errors.
109
+ const publishDone = (id) => {
110
+ this.flowControl.sent(pubsub_message_1.calculateMessageSize(message), 1);
111
+ return id;
112
+ };
113
+ idPromise.catch(publishDone);
114
+ idPromise = idPromise.then(publishDone);
115
+ this.idPromises.push(idPromise);
116
+ }
117
+ /**
118
+ * Returns a Promise that will resolve to all of the currently sent
119
+ * message IDs (or reject if there is an error). This also clears
120
+ * out any currently sent messages, so the next call to `all()` will
121
+ * be a clean slate.
122
+ *
123
+ * @returns {Promise<string[]>} A Promise that resolves when all current
124
+ * messages are sent.
125
+ */
126
+ all() {
127
+ const allPromise = Promise.all(this.idPromises);
128
+ this.idPromises = [];
129
+ return allPromise;
130
+ }
131
+ }
132
+ exports.FlowControlledPublisher = FlowControlledPublisher;
133
+ //# sourceMappingURL=flow-publisher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flow-publisher.js","sourceRoot":"","sources":["../../../src/publisher/flow-publisher.ts"],"names":[],"mapp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AAC,SAAS,CAAC,OAAO,CAAC,CAAC;IAC1B,CAAC;IAEO,SAAS,CAAC,OAAsB;QACtC,IAAI,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;QAEvD,0CAA0C;QAC1C,MAAM,WAAW,GAAG,CAAC,EAAU,EAAE,EAAE;YACjC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,qCAAoB,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC;YACxD,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC;QACF,SAAS,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;QAC7B,SAAS,GAAG,SAAS,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAExC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAClC,CAAC;IAED;;;;;;;;OAQG;IACH,GAAG;QACD,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAChD,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;QACrB,OAAO,UAAU,CAAC;IACpB,CAAC;CACF;AAhHD,0DAgHC"}
@@ -20,14 +20,13 @@ import { BatchPublishOptions } from './message-batch';
20
20
  import { Queue, OrderedQueue } from './message-queues';
21
21
  import { Topic } from '../topic';
22
22
  import { RequestCallback, EmptyCallback } from '../pubsub';
23
- import { google } from '../../protos/protos';
24
- export declare type PubsubMessage = google.pubsub.v1.IPubsubMessage;
25
- export interface Attributes {
26
- [key: string]: string;
27
- }
23
+ import { FlowControl, FlowControlOptions } from './flow-control';
24
+ import { PubsubMessage, Attributes } from './pubsub-message';
25
+ export { PubsubMessage, Attributes } from './pubsub-message';
28
26
  export declare type PublishCallback = RequestCallback<string>;
29
27
  export interface PublishOptions {
30
28
  batching?: BatchPublishOptions;
29
+ flowControlOptions?: FlowControlOptions;
31
30
  gaxOpts?: CallOptions;
32
31
  messageOrdering?: boolean;
33
32
  enableOpenTelemetryTracing?: boolean;
@@ -36,6 +35,8 @@ export interface PublishOptions {
36
35
  * @typedef PublishOptions
37
36
  * @property {BatchPublishOptions} [batching] The maximum number of bytes to
38
37
  * buffer before sending a payload.
38
+ * @property {FlowControlOptions} [publisherFlowControl] Publisher-side flow
39
+ * control settings. If this is undefined, Ignore will be the assumed action.
39
40
  * @property {object} [gaxOpts] Request configuration options, outlined
40
41
  * {@link https://googleapis.github.io/gax-nodejs/interfaces/CallOptions.html|here.}
41
42
  * @property {boolean} [messageOrdering] If true, messages published with the
@@ -44,6 +45,7 @@ export interface PublishOptions {
44
45
  * any order.
45
46
  */
46
47
  export declare const BATCH_LIMITS: BatchPublishOptions;
48
+ export declare const flowControlDefaults: FlowControlOptions;
47
49
  /**
48
50
  * A Publisher object allows you to publish messages to a specific topic.
49
51
  *
@@ -60,9 +62,34 @@ export declare class Publisher {
60
62
  settings: PublishOptions;
61
63
  queue: Queue;
62
64
  orderedQueues: Map<string, OrderedQueue>;
65
+ flowControl: FlowControl;
63
66
  constructor(topic: Topic, options?: PublishOptions);
67
+ /**
68
+ * Immediately sends all remaining queued data. This is mostly useful
69
+ * if you are planning to call close() on the PubSub object that holds
70
+ * the server connections.
71
+ *
72
+ * @private
73
+ *
74
+ * @param {EmptyCallback} [callback] Callback function.
75
+ * @returns {Promise<EmptyResponse>}
76
+ */
64
77
  flush(): Promise<void>;
65
78
  flush(callback: EmptyCallback): void;
79
+ /**
80
+ * Publish the provided message.
81
+ *
82
+ * @deprecated use {@link Publisher#publishMessage} instead.
83
+ *
84
+ * @private
85
+ * @see Publisher#publishMessage
86
+ *
87
+ * @param {buffer} data The message data. This must come in the form of a
88
+ * Buffer object.
89
+ * @param {object.<string, string>} [attributes] Attributes for this message.
90
+ * @param {PublishCallback} [callback] Callback function.
91
+ * @returns {Promise<PublishResponse>}
92
+ */
66
93
  publish(data: Buffer, attributes?: Attributes): Promise<string>;
67
94
  publish(data: Buffer, callback: PublishCallback): void;
68
95
  publish(data: Buffer, attributes: Attributes, callback: PublishCallback): void;
@@ -77,6 +104,7 @@ export declare class Publisher {
77
104
  * @param {PubsubMessage} [message] Options for this message.
78
105
  * @param {PublishCallback} [callback] Callback function.
79
106
  */
107
+ publishMessage(message: PubsubMessage): Promise<string>;
80
108
  publishMessage(message: PubsubMessage, callback: PublishCallback): void;
81
109
  /**
82
110
  * Indicates to the publisher that it is safe to continue publishing for the