Google Cloud Pub/Sub C++ Client 2.13.0
A C++ Client Library for Google Cloud Pub/Sub
Loading...
Searching...
No Matches
pull_ack_handler.h
1// Copyright 2022 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_PULL_ACK_HANDLER_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_PULL_ACK_HANDLER_H
17
18#include "google/cloud/pubsub/version.h"
19#include "google/cloud/future.h"
20#include "google/cloud/status.h"
21#include <memory>
22
23namespace google {
24namespace cloud {
25namespace pubsub {
26GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
27
28/**
29 * Defines the interface to acknowledge and reject messages.
30 *
31 * When applications pull a message from Cloud Pub/Sub messages the response
32 * includes both the `pubsub::Message` and its associated
33 * `pubsub::PullAckHandler`. Actions on a `pubsub::PullAckHandler` always affect
34 * the same message received in the callback. Applications cannot create
35 * standalone handlers (except in unit tests via mocks).
36 *
37 * This interface allows applications to acknowledge and reject messages that
38 * are provided by the Cloud Pub/Sub C++ client library to the application. Note
39 * that this class is move-able, to support applications that process messages
40 * asynchronously. However, this class is *not* copy-able, because messages can
41 * only be acknowledged or rejected exactly once.
42 *
43 * @par Thread Safety
44 *
45 * This class is *thread compatible*; only one thread should call non-const
46 * member functions of this class at a time. Note that because the non-const
47 * member functions are `&&` overloads the application can only call `ack()` or
48 * `nack()` exactly once, and only one of them.
49 */
50class PullAckHandler {
51 public:
52 PullAckHandler(PullAckHandler&&) = default;
55
56 /**
57 * Acknowledges the message associated with this handler.
58 *
59 * @par Idempotency
60 * If exactly-once is enabled in the subscription, the client library will
61 * retry this operation in the background until it succeeds, fails with a
62 * permanent error, or the ack id has become unusable (all ack ids are
63 * unusable after 10 minutes). The returned future is satisfied when the retry
64 * loop completes.
65 *
66 * If exactly-once is not enabled, the request is handled on a best-effort
67 * basis.
68 *
69 * If the future is satisfied with an Okay `Status` **and** exactly-once
70 * delivery is enabled in the subscription, then the message will not be
71 * resent by Cloud Pub/Sub. We remind the reader that Cloud Pub/Sub defaults
72 * to "at least once" delivery. That is, without exactly-once delivery, the
73 * message *may* be resent even after the future is satisfied with an Okay
74 * `Status`.
75 *
76 * If the future is satisfied with an error, it is possible that Cloud Pub/Sub
77 * never received the acknowledgement, and will resend the message.
78 */
79 future<Status> ack() && {
80 auto impl = std::move(impl_);
81 return impl->ack();
82 }
83
84 /**
85 * Rejects the message associated with this handler.
86 *
87 * @par Idempotency
88 * If exactly-once is enabled in the subscription, the client library will
89 * retry this operation in the background until it succeeds, fails with a
90 * permanent error, or the ack id has become unusable (all ack ids are
91 * unusable after 10 minutes). The returned future is satisfied when the retry
92 * loop completes.
93 *
94 * If exactly-once is not enabled, the request is handled on a best-effort
95 * basis.
96 *
97 * In any case, Cloud Pub/Sub will eventually resend the message. It might do
98 * so sooner if the operation succeeds.
99 */
100 future<Status> nack() && {
101 auto impl = std::move(impl_);
102 return impl->nack();
103 }
104
105 /// Returns the approximate number of times that Cloud Pub/Sub has attempted
106 /// to deliver the associated message to a subscriber.
107 std::int32_t delivery_attempt() const { return impl_->delivery_attempt(); }
108
109 /// Allow applications to mock an `PullAckHandler`.
110 class Impl {
111 public:
112 virtual ~Impl() = 0;
113 /// The implementation for `PullAckHandler::ack()`
114 virtual future<Status> ack() {
115 return make_ready_future(
116 Status(StatusCode::kUnimplemented, "base class"));
117 }
118 /// The implementation for `PullAckHandler::nack()`
119 virtual future<Status> nack() {
120 return make_ready_future(
121 Status(StatusCode::kUnimplemented, "base class"));
122 }
123 /// The implementation for `PullAckHandler::delivery_attempt()`
124 virtual std::int32_t delivery_attempt() const { return 0; }
125 };
126
127 /**
128 * Applications may use this constructor in their mocks.
129 */
130 explicit PullAckHandler(std::unique_ptr<Impl> impl)
131 : impl_(std::move(impl)) {}
132
133 private:
134 std::unique_ptr<Impl> impl_;
135};
136
137GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
138} // namespace pubsub
139} // namespace cloud
140} // namespace google
141
142#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_PULL_ACK_HANDLER_H
Status(StatusCode code, std::string message, ErrorInfo info={})
friend friend class future
Allow applications to mock an PullAckHandler.
Definition: pull_ack_handler.h:110
virtual std::int32_t delivery_attempt() const
The implementation for PullAckHandler::delivery_attempt()
Definition: pull_ack_handler.h:124
virtual future< Status > ack()
The implementation for PullAckHandler::ack()
Definition: pull_ack_handler.h:114
virtual future< Status > nack()
The implementation for PullAckHandler::nack()
Definition: pull_ack_handler.h:119
Defines the interface to acknowledge and reject messages.
Definition: pull_ack_handler.h:50
future< Status > ack() &&
Acknowledges the message associated with this handler.
Definition: pull_ack_handler.h:79
PullAckHandler & operator=(PullAckHandler &&)=default
PullAckHandler(std::unique_ptr< Impl > impl)
Applications may use this constructor in their mocks.
Definition: pull_ack_handler.h:130
future< Status > nack() &&
Rejects the message associated with this handler.
Definition: pull_ack_handler.h:100
std::int32_t delivery_attempt() const
Returns the approximate number of times that Cloud Pub/Sub has attempted to deliver the associated me...
Definition: pull_ack_handler.h:107
PullAckHandler(PullAckHandler &&)=default
Contains all the Cloud Pub/Sub C++ client types and functions.
Definition: ack_handler.h:25
The namespace Google Cloud Platform C++ client libraries.
future< typename internal::make_ready_return< T >::type > make_ready_future(T &&t)