userver: concurrent::AsyncEventSource< Args > Class Template Reference
Loading...
Searching...
No Matches
concurrent::AsyncEventSource< Args > Class Template Referenceabstract

#include <userver/concurrent/async_event_source.hpp>

Detailed Description

template<typename... Args>
class concurrent::AsyncEventSource< Args >

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.

Member Typedef Documentation

◆ Function

template<typename... Args>
using concurrent::AsyncEventSource< Args >::Function = std::function<void(Args... args)>

Definition at line 55 of file async_event_source.hpp.

Member Function Documentation

◆ AddListener() [1/2]

template<typename... Args>
template<class Class>
void concurrent::AsyncEventSource< Args >::AddListener ( utils::ResourceScopeStorage & scopes,
Class * obj,
std::string_view name,
void(Class::* func )(Args...) )
inline

Subscribes to updates from this event source.

The listener won't be called immediately.

Example usage:

class WeatherRecorder final {
public:
WeatherRecorder(utils::ResourceScopeStorage& scopes, WeatherStorage& weather_storage) {
weather_storage.GetSource()
.AddListener(scopes, concurrent::FunctionId(this), "recorder", [this](WeatherKind weather) {
recorded_weather.push_back(weather);
});
}
std::vector<WeatherKind> recorded_weather;
};
UTEST(AsyncEventChannel, AddListenerSample) {
WeatherStorage weather_storage(WeatherKind::kSunny);
utils::WithResourceScopes<WeatherRecorder> recorder(std::in_place, weather_storage);
weather_storage.Set(WeatherKind::kRainy);
weather_storage.Set(WeatherKind::kSunny);
weather_storage.Set(WeatherKind::kSunny);
EXPECT_EQ(recorder->recorded_weather, (std::vector{WeatherKind::kRainy, WeatherKind::kSunny, WeatherKind::kSunny}));
}

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.

Parameters
scopesstorage that owns the subscription lifetime. See where to obtain scopes.
objthe subscriber, which is the owner of the listener method, and is also used as the unique identifier of the subscription for this AsyncEventSource
namethe name of the subscriber, for diagnostic purposes
functhe listener method, usually called On<DataName>Update, e.g. OnConfigUpdate or OnCacheUpdate

Possible races when using AddListener

Manually processing the current value and subscribing in separate steps introduces a race. The following pseudocode assumes that scopes is already active:

OnCacheUpdate(cache.Get());
source.AddListener(scopes, this, "cache-listener", &MyClass::OnCacheUpdate);

An update between reading the cache and subscribing is missed. Reversing the order also introduces a race:

source.AddListener(scopes, this, "cache-listener", &MyClass::OnCacheUpdate);
OnCacheUpdate(cache.Get());

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.

◆ AddListener() [2/2]

template<typename... Args>
void concurrent::AsyncEventSource< Args >::AddListener ( utils::ResourceScopeStorage & scopes,
FunctionId id,
std::string_view name,
Function && func )
inline

A type-erased version of concurrent::AsyncEventSource::AddListener.

Parameters
scopesstorage that owns the subscription lifetime. See where to obtain scopes.
idthe unique identifier of the subscription
namethe name of the subscriber, for diagnostic purposes
functhe callback used for event notifications

Definition at line 116 of file async_event_source.hpp.


The documentation for this class was generated from the following file: