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 108 of file async_event_source.hpp.

Inheritance diagram for concurrent::AsyncEventSource< Args >:

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.
template<class Class>
AsyncEventSubscriberScope AddListener (Class *obj, std::string_view name, void(Class::*func)(Args...))
AsyncEventSubscriberScope AddListener (FunctionId id, std::string_view name, Function &&func)
 A type-erased version of concurrent::AsyncEventSource::AddListener.
void RemoveListener (FunctionId, UnsubscribingKind) noexcept override=0
 Used by AsyncEventSubscriberScope to cancel an event subscription.

Protected Member Functions

virtual AsyncEventSubscriberScope DoAddListener (FunctionId id, std::string_view name, Function &&func)=0

Member Typedef Documentation

◆ Function

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

Definition at line 110 of file async_event_source.hpp.

Member Function Documentation

◆ AddListener() [1/4]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Deprecated
Use the overload that takes utils::ResourceScopeStorage. Store the returned scope as a member and call Unsubscribe explicitly.

Definition at line 182 of file async_event_source.hpp.

◆ AddListener() [2/4]

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

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

Deprecated
Use the overload that takes utils::ResourceScopeStorage.
Parameters
idthe unique identifier of the subscription
namethe name of the subscriber, for diagnostic purposes
functhe callback used for event notifications

Definition at line 192 of file async_event_source.hpp.

◆ AddListener() [3/4]

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 156 of file async_event_source.hpp.

◆ AddListener() [4/4]

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 171 of file async_event_source.hpp.


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