userver: userver/dynamic_config/storage/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/storage/component.hpp
4/// @brief @copybrief components::DynamicConfig
5
6#include <memory>
7#include <string_view>
8#include <type_traits>
9
10#include <userver/components/loggable_component_base.hpp>
11#include <userver/concurrent/async_event_source.hpp>
12#include <userver/dynamic_config/snapshot.hpp>
13#include <userver/dynamic_config/source.hpp>
14#include <userver/dynamic_config/updates_sink/component.hpp>
15#include <userver/utils/fast_pimpl.hpp>
16
17USERVER_NAMESPACE_BEGIN
18
19namespace components {
20
21// clang-format off
22
23/// @ingroup userver_components
24///
25/// @brief Component that stores the
26/// @ref scripts/docs/en/userver/dynamic_config.md "dynamic config".
27///
28/// Note that the service with `updates-enabled: true` and without
29/// configs cache requires successful update to start. See
30/// @ref dynamic_config_fallback for details and explanation.
31///
32/// ## Behavior on missing configs
33///
34/// If a config variable is entirely missing the fetched config, the value from
35/// `dynamic_config_fallback.json` is used (see `fallback-path` static config
36/// option).
37///
38/// ## Behavior on config parsing failure
39///
40/// If a config variable from the fetched config fails to be parsed (the parser
41/// fails with an exception), then the whole config update fails. It means that:
42///
43/// - If the service is just starting, it will shut down
44/// - If the service is already running, the config updates will stop until
45/// the config in the config service changes to a valid one. You can
46/// monitor this situation using the metric at path
47/// `cache.any.time.time-from-last-successful-start-ms`
48///
49/// ## Static options
50///
51/// Name | Description | Default value
52/// ---- | ----------- | -------------
53/// updates-enabled | should be set to 'true' if there is an updater component | false
54/// defaults | overrides the defaults from dynamic_config::Key definitions in code | {}
55/// fallback-path | a path to the fallback config | defaults are taken from dynamic_config::Key definitions
56/// fs-cache-path | path to the file to read and dump a config cache; set to empty string to disable reading and dumping configs to FS | cache is disabled
57/// fs-task-processor | name of the task processor to run the blocking file write operations | required if `fs-cache-path` is present
58///
59/// ## Static configuration example:
60///
61/// @snippet components/common_component_list_test.cpp Sample dynamic config component config
62///
63/// ## Usage example:
64/// @snippet components/component_sample_test.cpp Sample user component runtime config source
65
66// clang-format on
67class DynamicConfig final : public DynamicConfigUpdatesSinkBase {
68 public:
69 /// @ingroup userver_component_names
70 /// @brief The default name of components::DynamicConfig
71 static constexpr std::string_view kName = "dynamic-config";
72
73 class NoblockSubscriber;
74
75 DynamicConfig(const ComponentConfig&, const ComponentContext&);
76 ~DynamicConfig() override;
77
78 /// Use `dynamic_config::Source` to get up-to-date config values, or to do
79 /// something special on config updates
80 dynamic_config::Source GetSource();
81
82 /// Get config defaults with overrides applied. Useful in the implementation
83 /// of custom config clients. Most code does not need to deal with these
84 const dynamic_config::DocsMap& GetDefaultDocsMap() const;
85
86 static yaml_config::Schema GetStaticConfigSchema();
87
88 private:
89 void OnLoadingCancelled() override;
90
91 void SetConfig(std::string_view updater,
92 dynamic_config::DocsMap&& value) override;
93
94 void SetConfig(std::string_view updater,
95 const dynamic_config::DocsMap& value) override;
96
97 void NotifyLoadingFailed(std::string_view updater,
98 std::string_view error) override;
99
100 class Impl;
101 std::unique_ptr<Impl> impl_;
102};
103
104/// @brief Allows to subscribe to `DynamicConfig` updates without waiting for
105/// the first update to complete. Primarily intended for internal use.
106class DynamicConfig::NoblockSubscriber final {
107 public:
108 explicit NoblockSubscriber(DynamicConfig& config_component) noexcept;
109
110 NoblockSubscriber(NoblockSubscriber&&) = delete;
111 NoblockSubscriber& operator=(NoblockSubscriber&&) = delete;
112
113 concurrent::AsyncEventSource<const dynamic_config::Snapshot&>&
114 GetEventSource() noexcept;
115
116 private:
117 DynamicConfig& config_component_;
118};
119
120template <>
121inline constexpr bool kHasValidate<DynamicConfig> = true;
122
123template <>
124inline constexpr auto kConfigFileMode<DynamicConfig> =
126
127} // namespace components
128
129USERVER_NAMESPACE_END