2323#include " Poco/MongoDB/ReadPreference.h"
2424#include " Poco/MongoDB/TopologyDescription.h"
2525#include " Poco/Net/SocketAddress.h"
26+ #include " Poco/Random.h"
2627#include < atomic>
2728#include < chrono>
29+ #include < condition_variable>
2830#include < cstddef>
2931#include < mutex>
3032#include < string>
@@ -93,22 +95,20 @@ class MongoDB_API ReplicaSet
9395 // / Default read preference for this replica set.
9496
9597 unsigned int connectTimeoutSeconds{10 };
96- // / Connection timeout in seconds (default: 10)
98+ // / Connection timeout in seconds (default: 10).
9799 // /
98- // / NOTE: This value is currently unused by ReplicaSet itself. It is intended
99- // / for use by custom SocketFactory implementations. Custom factories can
100- // / access this value via ReplicaSet::configuration() and use it when creating
101- // / sockets. Use ReplicaSet::setSocketFactory() to set a custom factory that
102- // / utilizes this timeout value.
100+ // / Applied when connecting to MongoDB servers during topology monitoring
101+ // / and when creating connections via getConnection()/getPrimaryConnection()/
102+ // / getSecondaryConnection(). When using a custom SocketFactory, the factory
103+ // / is responsible for applying its own connect timeout.
103104
104105 unsigned int socketTimeoutSeconds{30 };
105- // / Socket send/receive timeout in seconds (default: 30)
106+ // / Socket send/receive timeout in seconds (default: 30).
106107 // /
107- // / NOTE: This value is currently unused by ReplicaSet itself. It is intended
108- // / for use by custom SocketFactory implementations. Custom factories can
109- // / access this value via ReplicaSet::configuration() and use it when creating
110- // / sockets. Use ReplicaSet::setSocketFactory() to set a custom factory that
111- // / utilizes this timeout value.
108+ // / Applied as the send and receive timeout on sockets after successful
109+ // / connection. This affects how long sendRequest()/readResponse() will
110+ // / wait before timing out. When using a custom SocketFactory, the factory
111+ // / is responsible for applying its own socket timeout.
112112
113113 unsigned int heartbeatFrequencySeconds{10 };
114114 // / Topology monitoring interval in seconds (default: 10)
@@ -183,16 +183,33 @@ class MongoDB_API ReplicaSet
183183
184184 Connection::Ptr getConnection (const ReadPreference& readPref);
185185 // / Returns a connection to a server matching the read preference.
186+ // / Uses timeouts from Config.
187+ // / Returns null if no suitable server is available.
188+
189+ Connection::Ptr getConnection (const ReadPreference& readPref,
190+ const Poco::Timespan& connectTimeout, const Poco::Timespan& socketTimeout);
191+ // / Returns a connection to a server matching the read preference,
192+ // / using the specified connect and socket timeouts instead of
193+ // / the values from Config.
186194 // / Returns null if no suitable server is available.
187195
188196 Connection::Ptr waitForServerAvailability (const ReadPreference& readPref);
189197 // / Waits for a server to become available for the given read preference.
198+ // / Uses timeouts from Config.
190199 // / This method coordinates waiting between multiple threads - only one thread
191200 // / performs the actual sleep and topology refresh, while others benefit from
192201 // / the refresh done by the first thread.
193202 // / Returns a connection if a server becomes available, or null if still unavailable.
194203 // / Thread-safe: uses internal synchronization to prevent redundant refresh attempts.
195204
205+ Connection::Ptr waitForServerAvailability (const ReadPreference& readPref,
206+ const Poco::Timespan& connectTimeout, const Poco::Timespan& socketTimeout);
207+ // / Waits for a server to become available for the given read preference,
208+ // / using the specified connect and socket timeouts instead of
209+ // / the values from Config.
210+ // / Returns a connection if a server becomes available, or null if still unavailable.
211+ // / Thread-safe: uses internal synchronization to prevent redundant refresh attempts.
212+
196213 Connection::Ptr getPrimaryConnection ();
197214 // / Returns a connection to the primary server.
198215 // / Returns null if no primary is available.
@@ -202,7 +219,7 @@ class MongoDB_API ReplicaSet
202219 // / Returns null if no secondary is available.
203220
204221 [[nodiscard]] Config configuration () const ;
205- // Returns a copy of replica set configuration.
222+ // / Returns a copy of replica set configuration.
206223
207224 [[nodiscard]] TopologyDescription topology () const ;
208225 // / Returns a copy of the current topology description.
@@ -243,13 +260,22 @@ class MongoDB_API ReplicaSet
243260 Connection::Ptr selectServer (const ReadPreference& readPref);
244261 // / Selects a server based on read preference and creates a connection.
245262
263+ Connection::Ptr selectServer (const ReadPreference& readPref,
264+ const Poco::Timespan& connectTimeout, const Poco::Timespan& socketTimeout);
265+ // / Selects a server based on read preference and creates a connection
266+ // / using the specified timeouts.
267+
246268 Connection::Ptr createConnection (const Net::SocketAddress& address);
247269 // / Creates a new connection to the specified address.
248270
249- void updateTopologyFromHello (const Net::SocketAddress& address) noexcept ;
271+ Connection::Ptr createConnection (const Net::SocketAddress& address,
272+ const Poco::Timespan& connectTimeout, const Poco::Timespan& socketTimeout);
273+ // / Creates a new connection to the specified address using the given timeouts.
274+
275+ void updateTopologyFromHello (const Net::SocketAddress& address);
250276 // / Queries a server with 'hello' command and updates topology.
251277
252- void updateTopologyFromAllServers () noexcept ;
278+ void updateTopologyFromAllServers ();
253279 // / Queries all known servers and updates topology.
254280
255281 void parseURI (const std::string& uri);
@@ -264,9 +290,13 @@ class MongoDB_API ReplicaSet
264290 std::thread _monitorThread;
265291 std::atomic<bool > _stopMonitoring{false };
266292 std::atomic<bool > _monitoringActive{false };
293+ std::mutex _monitorMutex;
294+ std::condition_variable _monitorCV;
295+
296+ Poco::Random _random;
267297
268298 std::mutex _serverAvailabilityRetryMutex;
269- std::chrono::steady_clock::time_point _topologyRefreshTime;
299+ std::atomic<std:: chrono::steady_clock::time_point> _topologyRefreshTime;
270300};
271301
272302
0 commit comments