This document provides information about using concurrency control with messages published to a topic.
Concurrency control helps you override the default number of background (I/O) threads used by the client library to publish messages. This lets the publisher clients to send messages in parallel.
Concurrency control is an available feature in the Pub/Sub high-level client library. You can also implement your own concurrency control when you're using a low-level library.
Support for concurrency control depends on the programming language of the client library. For language implementations that support parallel threads, such as C++, Go, and Java, the client libraries make a default choice for the number of threads.
This page explains the concept of concurrency control and how to set up the feature for your publisher clients. To configure your subscriber clients for concurrency control, see Process more messages with concurrency control.
Before you begin
Before configuring the publish workflow, ensure you have completed the following tasks:
- Learn about topics and the publishing workflow.
- Create a topic.
Required roles
To get the permissions that you need to publish messages to a topic,
ask your administrator to grant you the
Pub/Sub Publisher (roles/pubsub.publisher
) IAM role on topic.
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
You need additional permissions to create or update topics and subscriptions.
Concurrency control configurations
The default values for the concurrency control variables and the names of the
variables might differ across client libraries. For example, in the Java client
library, the methods to configure concurrency control are
setExecutorProvider()
and setChannelProvider()
. For more information, see
the
API reference documentation.
setExecutorProvider() lets you customize the executor provider used for processing publish responses. For example, you can change the executor provider to one that returns a single, shared executor with a limited number of threads across multiple publisher clients. This configuration helps to limit the number of threads created.
setChannelProvider() lets you customize the channel provider used for opening connections to Pub/Sub. Typically, you don't configure this value unless you want to use the same channel across multiple publisher clients. Reusing a channel across too many clients might result in
GOAWAY
orENHANCE_YOUR_CALM
errors. If you see these errors in your application's logs or Cloud Logs, create more channels.
Code samples for concurrency control
C++
Before trying this sample, follow the C++ setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub C++ API reference documentation.
Go
Before trying this sample, follow the Go setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Go API reference documentation.
Java
Before trying this sample, follow the Java setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Java API reference documentation.
Ruby
Before trying this sample, follow the Ruby setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Ruby API reference documentation.
What's next
To restrict the locations in which Pub/Sub stores message data, see Restricting Pub/Sub resource locations.
To learn more about receiving messages, see Choose a subscription type.