11#include < storages/postgres/detail/cluster_impl.hpp>
22
33#include < fmt/format.h>
4+ #include < algorithm>
45
56#include < userver/dynamic_config/value.hpp>
67#include < userver/engine/async.hpp>
@@ -379,21 +380,27 @@ void ClusterImpl::SetPoolSettings(const PoolSettings& new_settings) {
379380 {
380381 auto cluster = cluster_settings_.StartWrite ();
381382
383+ cluster->original_min_pool_size = new_settings.min_size ;
382384 cluster->pool_settings = new_settings;
383- auto & settings = cluster->pool_settings ;
384385 if (IsConnlimitModeAuto (*cluster)) {
385386 auto connlimit = connlimit_watchdog_.GetConnlimit ();
386387 if (connlimit > 0 ) {
387- settings.max_size = connlimit;
388- if (settings.min_size > settings.max_size ) {
389- settings.min_size = settings.max_size ;
390- }
388+ AdjustPoolSettings (*cluster, connlimit);
391389 }
392390 }
393391
394392 cluster.Commit ();
395393 }
396394
395+ PropagateSettingsToPools ();
396+ }
397+
398+ void ClusterImpl::AdjustPoolSettings (ExtendedClusterSettings& cluster, std::size_t max_size) {
399+ cluster.pool_settings .max_size = max_size;
400+ cluster.pool_settings .min_size = std::min (cluster.original_min_pool_size , max_size);
401+ }
402+
403+ void ClusterImpl::PropagateSettingsToPools () {
397404 auto td = topology_data_.SharedLock ();
398405 auto cluster_settings = cluster_settings_.Read ();
399406 for (const auto & pool : td->host_pools ) {
@@ -409,18 +416,19 @@ void ClusterImpl::SetTopologySettings(const TopologySettings& settings) {
409416void ClusterImpl::OnConnlimitChanged () {
410417 auto max_size = connlimit_watchdog_.GetConnlimit ();
411418 auto cluster = cluster_settings_.StartWrite ();
419+
412420 if (!IsConnlimitModeAuto (*cluster)) {
413421 return ;
414422 }
415423
416424 if (cluster->pool_settings .max_size == max_size) {
417425 return ;
418426 }
419- cluster->pool_settings .max_size = max_size;
427+ AdjustPoolSettings (*cluster, max_size);
428+
420429 cluster.Commit ();
421430
422- auto cluster_settings = cluster_settings_.Read ();
423- SetPoolSettings (cluster_settings->pool_settings );
431+ PropagateSettingsToPools ();
424432}
425433
426434bool ClusterImpl::IsConnlimitModeAuto (const ClusterSettings& settings) {
@@ -478,6 +486,11 @@ void ClusterImpl::SetDsnList(const DsnList& dsn) {
478486 TESTPOINT (" postgres-new-dsn-list" , {});
479487}
480488
489+ ClusterImpl::ExtendedClusterSettings::ExtendedClusterSettings (const ClusterSettings& settings)
490+ : ClusterSettings(settings),
491+ original_min_pool_size (settings.pool_settings.min_size)
492+ {}
493+
481494} // namespace storages::postgres::detail
482495
483496USERVER_NAMESPACE_END
0 commit comments