#include <userver/concurrent/async_event_source.hpp>
The read-only side of an event channel. Events are delivered to listeners in a strict FIFO order, i.e. only after the event was processed a new event may appear for processing, same listener is never called concurrently.
Definition at line 53 of file async_event_source.hpp.
Classes | |
| class | AsyncEventSubscriberScope |
Public Types | |
| using | Function = std::function<void(Args... args)> |
Public Member Functions | |
| template<class Class> | |
| void | AddListener (utils::ResourceScopeStorage &scopes, Class *obj, std::string_view name, void(Class::*func)(Args...)) |
| Subscribes to updates from this event source. | |
| void | AddListener (utils::ResourceScopeStorage &scopes, FunctionId id, std::string_view name, Function &&func) |
| A type-erased version of concurrent::AsyncEventSource::AddListener. | |
Protected Member Functions | |
| virtual void | DoAddListener (FunctionId id, std::string_view name, Function &&func)=0 |
| Registers func as the listener for id. | |
| virtual void | RemoveListener (FunctionId id) noexcept=0 |
| Removes the listener for id when its subscription scope is destroyed. | |
| using concurrent::AsyncEventSource< Args >::Function = std::function<void(Args... args)> |
Definition at line 55 of file async_event_source.hpp.
|
inline |
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 |
A type-erased version of concurrent::AsyncEventSource::AddListener.
| scopes | storage that owns the subscription lifetime. See where to obtain scopes. |
| id | the unique identifier of the subscription |
| name | the name of the subscriber, for diagnostic purposes |
| func | the callback used for event notifications |
Definition at line 116 of file async_event_source.hpp.