Google Cloud Pub/Sub C++ Client 2.13.0
A C++ Client Library for Google Cloud Pub/Sub
Loading...
Searching...
No Matches
subscriber_connection.h
1// Copyright 2020 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_SUBSCRIBER_CONNECTION_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_SUBSCRIBER_CONNECTION_H
17
18#include "google/cloud/pubsub/application_callback.h"
19#include "google/cloud/pubsub/backoff_policy.h"
20#include "google/cloud/pubsub/connection_options.h"
21#include "google/cloud/pubsub/internal/subscriber_stub.h"
22#include "google/cloud/pubsub/message.h"
23#include "google/cloud/pubsub/pull_response.h"
24#include "google/cloud/pubsub/retry_policy.h"
25#include "google/cloud/pubsub/subscriber_options.h"
26#include "google/cloud/pubsub/subscription.h"
27#include "google/cloud/pubsub/version.h"
28#include "google/cloud/internal/non_constructible.h"
29#include "google/cloud/status_or.h"
30#include <functional>
31#include <initializer_list>
32#include <vector>
33
34namespace google {
35namespace cloud {
36namespace pubsub {
37GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
38
39/**
40 * A connection to the Cloud Pub/Sub service to receive events.
41 *
42 * This interface defines pure-virtual functions for each of the user-facing
43 * overload sets in `Subscriber`. That is, all of `Subscriber`'s overloads will
44 * forward to the one pure-virtual function declared in this interface. This
45 * allows users to inject custom behavior (e.g., with a Google Mock object) in a
46 * `Subscriber` object for use in their own tests.
47 *
48 * To create a concrete instance that connects you to the real Cloud Pub/Sub
49 * service, see `MakeSubscriberConnection()`.
50 *
51 * @par The *Params nested classes
52 * Applications may define classes derived from `SubscriberConnection`, for
53 * example, because they want to mock the class. To avoid breaking all such
54 * derived classes when we change the number or type of the arguments to the
55 * member functions we define lightweight structures to pass the arguments.
56 */
58 public:
59 virtual ~SubscriberConnection() = 0;
60
61 /// Wrap the arguments for `Subscribe()`
62 struct SubscribeParams {
63 ApplicationCallback callback;
64 };
65
66 /// Defines the interface for `Subscriber::Subscribe(ApplicationCallback)`
68
70 ExactlyOnceApplicationCallback callback;
71 };
72
73 /**
74 * Defines the interface for
75 * `Subscriber::Subscribe(ExactlyOnceApplicationCallback)`.
76 *
77 * We use a different name for this function (as opposed to an overload) to
78 * simplify the use of mocks.
79 */
81
82 virtual StatusOr<PullResponse> Pull();
83
84 /// Returns the configuration parameters for this object
85 virtual Options options() { return Options{}; }
86};
87
88/**
89 * Creates a new `SubscriberConnection` object to work with `Subscriber`.
90 *
91 * @note This function exists solely for backwards compatibility. It prevents
92 * existing code, which calls `MakeSubscriberConnection(subscription, {})`
93 * from breaking, due to ambiguity.
94 *
95 * @deprecated Please use `MakeSubscriberConnection(subscription)` instead.
96 */
97GOOGLE_CLOUD_CPP_DEPRECATED(
98 "use `MakeSubscriberConnection(subscription) instead")
100 Subscription subscription,
101 std::initializer_list<internal::NonConstructible>);
102
103/**
104 * Creates a new `SubscriberConnection` object to work with `Subscriber`.
105 *
106 * The `SubscriberConnection` class is not intended for direct use in
107 * applications, it is provided for applications wanting to mock the
108 * `Subscriber` behavior in their tests.
109 *
110 * @par Performance
111 * Creating a new `SubscriberConnection` is relatively expensive. This typically
112 * initiate connections to the service, and therefore these objects should be
113 * shared and reused when possible. Note that gRPC reuses existing OS resources
114 * (sockets) whenever possible, so applications may experience better
115 * performance on the second (and subsequent) calls to this function with the
116 * same `Options` from `GrpcOptionList` and `CommonOptionList`. However, this
117 * behavior is not guaranteed and applications should not rely on it.
118 *
119 * @see `SubscriberConnection`
120 *
121 * @par Changing Retry Parameters Example
122 * @snippet samples.cc subscriber-retry-settings
123 *
124 * @param subscription the Cloud Pub/Sub subscription used by the returned
125 * connection.
126 * @param opts The options to use for this call. Expected options are any
127 * of the types in the following option lists.
128 * - `google::cloud::CommonOptionList`
129 * - `google::cloud::GrpcOptionList`
130 * - `google::cloud::pubsub::PolicyOptionList`
131 * - `google::cloud::pubsub::SubscriberOptionList`
132 */
134 Subscription subscription, Options opts = {});
135
136/**
137 * Creates a new `SubscriberConnection` object to work with `Subscriber`.
138 *
139 * The `SubscriberConnection` class is not intended for direct use in
140 * applications, it is provided for applications wanting to mock the
141 * `Subscriber` behavior in their tests.
142 *
143 * @par Performance
144 * Creating a new `SubscriberConnection` is relatively expensive. This typically
145 * initiates connections to the service, and therefore these objects should be
146 * shared and reused when possible. Note that gRPC reuses existing OS resources
147 * (sockets) whenever possible, so applications may experience better
148 * performance on the second (and subsequent) calls to this function with the
149 * identical values for @p options. However, this behavior is not guaranteed
150 * and applications should not rely on it.
151 *
152 * @see `SubscriberConnection`
153 *
154 * @par Changing Retry Parameters Example
155 * @snippet samples.cc subscriber-retry-settings
156 *
157 * @param subscription the Cloud Pub/Sub subscription used by the returned
158 * connection.
159 * @param options configure the flow control and other parameters in the
160 * returned connection.
161 * @param connection_options (optional) general configuration for this
162 * connection, this type is also used to configure `pubsub::Publisher`.
163 * @param retry_policy control for how long (or how many times) are retryable
164 * RPCs attempted.
165 * @param backoff_policy controls the backoff behavior between retry attempts,
166 * typically some form of exponential backoff with jitter.
167 *
168 * @deprecated Please use the `MakeSubscriberConnection` function which accepts
169 * `google::cloud::Options` instead.
170 */
171GOOGLE_CLOUD_CPP_DEPRECATED(
172 "use the overload consuming google::cloud::Options instead")
174 Subscription subscription, SubscriberOptions options,
175 ConnectionOptions connection_options = {},
176 std::unique_ptr<pubsub::RetryPolicy const> retry_policy = {},
177 std::unique_ptr<pubsub::BackoffPolicy const> backoff_policy = {});
178
179GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
180} // namespace pubsub
181
182namespace pubsub_internal {
183GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
184
185std::shared_ptr<pubsub::SubscriberConnection> MakeTestSubscriberConnection(
186 pubsub::Subscription subscription, Options opts,
187 std::vector<std::shared_ptr<SubscriberStub>> stubs);
188
189GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
190} // namespace pubsub_internal
191} // namespace cloud
192} // namespace google
193
194#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_SUBSCRIBER_CONNECTION_H
friend friend class future
A connection to the Cloud Pub/Sub service to receive events.
Definition: subscriber_connection.h:57
virtual future< Status > Subscribe(SubscribeParams p)
Defines the interface for Subscriber::Subscribe(ApplicationCallback)
virtual future< Status > ExactlyOnceSubscribe(ExactlyOnceSubscribeParams p)
Defines the interface for Subscriber::Subscribe(ExactlyOnceApplicationCallback).
virtual StatusOr< PullResponse > Pull()
virtual Options options()
Returns the configuration parameters for this object.
Definition: subscriber_connection.h:85
Configure how a Subscriber handles incoming messages.
Definition: subscriber_options.h:97
Objects of this class identify a Cloud Pub/Sub subscription.
Definition: subscription.h:37
Contains all the Cloud Pub/Sub C++ client types and functions.
Definition: ack_handler.h:25
std::shared_ptr< SubscriberConnection > MakeSubscriberConnection(Subscription subscription, std::initializer_list< internal::NonConstructible >)
Creates a new SubscriberConnection object to work with Subscriber.
std::shared_ptr< SubscriberConnection > MakeSubscriberConnection(Subscription subscription, SubscriberOptions options, ConnectionOptions connection_options={}, std::unique_ptr< pubsub::RetryPolicy const > retry_policy={}, std::unique_ptr< pubsub::BackoffPolicy const > backoff_policy={})
Creates a new SubscriberConnection object to work with Subscriber.
std::shared_ptr< SubscriberConnection > MakeSubscriberConnection(Subscription subscription, Options opts={})
Creates a new SubscriberConnection object to work with Subscriber.
The namespace Google Cloud Platform C++ client libraries.
The response for a blocking pull.
Definition: pull_response.h:43
ExactlyOnceApplicationCallback callback
Definition: subscriber_connection.h:70
Wrap the arguments for Subscribe()
Definition: subscriber_connection.h:62
ApplicationCallback callback
Definition: subscriber_connection.h:63