userver: userver/storages/redis/subscribe_client.hpp Source File
Loading...
Searching...
No Matches
subscribe_client.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/redis/subscribe_client.hpp
4/// @brief @copybrief storages::redis::SubscribeClient
5
6#include <string>
7
8#include <userver/storages/redis/base.hpp>
9#include <userver/storages/redis/client_fwd.hpp>
10#include <userver/storages/redis/health_check_param.hpp>
11
12#include <userver/storages/redis/subscription_token.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace storages::redis {
17
18/// @ingroup userver_clients
19///
20/// @brief Client that allows subscribing to Redis channel messages.
21///
22/// Usually retrieved from components::Redis component.
23///
24/// Callback for each received message is called only
25/// after the execution of callback for previous message has finished. For
26/// parallel message processing use the utils::Async().
27///
28/// @warning Messages can be received in any order due to Redis sharding.
29/// Sometimes messages can be duplicated due to subscriptions rebalancing.
30/// Some messages may be lost (it's a Redis limitation).
31///
32/// @warning The first callback execution can happen before `Subscribe()` or
33/// `Psubscribe()` return as it happens in a separate task.
34///
35/// @note a good GMock-based mock for this class can be found here:
36/// userver/storages/redis/mock_subscribe_client.hpp
38public:
39 virtual ~SubscribeClient();
40
41 virtual SubscriptionToken Subscribe(
42 std::string channel,
43 SubscriptionToken::OnMessageCb on_message_cb,
44 const CommandControl& command_control
45 ) = 0;
46
47 SubscriptionToken Subscribe(std::string channel, SubscriptionToken::OnMessageCb on_message_cb) {
48 return Subscribe(std::move(channel), std::move(on_message_cb), {});
49 }
50
51 virtual SubscriptionToken Psubscribe(
52 std::string pattern,
53 SubscriptionToken::OnPmessageCb on_pmessage_cb,
54 const CommandControl& command_control
55 ) = 0;
56
57 virtual size_t ShardsCount() const = 0;
58 virtual bool IsInClusterMode() const = 0;
59 virtual bool IsReady(const HealthCheckParams& params) const = 0;
60
61 bool IsReady(WaitConnectedMode mode) const { return IsReady(HealthCheckParams{mode, 0, 0}); }
62
63 SubscriptionToken Psubscribe(std::string pattern, SubscriptionToken::OnPmessageCb on_pmessage_cb) {
64 return Psubscribe(std::move(pattern), std::move(on_pmessage_cb), {});
65 }
66
67 virtual SubscriptionToken Ssubscribe(
68 std::string channel,
69 SubscriptionToken::OnMessageCb on_message_cb,
70 const CommandControl& command_control
71 ) = 0;
72
73 /// Retrieves a single subscription token for multiple channels.
74 /// Uses a single coroutine to handle messages from all channels.
75 virtual SubscriptionToken Subscribe(
76 std::vector<std::string> channels,
77 SubscriptionToken::OnMessageCb on_message_cb,
78 const CommandControl& command_control
79 ) = 0;
80 /// Retrieves a single subscription token for multiple channels.
81 /// Uses a single coroutine to handle messages from all channels.
82 virtual SubscriptionToken Psubscribe(
83 std::vector<std::string> patterns,
84 SubscriptionToken::OnPmessageCb on_message_cb,
85 const CommandControl& command_control
86 ) = 0;
87 /// Retrieves a single subscription token for multiple channels.
88 /// Uses a single coroutine to handle messages from all channels.
89 virtual SubscriptionToken Ssubscribe(
90 std::vector<std::string> channels,
91 SubscriptionToken::OnMessageCb on_message_cb,
92 const CommandControl& command_control
93 ) = 0;
94
95 SubscriptionToken Ssubscribe(std::string channel, SubscriptionToken::OnMessageCb on_message_cb) {
96 return Ssubscribe(std::move(channel), std::move(on_message_cb), {});
97 }
98
99 SubscriptionToken Ssubscribe(std::vector<std::string> channels, SubscriptionToken::OnMessageCb on_message_cb) {
100 return Ssubscribe(std::move(channels), std::move(on_message_cb), {});
101 }
102
103 SubscriptionToken Psubscribe(std::vector<std::string> patterns, SubscriptionToken::OnPmessageCb on_message_cb) {
104 return Psubscribe(std::move(patterns), std::move(on_message_cb), {});
105 }
106};
107
108} // namespace storages::redis
109
110USERVER_NAMESPACE_END