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 <memory>
7#include <string>
8
9#include <userver/storages/redis/client_fwd.hpp>
10#include <userver/storages/redis/impl/base.hpp>
11#include <userver/storages/redis/impl/wait_connected_mode.hpp>
12
13#include <userver/storages/redis/subscription_token.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace redis {
18class SubscribeSentinel;
19} // namespace redis
20
21namespace storages::redis {
22
23/// @ingroup userver_clients
24///
25/// @brief Client that allows subscribing to Redis channel messages.
26///
27/// Usually retrieved from components::Redis component.
28///
29/// Callback for each received message is called only
30/// after the execution of callback for previous message has finished. For
31/// parallel message processing use the utils::Async().
32///
33/// @warning Messages can be received in any order due to Redis sharding.
34/// Sometimes messages can be duplicated due to subscriptions rebalancing.
35/// Some messages may be lost (it's a Redis limitation).
36///
37/// @warning The first callback execution can happen before `Subscribe()` or
38/// `Psubscribe()` return as it happens in a separate task.
39///
40/// @note a good GMock-based mock for this class can be found here:
41/// userver/storages/redis/mock_subscribe_client.hpp
43 public:
44 virtual ~SubscribeClient();
45
46 virtual SubscriptionToken Subscribe(
47 std::string channel, SubscriptionToken::OnMessageCb on_message_cb,
48 const USERVER_NAMESPACE::redis::CommandControl& command_control) = 0;
49
50 SubscriptionToken Subscribe(std::string channel,
51 SubscriptionToken::OnMessageCb on_message_cb) {
52 return Subscribe(std::move(channel), std::move(on_message_cb), {});
53 }
54
55 virtual SubscriptionToken Psubscribe(
56 std::string pattern, SubscriptionToken::OnPmessageCb on_pmessage_cb,
57 const USERVER_NAMESPACE::redis::CommandControl& command_control) = 0;
58
59 virtual size_t ShardsCount() const = 0;
60 virtual bool IsInClusterMode() const = 0;
61
62 SubscriptionToken Psubscribe(std::string pattern,
63 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, SubscriptionToken::OnMessageCb on_message_cb,
69 const USERVER_NAMESPACE::redis::CommandControl& command_control) = 0;
70
71 SubscriptionToken Ssubscribe(std::string channel,
72 SubscriptionToken::OnMessageCb on_message_cb) {
73 return Ssubscribe(std::move(channel), std::move(on_message_cb), {});
74 }
75};
76
77} // namespace storages::redis
78
79USERVER_NAMESPACE_END