userver: dynamic_config::Source Class Reference
Loading...
Searching...
No Matches
dynamic_config::Source Class Referencefinal

#include <userver/dynamic_config/source.hpp>

Detailed Description

A client for easy dynamic config fetching in components.

After construction, dynamic_config::Source can be copied around and passed to clients or child helper classes.

Usually retrieved from components::DynamicConfig component.

Typical usage:

namespace myservice::smth {
inline const dynamic_config::Key kMyConfig{"SAMPLE_INTEGER_FROM_RUNTIME_CONFIG", 42};
int Component::DoSomething() const {
// Getting a snapshot of dynamic config.
const auto runtime_config = config_.GetSnapshot();
return runtime_config[kMyConfig];
}
} // namespace myservice::smth
Examples
components/component_sample_test.hpp.

Definition at line 74 of file source.hpp.

Public Types

using SnapshotEventSource = concurrent::AsyncEventSource<const Snapshot&>
 
using DiffEventSource = concurrent::AsyncEventSource<const Diff&>
 

Public Member Functions

 Source (impl::StorageData &storage)
 
 Source (const Source &)=default
 
 Source (Source &&)=default
 
Sourceoperator= (const Source &)=default
 
Sourceoperator= (Source &&)=default
 
Snapshot GetSnapshot () const
 
template<typename VariableType >
VariableSnapshotPtr< VariableType > GetSnapshot (const Key< VariableType > &key) const
 
template<typename VariableType >
VariableType GetCopy (const Key< VariableType > &key) const
 
template<typename Class >
concurrent::AsyncEventSubscriberScope UpdateAndListen (Class *obj, std::string_view name, void(Class::*func)(const dynamic_config::Snapshot &config))
 
template<typename Class >
concurrent::AsyncEventSubscriberScope UpdateAndListen (Class *obj, std::string_view name, void(Class::*func)(const dynamic_config::Diff &diff))
 Subscribes to dynamic-config updates with information about the current and previous states.
 
template<typename Class , typename... Keys>
concurrent::AsyncEventSubscriberScope UpdateAndListen (Class *obj, std::string_view name, void(Class::*func)(const dynamic_config::Snapshot &config), const Keys &... keys)
 Subscribes to updates of a subset of all configs.
 
SnapshotEventSourceGetEventChannel ()
 

Member Typedef Documentation

◆ DiffEventSource

◆ SnapshotEventSource

Constructor & Destructor Documentation

◆ Source()

dynamic_config::Source::Source ( impl::StorageData & storage)
explicit

For internal use only. Obtain using components::DynamicConfig or dynamic_config::StorageMock instead.

Member Function Documentation

◆ GetCopy()

template<typename VariableType >
VariableType dynamic_config::Source::GetCopy ( const Key< VariableType > & key) const
inline

Definition at line 97 of file source.hpp.

◆ GetSnapshot()

template<typename VariableType >
VariableSnapshotPtr< VariableType > dynamic_config::Source::GetSnapshot ( const Key< VariableType > & key) const
inline

Definition at line 92 of file source.hpp.

◆ UpdateAndListen() [1/3]

template<typename Class >
concurrent::AsyncEventSubscriberScope dynamic_config::Source::UpdateAndListen ( Class * obj,
std::string_view name,
void(Class::*)(const dynamic_config::Diff &diff) func )
inline

Subscribes to dynamic-config updates with information about the current and previous states.

Subscribes to dynamic-config updates using a member function, named OnConfigUpdate by convention. Also constructs dynamic_config::Diff object using std::nullopt and current config snapshot, then immediately invokes the function with it (this invocation will be executed synchronously).

Note
Callbacks occur in full accordance with components::DynamicConfigClientUpdater options.
Warning
In debug mode the last notification for any subscriber will be called with std::nullopt and current config snapshot.

Example usage:

void OnConfigUpdate(const dynamic_config::Diff& diff_data) {
if (!diff_data.previous) return;
const auto& previous = *diff_data.previous;
const auto& current = diff_data.current;
if (previous[kBoolConfig] != current[kBoolConfig]) {
OnBoolConfigChanged();
}
if (previous[kDummyConfig].foo != current[kDummyConfig].foo) {
OnFooChanged();
}
}
void OnBoolConfigChanged() { bool_config_change_counter_++; }
void OnFooChanged() { foo_interesting_event_counter_++; }
Parameters
objthe subscriber, which is the owner of the listener method, and is also used as the unique identifier of the subscription
namethe name of the subscriber, for diagnostic purposes
functhe listener method, named OnConfigUpdate by convention.
Returns
a concurrent::AsyncEventSubscriberScope controlling the subscription, which should be stored as a member in the subscriber; Unsubscribe should be called explicitly
See also
based on concurrent::AsyncEventSource engine
dynamic_config::Diff

Definition at line 163 of file source.hpp.

◆ UpdateAndListen() [2/3]

template<typename Class >
concurrent::AsyncEventSubscriberScope dynamic_config::Source::UpdateAndListen ( Class * obj,
std::string_view name,
void(Class::*)(const dynamic_config::Snapshot &config) func )
inline

Subscribes to dynamic-config updates using a member function. Also immediately invokes the function with the current config snapshot (this invocation will be executed synchronously).

Note
Callbacks occur in full accordance with components::DynamicConfigClientUpdater options.
Parameters
objthe subscriber, which is the owner of the listener method, and is also used as the unique identifier of the subscription
namethe name of the subscriber, for diagnostic purposes
functhe listener method, named OnConfigUpdate by convention.
Returns
a concurrent::AsyncEventSubscriberScope controlling the subscription, which should be stored as a member in the subscriber; Unsubscribe should be called explicitly
See also
based on concurrent::AsyncEventSource engine

Definition at line 120 of file source.hpp.

◆ UpdateAndListen() [3/3]

template<typename Class , typename... Keys>
concurrent::AsyncEventSubscriberScope dynamic_config::Source::UpdateAndListen ( Class * obj,
std::string_view name,
void(Class::*)(const dynamic_config::Snapshot &config) func,
const Keys &... keys )
inline

Subscribes to updates of a subset of all configs.

Subscribes to dynamic-config updates using a member function, named OnConfigUpdate by convention. The function will be invoked if at least one of the configs has been changed since the previous invocation. So at the first time immediately invokes the function with the current config snapshot (this invocation will be executed synchronously).

Note
Сallbacks occur only if one of the passed config is changed. This is true under any components::DynamicConfigClientUpdater options.
Warning
To use this function, configs must have the operator==.
Parameters
objthe subscriber, which is the owner of the listener method, and is also used as the unique identifier of the subscription
namethe name of the subscriber, for diagnostic purposes
functhe listener method, named OnConfigUpdate by convention.
keysconfig objects, specializations of dynamic_config::Key.
Returns
a concurrent::AsyncEventSubscriberScope controlling the subscription, which should be stored as a member in the subscriber; Unsubscribe should be called explicitly
See also
based on concurrent::AsyncEventSource engine

Definition at line 193 of file source.hpp.


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