FlowControlledPublisher

FlowControlledPublisher

Encapsulates a series of message publishes from a rapid loop (or similar circumstance).

This class is not meant to be instantiated outside of the @google-cloud/pubsub package. It is returned from Topic#flowControlled. Messages sent through an instance of this class will obey publisher flow control settings set through PublisherOptions on Topic, across all instances returned by Topic#flowControlled on that Topic.

Constructor

new FlowControlledPublisher()

Methods

all() → {Promise.<Array.<string>>}

Returns a Promise that will resolve to all of the currently sent message IDs (or reject if there is an error). This also clears out any currently sent messages, so the next call to all() will be a clean slate.

Returns:
Type Description
Promise.<Array.<string>>

A Promise that resolves when all current messages are sent.

publish(dataopt, attributesopt)

Publishes a message, subject to flow control restrictions.

If the message can be sent immediately, this will return null. Otherwise, it will return a Promise that resolves after it's okay to resume calling the method.

Parameters:
Name Type Attributes Description
data Buffer <optional>

The message contents to be sent.

attributes Attributes <optional>

Optional attributes.

Returns:
Type Description

null, or a Promise that resolves when sending may resume.

Examples
```
const wait = flowControlled.publish({data});
if (wait) {
  await wait;
}

```
```
// It's okay to await unconditionally, it's equivalent to nextTick().
await flowControlled.publish(data);
```

publishNow(dataopt, attributesopt)

Publishes a message unconditionally, updating flow control counters.

You'll generally only want to use this if you want to deal with timing the flow control yourself, but you'd like the library to do the bean counting.

Parameters:
Name Type Attributes Description
data Buffer <optional>

The message contents to be sent.

attributes Attributes <optional>

Optional attributes.

Example
```
if (!flowControlled.wouldExceed(data)) {
  flowControlled.publishNow(data);
}
```

wouldExceed(message) → {boolean}

Returns true if sending the specified Buffer would result in exceeding the limits of the flow control settings.

Parameters:
Name Type Description
message PubsubMessage

The data buffer with the message's contents.

Returns:
Type Description
boolean

True if the message would exceed flow control limits.