Google Cloud Pub/Sub C++ Client 2.13.0
A C++ Client Library for Google Cloud Pub/Sub
Loading...
Searching...
No Matches
ack_handler.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_ACK_HANDLER_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_ACK_HANDLER_H
17
18#include "google/cloud/pubsub/version.h"
19#include "google/cloud/status.h"
20#include <cstdint>
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 register a callback to receive Pub/Sub messages the
32 * callback must be able to receive both a `pubsub::Message` and its associated
33 * `pubsub::AckHandler`. Actions on a `pubsub::AckHandler` always affect the
34 * same message received in the callback. Applications cannot create standalone
35 * 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 * This class is *thread compatible*, only one thread should call non-const
45 * member functions of this class at a time. Note that because the non-const
46 * member functions are `&&` overloads the application can only call `ack()` or
47 * `nack()` exactly once, and only one of them.
48 */
49class AckHandler {
50 public:
51 ~AckHandler();
52
53 AckHandler(AckHandler&&) = default;
54 AckHandler& operator=(AckHandler&&) = default;
55
56 /**
57 * Acknowledges the message associated with this handler.
58 *
59 * @par Idempotency
60 * Note that this is not an idempotent operation, and therefore it is never
61 * retried. Furthermore, the service may still resend a message after a
62 * successful `ack()`. Applications developers are reminded that Cloud Pub/Sub
63 * offers "at least once" semantics so they should be prepared to handle
64 * duplicate messages.
65 */
66 void ack() && {
67 auto impl = std::move(impl_);
68 impl->ack();
69 }
70
71 /**
72 * Rejects the message associated with this handler.
73 *
74 * @par Idempotency
75 * Note that this is not an idempotent operation, and therefore it is never
76 * retried. Furthermore, the service may still resend a message after a
77 * successful `nack()`. Applications developers are reminded that Cloud
78 * Pub/Sub offers "at least once" semantics so they should be prepared to
79 * handle duplicate messages.
80 */
81 void nack() && {
82 auto impl = std::move(impl_);
83 impl->nack();
84 }
85
86 /// Returns the approximate number of times that Cloud Pub/Sub has attempted
87 /// to deliver the associated message to a subscriber.
88 std::int32_t delivery_attempt() const { return impl_->delivery_attempt(); }
89
90 /// Allow applications to mock an `AckHandler`.
91 class Impl {
92 public:
93 virtual ~Impl() = 0;
94 /// The implementation for `AckHandler::ack()`
95 virtual void ack() {}
96 /// The implementation for `AckHandler::nack()`
97 virtual void nack() {}
98 /// The implementation for `AckHandler::delivery_attempt()`
99 virtual std::int32_t delivery_attempt() const { return 0; }
100 };
101
102 /**
103 * Applications may use this constructor in their mocks.
104 */
105 explicit AckHandler(std::unique_ptr<Impl> impl) : impl_(std::move(impl)) {}
106
107 private:
108 std::unique_ptr<Impl> impl_;
109};
110
111GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
112} // namespace pubsub
113} // namespace cloud
114} // namespace google
115
116#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_ACK_HANDLER_H
Allow applications to mock an AckHandler.
Definition: ack_handler.h:91
virtual void nack()
The implementation for AckHandler::nack()
Definition: ack_handler.h:97
virtual void ack()
The implementation for AckHandler::ack()
Definition: ack_handler.h:95
virtual std::int32_t delivery_attempt() const
The implementation for AckHandler::delivery_attempt()
Definition: ack_handler.h:99
Defines the interface to acknowledge and reject messages.
Definition: ack_handler.h:49
AckHandler(AckHandler &&)=default
void nack() &&
Rejects the message associated with this handler.
Definition: ack_handler.h:81
AckHandler(std::unique_ptr< Impl > impl)
Applications may use this constructor in their mocks.
Definition: ack_handler.h:105
void ack() &&
Acknowledges the message associated with this handler.
Definition: ack_handler.h:66
AckHandler & operator=(AckHandler &&)=default
std::int32_t delivery_attempt() const
Returns the approximate number of times that Cloud Pub/Sub has attempted to deliver the associated me...
Definition: ack_handler.h:88
Contains all the Cloud Pub/Sub C++ client types and functions.
Definition: ack_handler.h:25
The namespace Google Cloud Platform C++ client libraries.