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
43public:
44 virtual ~SubscribeClient();
45
46 virtual SubscriptionToken Subscribe(
47 std::string channel,
48 SubscriptionToken::OnMessageCb on_message_cb,
49 const USERVER_NAMESPACE::redis::CommandControl& command_control
50 ) = 0;
51
52 SubscriptionToken Subscribe(std::string channel, SubscriptionToken::OnMessageCb on_message_cb) {
53 return Subscribe(std::move(channel), std::move(on_message_cb), {});
54 }
55
56 virtual SubscriptionToken Psubscribe(
57 std::string pattern,
58 SubscriptionToken::OnPmessageCb on_pmessage_cb,
59 const USERVER_NAMESPACE::redis::CommandControl& command_control
60 ) = 0;
61
62 virtual size_t ShardsCount() const = 0;
63 virtual bool IsInClusterMode() const = 0;
64
65 SubscriptionToken Psubscribe(std::string pattern, SubscriptionToken::OnPmessageCb on_pmessage_cb) {
66 return Psubscribe(std::move(pattern), std::move(on_pmessage_cb), {});
67 }
68
69 virtual SubscriptionToken Ssubscribe(
70 std::string channel,
71 SubscriptionToken::OnMessageCb on_message_cb,
72 const USERVER_NAMESPACE::redis::CommandControl& command_control
73 ) = 0;
74
75 SubscriptionToken Ssubscribe(std::string channel, SubscriptionToken::OnMessageCb on_message_cb) {
76 return Ssubscribe(std::move(channel), std::move(on_message_cb), {});
77 }
78};
79
80} // namespace storages::redis
81
82USERVER_NAMESPACE_END