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