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
104template <>
105inline constexpr bool kHasValidate<Mongo> = true;
106
107/// @ingroup userver_components
108///
109/// @brief Dynamically configurable MongoDB client component
110///
111/// Provides access to a dynamically reconfigurable set of MongoDB databases.
112///
113/// ## Dynamic options:
114/// * @ref MONGO_CONGESTION_CONTROL_DATABASES_SETTINGS
115/// * @ref MONGO_CONGESTION_CONTROL_ENABLED
116/// * @ref MONGO_CONGESTION_CONTROL_SETTINGS
117/// * @ref MONGO_CONNECTION_POOL_SETTINGS
118/// * @ref MONGO_DEFAULT_MAX_TIME_MS
119///
120/// ## Static configuration example:
121///
122/// ```
123/// multi-mongo:
124/// appname: userver-sample
125/// conn_timeout: 2s
126/// so_timeout: 10s
127/// queue_timeout: 1s
128/// initial_size: 16
129/// max_size: 128
130/// idle_limit: 64
131/// connecting_limit: 8
132/// local_threshold: 15ms
133/// stats_verbosity: terse
134/// ```
135///
136/// ## Static options:
137/// @include{doc} scripts/docs/en/components_schema/mongo/src/storages/mongo/component_multi.md
138///
139/// `stats_verbosity` accepts one of the following values:
140/// Value | Description
141/// ----- | -----------
142/// terse | Default value, report only cumulative stats and read/write totals
143/// full | Separate metrics for each operation, divided by read preference or write concern
144///
145/// Note that if the components::Secdist component has `update-period` other
146/// than 0, then new connections are created or gracefully closed as the secdist configuration change to new value.
147class MultiMongo : public ComponentBase {
148public:
149 /// @ingroup userver_component_names
150 /// @brief The default name of components::MultiMongo
151 static constexpr std::string_view kName = "multi-mongo";
152
153 /// Component constructor
154 MultiMongo(const ComponentConfig&, const ComponentContext&);
155
156 /// Component destructor
157 ~MultiMongo() override;
158
159 /// @brief Client pool accessor
160 /// @param dbalias name previously passed to `AddPool`
161 /// @throws PoolNotFound if no such database is enabled
162 storages::mongo::PoolPtr GetPool(const std::string& dbalias) const;
163
164 /// @brief Adds a database to the working set by its name.
165 /// Equivalent to
166 /// `NewPoolSet()`-`AddExistingPools()`-`AddPool(dbalias)`-`Activate()`
167 /// @param dbalias name of the database in secdist config
168 void AddPool(std::string dbalias);
169
170 /// @brief Removes the database with the specified name from the working set.
171 /// Equivalent to
172 /// `NewPoolSet()`-`AddExistingPools()`-`RemovePool(dbalias)`-`Activate()`
173 /// @param dbalias name of the database passed to AddPool
174 /// @returns whether the database was in the working set
175 bool RemovePool(const std::string& dbalias);
176
177 /// Creates an empty database set bound to the component
178 storages::mongo::MultiMongo::PoolSet NewPoolSet();
179
180 using PoolSet = storages::mongo::MultiMongo::PoolSet;
181
182 static yaml_config::Schema GetStaticConfigSchema();
183
184private:
185 storages::mongo::MultiMongo multi_mongo_;
186};
187
188template <>
189inline constexpr bool kHasValidate<MultiMongo> = true;
190
191} // namespace components
192
193USERVER_NAMESPACE_END