userver: userver/storages/redis/subscription_token.hpp Source File
Loading...
Searching...
No Matches
subscription_token.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/redis/subscription_token.hpp
4/// @brief @copybrief storages::redis::SubscriptionToken
5
6#include <functional>
7#include <memory>
8#include <string>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace redis {
13struct CommandControl;
14class SubscribeSentinel;
15} // namespace redis
16
17namespace storages::redis {
18
19namespace impl {
20
21class SubscriptionTokenImplBase {
22public:
23 SubscriptionTokenImplBase() = default;
24
25 SubscriptionTokenImplBase(SubscriptionTokenImplBase&&) = delete;
26 SubscriptionTokenImplBase& operator=(SubscriptionTokenImplBase&&) = delete;
27
28 SubscriptionTokenImplBase(const SubscriptionTokenImplBase&) = delete;
29 SubscriptionTokenImplBase& operator=(const SubscriptionTokenImplBase&&) = delete;
30
31 virtual ~SubscriptionTokenImplBase();
32
33 virtual void SetMaxQueueLength(size_t length) = 0;
34
35 virtual void Unsubscribe() = 0;
36};
37
38} // namespace impl
39
40/// @brief RAII subscription guard, that is usually retrieved from
41/// storages::redis::SubscribeClient.
42///
43/// See storages::redis::MockSubscriptionTokenImpl for hints on mocking and
44/// testing.
45class [[nodiscard]] SubscriptionToken final {
46public:
47 using OnMessageCb = std::function<void(const std::string& channel, const std::string& message)>;
48 using OnPmessageCb =
49 std::function<void(const std::string& pattern, const std::string& channel, const std::string& message)>;
50
51 SubscriptionToken();
52 SubscriptionToken(SubscriptionToken&&) noexcept;
53
54 /// This constructor can be used in tests/mocks to create your own
55 /// subscription token
56 SubscriptionToken(std::unique_ptr<impl::SubscriptionTokenImplBase>&& implementation);
57
58 ~SubscriptionToken();
59
60 SubscriptionToken& operator=(SubscriptionToken&&) noexcept;
61
62 /// There is a MPSC queue inside the connection. This parameter regulates
63 /// its maximum length. If it overflows, new messages are discarded.
64 void SetMaxQueueLength(size_t length);
65
66 /// Unsubscribe from the channel. This method is synchronous, once it
67 /// returned, no new calls to callback will be made.
69
70 /// Checks that token is not empty. Empty token has no implementation
71 /// inside. This method is mostly useful in unit tests. All methods
72 /// of this class work correctly on empty tokens.
73 bool IsEmpty() const noexcept { return impl_ == nullptr; }
74
75private:
76 std::unique_ptr<impl::SubscriptionTokenImplBase> impl_;
77};
78
79} // namespace storages::redis
80
81USERVER_NAMESPACE_END