userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
conflated_event_channel.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/concurrent/conflated_event_channel.hpp
4
/// @brief @copybrief concurrent::ConflatedEventChannel
5
6
#
include
<
atomic
>
7
#
include
<
string
>
8
#
include
<
utility
>
9
10
#
include
<
userver
/
concurrent
/
async_event_channel
.
hpp
>
11
#
include
<
userver
/
engine
/
single_consumer_event
.
hpp
>
12
#
include
<
userver
/
engine
/
task
/
task_with_result
.
hpp
>
13
#
include
<
userver
/
utils
/
resource_scopes
.
hpp
>
14
15
USERVER_NAMESPACE_BEGIN
16
17
namespace
concurrent
{
18
19
/// @ingroup userver_concurrency
20
///
21
/// @brief A non-blocking version of 'AsyncEventChannel'
22
///
23
/// The difference is that 'SendEvent' returns immediately, without waiting for
24
/// subscribers to finish. If 'SendEvent' is called multiple times while
25
/// subscribers are handling the previous event, new events will be conflated
26
/// (all events except for the last one will be ignored). This class can be used
27
/// instead of 'AsyncEventChannel' when we've got a "heavy" subscriber and we
28
/// don't want to slow down the pipeline.
29
class
ConflatedEventChannel
:
private
AsyncEventChannel
<> {
30
public
:
31
explicit
ConflatedEventChannel(std::string name, OnRemoveCallback on_listener_removal = {});
32
~ConflatedEventChannel()
override
;
33
34
/// @brief Forwards new events from an @ref concurrent::AsyncEventSource without replaying past events.
35
template
<
typename
... Args>
36
void
AddChannel
(
concurrent
::
AsyncEventSource
<Args...>& channel);
37
38
using
AsyncEventChannel
<>::AddListener;
39
40
/// Subscribes to updates using a member function. Also immediately invokes
41
/// the function with the current config snapshot.
42
///
43
/// Further updates are delivered after @ref utils::ResourceScopeStorage::AfterConstruction, including those updates
44
/// that arrived before it. Unsubscribe runs in @ref utils::ResourceScopeStorage::BeforeDestruction.
45
///
46
/// @param scopes storage that owns the subscription lifetime.
47
/// See @ref resource_scopes_parameter "where to obtain scopes".
48
template
<
typename
Class>
49
void
UpdateAndListen
(
utils
::ResourceScopeStorage& scopes, Class* obj, std::string_view name,
void
(Class::*func)());
50
51
#
ifndef
ARCADIA_ROOT
52
/// @overload
53
/// @deprecated Use the overload that takes @ref utils::ResourceScopeStorage.
54
///
55
/// Store the returned scope as a member and call `Unsubscribe` explicitly.
56
template
<
typename
Class>
57
concurrent
::AsyncEventSubscriberScope
UpdateAndListen
(Class* obj, std::string_view name,
void
(Class::*func)());
58
#
endif
59
60
void
SendEvent();
61
62
private
:
63
struct
Data
final
{
64
Data(
utils
::ResourceScopeStorage& scopes,
AsyncEventChannel
<>& channel);
65
~Data();
66
67
void
SendEvent();
68
69
template
<
typename
... Args>
70
void
OnChannelEvent(Args...) {
71
SendEvent();
72
}
73
74
utils
::ResourceScopeStorage& scopes;
75
const
std::string name;
76
std::atomic<
bool
> stop_flag{
false
};
77
engine::SingleConsumerEvent event;
78
engine::
TaskWithResult
<
void
> task;
79
};
80
81
utils
::WithResourceScopes<Data> data_;
82
};
83
84
template
<
typename
... Args>
85
void
ConflatedEventChannel
::
AddChannel
(
concurrent
::
AsyncEventSource
<Args...>& channel) {
86
auto
& data =
*
data_;
87
channel.AddListener(data.scopes, &data, data.name, &Data::OnChannelEvent<Args...>);
88
}
89
90
template
<
typename
Class>
91
void
ConflatedEventChannel
::
UpdateAndListen
(
92
utils
::ResourceScopeStorage& scopes,
93
Class* obj,
94
std::string_view name,
95
void
(Class::*func)()
96
) {
97
DoUpdateAndListen(scopes, obj, name, func, [obj, func] { (obj->*func)(); });
98
}
99
100
#
ifndef
ARCADIA_ROOT
101
template
<
typename
Class>
102
concurrent
::AsyncEventSubscriberScope
ConflatedEventChannel
::
UpdateAndListen
(
103
Class* obj,
104
std::string_view name,
105
void
(Class::*func)()
106
) {
107
return
DoUpdateAndListen(obj, name, func, [&] { (obj->*func)(); });
108
}
109
#
endif
110
111
}
// namespace concurrent
112
113
USERVER_NAMESPACE_END
userver
concurrent
conflated_event_channel.hpp
Generated on
for userver by
Doxygen
1.17.0