userver: userver/storages/mongo/component.hpp Source File
Loading...
Searching...
No Matches
component.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/mongo/component.hpp
4/// @brief @copybrief components::Mongo
5
6#include <userver/components/component_base.hpp>
7#include <userver/storages/mongo/multi_mongo.hpp>
8#include <userver/storages/mongo/pool.hpp>
9#include <userver/storages/secdist/component.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace components {
14
15/// @ingroup userver_components
16///
17/// @brief MongoDB client component
18///
19/// Provides access to a MongoDB database.
20///
21/// ## Dynamic options:
22/// * @ref MONGO_CONGESTION_CONTROL_DATABASES_SETTINGS
23/// * @ref MONGO_CONGESTION_CONTROL_ENABLED
24/// * @ref MONGO_CONGESTION_CONTROL_SETTINGS
25/// * @ref MONGO_CONNECTION_POOL_SETTINGS
26/// * @ref MONGO_DEFAULT_MAX_TIME_MS
27///
28/// ## Static configuration example:
29///
30/// ```
31/// mongo-taxi:
32/// dbalias: taxi
33/// appname: userver-sample
34/// conn_timeout: 2s
35/// so_timeout: 10s
36/// queue_timeout: 1s
37/// initial_size: 16
38/// max_size: 128
39/// idle_limit: 64
40/// connecting_limit: 8
41/// local_threshold: 15ms
42/// maintenance_period: 15s
43/// stats_verbosity: terse
44/// ```
45/// You must specify one of `dbalias` or `dbconnection`.
46///
47/// ## Static options of components::Mongo :
48/// @include{doc} scripts/docs/en/components_schema/mongo/src/storages/mongo/component.md
49///
50/// Options inherited from @ref components::MultiMongo
51/// @include{doc} scripts/docs/en/components_schema/mongo/src/storages/mongo/component_multi.md
52///
53/// Options inherited from @ref components::ComponentBase :
54/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
55///
56/// `stats_verbosity` accepts one of the following values:
57/// Value | Description
58/// ----- | -----------
59/// terse | Default value, report only cumulative stats and read/write totals
60/// full | Separate metrics for each operation, divided by read preference or write concern
61///
62/// It is a common practice to provide a database connection string via
63/// environment variables. To retrieve a value from the environment use
64/// `dbconnection#env: THE_ENV_VARIABLE_WITH_CONNECTION_STRING` as described
65/// in yaml_config::YamlConfig.
66///
67/// Note that if the `dbalias` option is provided and the components::Secdist component has `update-period` other
68/// than 0, then new connections are created or gracefully closed as the secdist configuration change to new value.
69///
70/// ## Secdist format
71///
72/// If a `dbalias` option is provided, for example
73/// `dbalias: some_name_of_your_database`, then the Secdist entry for that alias
74/// should look like following:
75/// @code{.json}
76/// {
77/// "mongo_settings": {
78/// "some_name_of_your_database": {
79/// "uri": "mongodb://user:password@host:port/database_name"
80/// }
81/// }
82/// }
83/// @endcode
84class Mongo : public ComponentBase {
85public:
86 /// Component constructor
87 Mongo(const ComponentConfig&, const ComponentContext&);
88
89 /// Component destructor
90 ~Mongo() override;
91
92 /// Client pool accessor
93 storages::mongo::PoolPtr GetPool() const;
94
95 static yaml_config::Schema GetStaticConfigSchema();
96
97private:
98 void OnSecdistUpdate(const storages::secdist::SecdistConfig& config);
99
100 std::string dbalias_;
101 storages::mongo::PoolPtr pool_;
102
103 // Subscriptions must be the last fields.
104 concurrent::AsyncEventSubscriberScope secdist_subscriber_;
105};
106
107template <>
108inline constexpr bool kHasValidate<Mongo> = true;
109
110/// @ingroup userver_components
111///
112/// @brief Dynamically configurable MongoDB client component
113///
114/// Provides access to a dynamically reconfigurable set of MongoDB databases.
115///
116/// ## Dynamic options:
117/// * @ref MONGO_CONGESTION_CONTROL_DATABASES_SETTINGS
118/// * @ref MONGO_CONGESTION_CONTROL_ENABLED
119/// * @ref MONGO_CONGESTION_CONTROL_SETTINGS
120/// * @ref MONGO_CONNECTION_POOL_SETTINGS
121/// * @ref MONGO_DEFAULT_MAX_TIME_MS
122///
123/// ## Static configuration example:
124///
125/// ```
126/// multi-mongo:
127/// appname: userver-sample
128/// conn_timeout: 2s
129/// so_timeout: 10s
130/// queue_timeout: 1s
131/// initial_size: 16
132/// max_size: 128
133/// idle_limit: 64
134/// connecting_limit: 8
135/// local_threshold: 15ms
136/// stats_verbosity: terse
137/// ```
138///
139/// ## Static options:
140/// @include{doc} scripts/docs/en/components_schema/mongo/src/storages/mongo/component_multi.md
141///
142/// `stats_verbosity` accepts one of the following values:
143/// Value | Description
144/// ----- | -----------
145/// terse | Default value, report only cumulative stats and read/write totals
146/// full | Separate metrics for each operation, divided by read preference or write concern
147///
148/// Note that if the components::Secdist component has `update-period` other
149/// than 0, then new connections are created or gracefully closed as the secdist configuration change to new value.
150class MultiMongo : public ComponentBase {
151public:
152 /// @ingroup userver_component_names
153 /// @brief The default name of components::MultiMongo
154 static constexpr std::string_view kName = "multi-mongo";
155
156 /// Component constructor
157 MultiMongo(const ComponentConfig&, const ComponentContext&);
158
159 /// Component destructor
160 ~MultiMongo() override;
161
162 /// @brief Client pool accessor
163 /// @param dbalias name previously passed to `AddPool`
164 /// @throws PoolNotFound if no such database is enabled
165 storages::mongo::PoolPtr GetPool(const std::string& dbalias) const;
166
167 /// @brief Adds a database to the working set by its name.
168 /// Equivalent to
169 /// `NewPoolSet()`-`AddExistingPools()`-`AddPool(dbalias)`-`Activate()`
170 /// @param dbalias name of the database in secdist config
171 void AddPool(std::string dbalias);
172
173 /// @brief Removes the database with the specified name from the working set.
174 /// Equivalent to
175 /// `NewPoolSet()`-`AddExistingPools()`-`RemovePool(dbalias)`-`Activate()`
176 /// @param dbalias name of the database passed to AddPool
177 /// @returns whether the database was in the working set
178 bool RemovePool(const std::string& dbalias);
179
180 /// Creates an empty database set bound to the component
181 storages::mongo::MultiMongo::PoolSet NewPoolSet();
182
183 using PoolSet = storages::mongo::MultiMongo::PoolSet;
184
185 static yaml_config::Schema GetStaticConfigSchema();
186
187private:
188 storages::mongo::MultiMongo multi_mongo_;
189};
190
191template <>
192inline constexpr bool kHasValidate<MultiMongo> = true;
193
194} // namespace components
195
196USERVER_NAMESPACE_END