Skip to content

docs(pubsub): Create a Cloud Pub/Sub sample for publishing with hedging - #14623

Merged
michaelpri10 merged 1 commit into
googleapis:mainfrom
michaelpri10:docs/publish-hedging-sample
Oct 9, 2026
Merged

michaelpri10 merged 1 commit into
googleapis:mainfrom
michaelpri10:docs/publish-hedging-sample

Conversation

@michaelpri10

Copy link
Copy Markdown
Contributor

This also bumps the samples libraries-bom version

…ng and bump the samples libraries-bom version
@michaelpri10
michaelpri10 requested review from a team as code owners October 9, 2026 21:44
@snippet-bot

snippet-bot Bot commented Oct 9, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the libraries-bom dependency version to 26.90.0 and introduces a new sample snippet, PublishWithHedgingSettingsExample.java, which demonstrates how to publish Pub/Sub messages with custom hedging settings. It also adds a corresponding integration test in PublisherIT.java. Feedback on the changes suggests refactoring the resource management of the Publisher instance to use a try-with-resources block for cleaner and safer resource handling, as well as using a double instead of a float for the refillRatio setting to avoid implicit widening.

Comment on lines +44 to +78
Publisher publisher = null;

try {
// Hedging settings configures how and when the publisher sends multiple concurrent publish
// requests to reduce tail latency.
Duration hedgeDelay = Duration.ofMillis(500); // default: 1000 ms (1 second)
int maxTokens = 100; // default: 50
float refillRatio = 0.05f; // default: 0.1

HedgingSettings hedgingSettings =
HedgingSettings.newBuilder()
.setHedgeDelay(hedgeDelay)
.setMaxTokens(maxTokens)
.setRefillRatio(refillRatio)
.build();

// Create a publisher instance with hedging settings bound to the topic
publisher = Publisher.newBuilder(topicName).setHedgingSettings(hedgingSettings).build();

String message = "Hello world!";
ByteString data = ByteString.copyFromUtf8(message);
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();

// Once published, returns a server-assigned message id (unique within the topic)
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
String messageId = messageIdFuture.get();
System.out.println("Published a message with hedging settings: " + messageId);

} finally {
if (publisher != null) {
// When finished with the publisher, shutdown to free up resources.
publisher.shutdown();
publisher.awaitTermination(1, TimeUnit.MINUTES);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using try-with-resources is the modern, idiomatic Java way to manage resources. Since Publisher implements AutoCloseable (via BackgroundResource), its close() method automatically handles shutdown() and awaitTermination(). Refactoring this to use try-with-resources reduces boilerplate and ensures the publisher is safely closed. Additionally, HedgingSettings.Builder#setRefillRatio expects a double parameter, so we can use double directly instead of float to avoid implicit widening.

    // Hedging settings configures how and when the publisher sends multiple concurrent publish
    // requests to reduce tail latency.
    Duration hedgeDelay = Duration.ofMillis(500); // default: 1000 ms (1 second)
    int maxTokens = 100; // default: 50
    double refillRatio = 0.05; // default: 0.1

    HedgingSettings hedgingSettings =
        HedgingSettings.newBuilder()
            .setHedgeDelay(hedgeDelay)
            .setMaxTokens(maxTokens)
            .setRefillRatio(refillRatio)
            .build();

    // Create a publisher instance with hedging settings bound to the topic
    try (Publisher publisher =
        Publisher.newBuilder(topicName).setHedgingSettings(hedgingSettings).build()) {

      String message = "Hello world!";
      ByteString data = ByteString.copyFromUtf8(message);
      PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();

      // Once published, returns a server-assigned message id (unique within the topic)
      ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
      String messageId = messageIdFuture.get();
      System.out.println("Published a message with hedging settings: " + messageId);
    }

@michaelpri10
michaelpri10 merged commit 9de611f into googleapis:main Oct 9, 2026
206 of 208 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants