#include <userver/concurrent/async_event_channel.hpp>
AsyncEventChannel is an in-process pub-sub with strict FIFO serialization, i.e. only after the event was processed a new event may appear for processing, same listener is never called concurrently.
Example usage:
Definition at line 83 of file async_event_channel.hpp.
Public Types | |
| using | Function = typename AsyncEventSource<Args...>::Function |
| using | OnRemoveCallback = std::function<void(const Function&)> |
Public Member Functions | |
| AsyncEventChannel (std::string_view name) | |
| The primary constructor. | |
| AsyncEventChannel (std::string_view name, OnRemoveCallback on_listener_removal) | |
| Constructs a channel with a debug check for listener removal. | |
| template<typename UpdaterFunc> | |
| void | DoUpdateAndListen (utils::ResourceScopeStorage &scopes, FunctionId id, std::string_view name, Function &&func, UpdaterFunc &&updater) |
| Processes the current value and subscribes to subsequent events for the lifetime of scopes. | |
| template<typename Class, typename UpdaterFunc> | |
| void | DoUpdateAndListen (utils::ResourceScopeStorage &scopes, Class *obj, std::string_view name, void(Class::*func)(Args...), UpdaterFunc &&updater) |
| void | SendEvent (Args... args) const |
| const std::string & | Name () const noexcept |
| void | AddListener (utils::ResourceScopeStorage &scopes, Class *obj, std::string_view name, void(Class::*func)(Args...)) |
| Subscribes to updates from this event source. | |
| using concurrent::AsyncEventChannel< Args >::Function = typename AsyncEventSource<Args...>::Function |
Definition at line 85 of file async_event_channel.hpp.
| using concurrent::AsyncEventChannel< Args >::OnRemoveCallback = std::function<void(const Function&)> |
Definition at line 86 of file async_event_channel.hpp.
|
inlineexplicit |
The primary constructor.
| name | used for diagnostic purposes and is also accessible with Name |
Definition at line 90 of file async_event_channel.hpp.
|
inline |
Constructs a channel with a debug check for listener removal.
The callback receives the listener function when its subscription ends and can invoke it to check the lifetime of the captured data.
Example usage:
| name | used for diagnostic purposes and is also accessible with Name |
| on_listener_removal | the callback used for check |
Definition at line 112 of file async_event_channel.hpp.
|
inlineinherited |
Subscribes to updates from this event source.
The listener won't be called immediately.
Example usage:
Subscription starts after utils::ResourceScopeStorage::AfterConstruction. Events sent before that are not delivered to the listener. Unsubscribe runs in utils::ResourceScopeStorage::BeforeDestruction. This event source must outlive the resource scopes.
| scopes | storage that owns the subscription lifetime. See where to obtain scopes. |
| obj | the subscriber, which is the owner of the listener method, and is also used as the unique identifier of the subscription for this AsyncEventSource |
| name | the name of the subscriber, for diagnostic purposes |
| func | the listener method, usually called On<DataName>Update, e.g. OnConfigUpdate or OnCacheUpdate |
Manually processing the current value and subscribing in separate steps introduces a race. The following pseudocode assumes that scopes is already active:
An update between reading the cache and subscribing is missed. Reversing the order also introduces a race:
The event callback may run concurrently with the manual call, possibly processing the same cache value twice.
To process the current value and then listen to updates, use UpdateAndListen on the concrete data source, e.g. components::CachingComponentBase::UpdateAndListen. If only concurrent::AsyncEventChannel is available, use concurrent::AsyncEventChannel::DoUpdateAndListen.
Definition at line 101 of file async_event_source.hpp.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
| scopes | storage that owns the subscription lifetime. See where to obtain scopes. |
| obj | object on which to invoke the callback. |
| name | subscriber name. |
| func | member function to invoke. |
| updater | callback that reads the current state. |
Definition at line 178 of file async_event_channel.hpp.
|
inline |
Processes the current value and subscribes to subsequent events for the lifetime of scopes.
Synchronously calls updater and records the channel's event generation. When the scope is entered, updater is called again if an event was sent during construction, then func is subscribed atomically with that check. Unsubscribes on scope exit.
obj and func) and capture this explicitly.| scopes | storage that owns the subscription lifetime. See where to obtain scopes. |
| id | subscriber identity. |
| name | subscriber name. |
| func | callback invoked for channel events. |
| updater | callback that reads the current state. |
Definition at line 134 of file async_event_channel.hpp.
|
inlinenoexcept |
Definition at line 245 of file async_event_channel.hpp.
|
inline |
Send the next event and wait until all the listeners process it.
Strict FIFO serialization is guaranteed, i.e. only after this event is processed a new event may be delivered for the subscribers, same listener/subscriber is never called concurrently.
Definition at line 199 of file async_event_channel.hpp.