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, formats::parse::To<PoolSettings>);
51
52PoolSettings Parse(const yaml_config::YamlConfig& config, formats::parse::To<PoolSettings>);
53
54/// MongoDB connection pool configuration
55struct PoolConfig final {
56 enum class DriverImpl {
57 kMongoCDriver,
58 };
59
60 /// Default connection timeout
61 static constexpr auto kDefaultConnTimeout = std::chrono::milliseconds{100};
62 /// Default socket timeout
63 static constexpr auto kDefaultSoTimeout = std::chrono::seconds{10};
64 /// Default connection queue timeout
65 static constexpr auto kDefaultQueueTimeout = std::chrono::seconds{1};
66 /// Default pool maintenance period
67 static constexpr auto kDefaultMaintenancePeriod = std::chrono::seconds{15};
68 /// Default application name
69 static constexpr char kDefaultAppName[] = "userver";
70
71 /// @throws InvalidConfigException if the config is invalid
72 void Validate(const std::string& pool_id) const;
73
74 /// Connection (I/O) timeout
75 std::chrono::milliseconds conn_timeout = kDefaultConnTimeout;
76 /// Socket (I/O) timeout
77 std::chrono::milliseconds so_timeout = kDefaultSoTimeout;
78 /// Connection queue wait time
79 std::chrono::milliseconds queue_timeout = kDefaultQueueTimeout;
80 /// settings for connections pool
81 PoolSettings pool_settings{};
82 /// Instance selection latency window override
83 std::optional<std::chrono::milliseconds> local_threshold{};
84 /// Pool maintenance period
85 std::chrono::milliseconds maintenance_period = kDefaultMaintenancePeriod;
86
87 /// Application name (sent to server)
88 std::string app_name = kDefaultAppName;
89 /// Default max replication lag for the pool
91
92 /// Driver implementation to use
93 DriverImpl driver_impl = DriverImpl::kMongoCDriver;
94
95 /// Whether to write detailed stats
97
98 /// Congestion control config
99 congestion_control::v2::LinearController::StaticConfig cc_config;
100};
101
102PoolConfig Parse(const yaml_config::YamlConfig& config, formats::parse::To<PoolConfig>);
103
104} // namespace storages::mongo
105
106USERVER_NAMESPACE_END