userver: userver/storages/mongo/component.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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/loggable_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#include <userver/utils/statistics/entry.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace components {
15
16// clang-format off
17
18/// @ingroup userver_components
19///
20/// @brief MongoDB client component
21///
22/// Provides access to a MongoDB database.
23///
24/// ## Dynamic options:
25/// * @ref MONGO_DEFAULT_MAX_TIME_MS
26///
27/// ## Static configuration example:
28///
29/// ```
30/// mongo-taxi:
31/// dbalias: taxi
32/// appname: userver-sample
33/// conn_timeout: 2s
34/// so_timeout: 10s
35/// queue_timeout: 1s
36/// initial_size: 16
37/// max_size: 128
38/// idle_limit: 64
39/// connecting_limit: 8
40/// local_threshold: 15ms
41/// maintenance_period: 15s
42/// stats_verbosity: terse
43/// ```
44/// You must specify one of `dbalias` or `dbconnection`.
45///
46/// ## Static options:
47/// Name | Description | Default value
48/// ---- | ----------- | -------------
49/// dbalias | name of the database in secdist config (if available) | --
50/// dbconnection | connection string (used if no dbalias specified) | --
51/// appname | application name for the DB server | userver
52/// conn_timeout | connection timeout | 2s
53/// so_timeout | socket timeout | 10s
54/// queue_timeout | max connection queue wait time | 1s
55/// initial_size | number of connections created initially | 16
56/// max_size | limit for total connections number | 128
57/// idle_limit | limit for idle connections number | 64
58/// connecting_limit | limit for establishing connections number | 8
59/// local_threshold | latency window for instance selection | mongodb default
60/// max_replication_lag | replication lag limit for usable secondaries, min. 90s | -
61/// maintenance_period | pool maintenance period (idle connections pruning etc.) | 15s
62/// stats_verbosity | changes the granularity of reported metrics | 'terse'
63/// dns_resolver | server hostname resolver type (getaddrinfo or async) | 'async'
64///
65/// `stats_verbosity` accepts one of the following values:
66/// Value | Description
67/// ----- | -----------
68/// terse | Default value, report only cumulative stats and read/write totals
69/// full | Separate metrics for each operation, divided by read preference or write concern
70
71// clang-format on
72
74 public:
75 /// Component constructor
76 Mongo(const ComponentConfig&, const ComponentContext&);
77
78 /// Component destructor
79 ~Mongo() override;
80
81 /// Client pool accessor
82 storages::mongo::PoolPtr GetPool() const;
83
84 static yaml_config::Schema GetStaticConfigSchema();
85
86 private:
87 storages::mongo::PoolPtr pool_;
88 utils::statistics::Entry statistics_holder_;
89};
90
91template <>
92inline constexpr bool kHasValidate<Mongo> = true;
93
94// clang-format off
95
96/// @ingroup userver_components
97///
98/// @brief Dynamically configurable MongoDB client component
99///
100/// Provides access to a dynamically reconfigurable set of MongoDB databases.
101///
102/// ## Dynamic options:
103/// * @ref MONGO_DEFAULT_MAX_TIME_MS
104///
105/// ## Static configuration example:
106///
107/// ```
108/// multi-mongo:
109/// appname: userver-sample
110/// conn_timeout: 2s
111/// so_timeout: 10s
112/// queue_timeout: 1s
113/// initial_size: 16
114/// max_size: 128
115/// idle_limit: 64
116/// connecting_limit: 8
117/// local_threshold: 15ms
118/// stats_verbosity: terse
119/// ```
120///
121/// ## Static options:
122/// Name | Description | Default value
123/// ---- | ----------- | -------------
124/// appname | application name for the DB server | userver
125/// conn_timeout | connection timeout | 2s
126/// so_timeout | socket timeout | 10s
127/// queue_timeout | max connection queue wait time | 1s
128/// initial_size | number of connections created initially (per database) | 16
129/// max_size | limit for total connections number (per database) | 128
130/// idle_limit | limit for idle connections number (per database) | 64
131/// connecting_limit | limit for establishing connections number (per database) | 8
132/// local_threshold | latency window for instance selection | mongodb default
133/// max_replication_lag | replication lag limit for usable secondaries, min. 90s | -
134/// stats_verbosity | changes the granularity of reported metrics | 'terse'
135/// dns_resolver | server hostname resolver type (getaddrinfo or async) | 'async'
136///
137/// `stats_verbosity` accepts one of the following values:
138/// Value | Description
139/// ----- | -----------
140/// terse | Default value, report only cumulative stats and read/write totals
141/// full | Separate metrics for each operation, divided by read preference or write concern
142
143// clang-format on
144
146 public:
147 /// @ingroup userver_component_names
148 /// @brief The default name of components::MultiMongo
149 static constexpr std::string_view kName = "multi-mongo";
150
151 /// Component constructor
152 MultiMongo(const ComponentConfig&, const ComponentContext&);
153
154 /// Component destructor
155 ~MultiMongo() override;
156
157 /// @brief Client pool accessor
158 /// @param dbalias name previously passed to `AddPool`
159 /// @throws PoolNotFound if no such database is enabled
160 storages::mongo::PoolPtr GetPool(const std::string& dbalias) const;
161
162 /// @brief Adds a database to the working set by its name.
163 /// Equivalent to
164 /// `NewPoolSet()`-`AddExistingPools()`-`AddPool(dbalias)`-`Activate()`
165 /// @param dbalias name of the database in secdist config
166 void AddPool(std::string dbalias);
167
168 /// @brief Removes the database with the specified name from the working set.
169 /// Equivalent to
170 /// `NewPoolSet()`-`AddExistingPools()`-`RemovePool(dbalias)`-`Activate()`
171 /// @param dbalias name of the database passed to AddPool
172 /// @returns whether the database was in the working set
173 bool RemovePool(const std::string& dbalias);
174
175 /// Creates an empty database set bound to the component
176 storages::mongo::MultiMongo::PoolSet NewPoolSet();
177
178 using PoolSet = storages::mongo::MultiMongo::PoolSet;
179
180 static yaml_config::Schema GetStaticConfigSchema();
181
182 private:
183 storages::mongo::MultiMongo multi_mongo_;
184 utils::statistics::Entry statistics_holder_;
185};
186
187template <>
188inline constexpr bool kHasValidate<MultiMongo> = true;
189
190} // namespace components
191
192USERVER_NAMESPACE_END