userver: userver/dynamic_config/updates_sink/component.hpp Source File
Loading...
Searching...
No Matches
component.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/dynamic_config/updates_sink/component.hpp
4/// @brief @copybrief components::DynamicConfigUpdatesSinkBase
5
6#include <memory>
7#include <string_view>
8
9#include <userver/components/component_base.hpp>
10#include <userver/dynamic_config/fwd.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace components {
16} // namespace components
17
18namespace dynamic_config::impl {
19void RegisterUpdater(
21 std::string_view sink_component_name,
22 std::string_view updater_component_name
23);
24} // namespace dynamic_config::impl
25
26namespace components {
27
28/// @ingroup userver_base_classes
29///
30/// @brief Base class for components acting as dynamic config updates sinks.
31///
32/// Dynamic config updaters (see components::DynamicConfigClientUpdater for
33/// example) internally look for updates sink object using
34/// dynamic_config::FindUpdatesSink function and use it to store received
35/// updates to the dynamic config. By default, this function returns
36/// components::DynamicConfig itself, however some user-defined components may
37/// inherit from components::DynamicConfigUpdatesSinkBase to apply various
38/// transformations to parameters before storing them to dynamic config.
39///
40/// @warning Unless explicitly stated, descendants of this class expect that
41/// `SetConfig` method calls are serialized.
43public:
44 DynamicConfigUpdatesSinkBase(const components::ComponentConfig&, const components::ComponentContext&);
45
46 ~DynamicConfigUpdatesSinkBase() override;
47
48 /// Called by updaters to store new version of dynamic config.
49 /// @param updater updater name used for logging
50 /// @param config updated dynamic config
51 virtual void SetConfig(std::string_view updater, dynamic_config::DocsMap&& config) = 0;
52
53 /// Called by updaters to store new version of dynamic config.
54 /// Default implementation creates a copy of `config` and then uses it
55 /// in the call to `SetConfig` overload with an rvalue `DocsMap` parameter.
56 /// @param updater updater name used for logging
57 /// @param config updated dynamic config
58 virtual void SetConfig(std::string_view updater, const dynamic_config::DocsMap& config);
59
60 /// Should be called when updater fails to load dynamic config.
61 /// When the service starts up and dynamic config cache file is not found,
62 /// components::DynamicConfig expects updater to provide the current version
63 /// of the dynamic config and blocks all reads until it is received. Updater
64 /// is expected to call this method if it can't load dynamic config
65 /// to interrupt the service startup with proper diagnostics.
66 /// This method should not typically throw any exceptions.
67 /// If the config is already loaded, calling this method should do nothing.
68 /// @param updater updater name used for logging
69 /// @param error error to be outputted to log
70 virtual void NotifyLoadingFailed(std::string_view updater, std::string_view error) = 0;
71
72private:
73 struct UsedByInfo;
74
75 friend void dynamic_config::impl::RegisterUpdater(
77 std::string_view sink_component_name,
78 std::string_view updater_component_name
79 );
80
81 std::unique_ptr<UsedByInfo> used_by_;
82};
83
84} // namespace components
85
86USERVER_NAMESPACE_END