userver: userver/storages/mongo/pool_config.hpp Source File
Loading...
Searching...
No Matches
pool_config.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/mongo/pool_config.hpp
4/// @brief @copybrief storages::mongo::PoolConfig
5
6#include <chrono>
7#include <cstddef>
8#include <optional>
9#include <string>
10
11#include <userver/components/component_fwd.hpp>
12#include <userver/congestion_control/controllers/linear.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace storages::mongo {
17
18enum class StatsVerbosity {
19 kTerse, ///< Only pool stats and read/write overalls by collection
20 kFull, ///< Stats with separate metrics per operation type and label
21 kNone, ///< No stats at all
22};
23
24/// @brief Mongo connection pool options
25///
26/// Dynamic option @ref MONGO_CONNECTION_POOL_SETTINGS
27struct PoolSettings final {
28 /// Default initial connection count
29 static constexpr size_t kDefaultInitialSize = 16;
30 /// Default total connections limit
31 static constexpr size_t kDefaultMaxSize = 128;
32 /// Default idle connections limit
33 static constexpr size_t kDefaultIdleLimit = 64;
34 /// Default establishing connections limit
35 static constexpr size_t kDefaultConnectingLimit = 8;
36
37 /// Initial connection count
38 size_t initial_size = kDefaultInitialSize;
39 /// Total connections limit
40 size_t max_size = kDefaultMaxSize;
41 /// Idle connections limit
42 size_t idle_limit = kDefaultIdleLimit;
43 /// Establishing connections limit
44 size_t connecting_limit = kDefaultConnectingLimit;
45
46 /// @throws InvalidConfigException if pool settings are invalid
47 void Validate(const std::string& pool_id) const;
48};
49
50PoolSettings Parse(const formats::json::Value& config,
51 formats::parse::To<PoolSettings>);
52
53PoolSettings Parse(const yaml_config::YamlConfig& config,
54 formats::parse::To<PoolSettings>);
55
56/// MongoDB connection pool configuration
57struct PoolConfig final {
58 enum class DriverImpl {
59 kMongoCDriver,
60 };
61
62 /// Default connection timeout
63 static constexpr auto kDefaultConnTimeout = std::chrono::seconds{2};
64 /// Default socket timeout
65 static constexpr auto kDefaultSoTimeout = std::chrono::seconds{10};
66 /// Default connection queue timeout
67 static constexpr auto kDefaultQueueTimeout = std::chrono::seconds{1};
68 /// Default pool maintenance period
69 static constexpr auto kDefaultMaintenancePeriod = std::chrono::seconds{15};
70 /// Default application name
71 static constexpr char kDefaultAppName[] = "userver";
72
73 /// @throws InvalidConfigException if the config is invalid
74 void Validate(const std::string& pool_id) const;
75
76 /// Connection (I/O) timeout
77 std::chrono::milliseconds conn_timeout = kDefaultConnTimeout;
78 /// Socket (I/O) timeout
79 std::chrono::milliseconds so_timeout = kDefaultSoTimeout;
80 /// Connection queue wait time
81 std::chrono::milliseconds queue_timeout = kDefaultQueueTimeout;
82 /// settings for connections pool
83 PoolSettings pool_settings{};
84 /// Instance selection latency window override
85 std::optional<std::chrono::milliseconds> local_threshold{};
86 /// Pool maintenance period
87 std::chrono::milliseconds maintenance_period = kDefaultMaintenancePeriod;
88
89 /// Application name (sent to server)
90 std::string app_name = kDefaultAppName;
91 /// Default max replication lag for the pool
93
94 /// Driver implementation to use
95 DriverImpl driver_impl = DriverImpl::kMongoCDriver;
96
97 /// Whether to write detailed stats
99
100 /// Congestion control config
101 congestion_control::v2::LinearController::StaticConfig cc_config;
102};
103
104PoolConfig Parse(const yaml_config::YamlConfig& config,
105 formats::parse::To<PoolConfig>);
106
107} // namespace storages::mongo
108
109USERVER_NAMESPACE_END