From e43d2e3dee018ed342638378c17f3664bf97322d Mon Sep 17 00:00:00 2001 From: Matej Kenda Date: Thu, 4 Dec 2025 13:20:08 +0100 Subject: [PATCH] enh(MongoDB): Updates to use SSL socket factory. --- MongoDB/include/Poco/MongoDB/ReplicaSet.h | 24 ++++++++++++++++++++++- MongoDB/src/ReplicaSet.cpp | 21 +++++++++++++++----- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/MongoDB/include/Poco/MongoDB/ReplicaSet.h b/MongoDB/include/Poco/MongoDB/ReplicaSet.h index 5eac904c8..8f7d7c0c3 100644 --- a/MongoDB/include/Poco/MongoDB/ReplicaSet.h +++ b/MongoDB/include/Poco/MongoDB/ReplicaSet.h @@ -90,9 +90,21 @@ public: unsigned int connectTimeoutSeconds{10}; /// Connection timeout in seconds (default: 10) + /// + /// NOTE: This value is currently unused by ReplicaSet itself. It is intended + /// for use by custom SocketFactory implementations. Custom factories can + /// access this value via ReplicaSet::configuration() and use it when creating + /// sockets. Use ReplicaSet::setSocketFactory() to set a custom factory that + /// utilizes this timeout value. unsigned int socketTimeoutSeconds{30}; /// Socket send/receive timeout in seconds (default: 30) + /// + /// NOTE: This value is currently unused by ReplicaSet itself. It is intended + /// for use by custom SocketFactory implementations. Custom factories can + /// access this value via ReplicaSet::configuration() and use it when creating + /// sockets. Use ReplicaSet::setSocketFactory() to set a custom factory that + /// utilizes this timeout value. unsigned int heartbeatFrequencySeconds{10}; /// Topology monitoring interval in seconds (default: 10) @@ -107,7 +119,9 @@ public: /// Enable background topology monitoring (default: true) Connection::SocketFactory* socketFactory{nullptr}; - /// Optional socket factory for SSL/TLS connections + /// Optional socket factory for SSL/TLS connections. + /// Can be set via config or later using setSocketFactory(). + /// Custom factories can access timeout config via ReplicaSet::configuration(). Logger::Ptr logger; /// Optional logger to write important information about replica set activity @@ -174,6 +188,14 @@ public: void setLogger(Logger::Ptr logger); /// Sets the logger to log important replica set activity. + void setSocketFactory(Connection::SocketFactory* factory); + /// Sets the socket factory for creating connections. + /// The factory can access timeout configuration via configuration().connectTimeoutSeconds + /// and configuration().socketTimeoutSeconds. + /// + /// Example: + /// rs.setSocketFactory(&myCustomFactory); + void setReadPreference(const ReadPreference& pref); /// Sets the default read preference. diff --git a/MongoDB/src/ReplicaSet.cpp b/MongoDB/src/ReplicaSet.cpp index bbc384558..c70d6660e 100644 --- a/MongoDB/src/ReplicaSet.cpp +++ b/MongoDB/src/ReplicaSet.cpp @@ -196,6 +196,13 @@ void ReplicaSet::setLogger(Logger::Ptr logger) } +void ReplicaSet::setSocketFactory(Connection::SocketFactory* factory) +{ + std::lock_guard lock(_mutex); + _config.socketFactory = factory; +} + + void ReplicaSet::setReadPreference(const ReadPreference& pref) { std::lock_guard lock(_mutex); @@ -290,7 +297,9 @@ Connection::Ptr ReplicaSet::createConnection(const Net::SocketAddress& address) if (_config.socketFactory != nullptr) { // Use custom socket factory (e.g., for SSL/TLS) - // Note: Socket timeouts should be configured in the SocketFactory + // Custom factories can be set via Config or using setSocketFactory(). + // They can access timeout values via configuration().connectTimeoutSeconds + // and configuration().socketTimeoutSeconds to properly configure sockets. conn->connect(address.toString(), *_config.socketFactory); } else @@ -298,8 +307,8 @@ Connection::Ptr ReplicaSet::createConnection(const Net::SocketAddress& address) conn->connect(address); } - // Note: Socket timeouts are configured via URI or during socket creation - // Connection class doesn't expose socket() accessor + // Note: Connection class doesn't expose socket() accessor, so socket timeouts + // must be configured during socket creation via custom SocketFactory. return conn; } @@ -324,6 +333,8 @@ void ReplicaSet::updateTopologyFromHello(const Net::SocketAddress& address) noex if (_config.socketFactory != nullptr) { + // Custom factories can be set via Config or using setSocketFactory(). + // They can access timeout values via configuration() to configure sockets. conn->connect(address.toString(), *_config.socketFactory); } else @@ -331,8 +342,8 @@ void ReplicaSet::updateTopologyFromHello(const Net::SocketAddress& address) noex conn->connect(address); } - // Note: Socket timeouts are configured via URI or during socket creation - // Connection class doesn't expose socket() accessor + // Note: Connection class doesn't expose socket() accessor, so socket timeouts + // must be configured during socket creation via custom SocketFactory. // Send hello command OpMsgMessage request("admin", "");