Skip to content

Commit 1550273

Browse files
committed
JAVA-1005, JAVA-1009: Added support for minPoolSize, maxIdleTimeMS and maxLifeTimeMS to MongoClientURI
1 parent 9bb0f73 commit 1550273

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/main/com/mongodb/MongoClientURI.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,13 @@
6666
* <li>{@code ssl=true|false}: Whether to connect using SSL.</li>
6767
* <li>{@code connectTimeoutMS=ms}: How long a connection can take to be opened before timing out.</li>
6868
* <li>{@code socketTimeoutMS=ms}: How long a send or receive on a socket can take before timing out.</li>
69+
* <li>{@code maxIdleTimeMS=ms}: Maximum idle time of a pooled connection. A connection that exceeds this limit will be closed</li>
70+
* <li>{@code maxLifeTimeMS=ms}: Maximum life time of a pooled connection. A connection that exceeds this limit will be closed</li>
6971
* </ul>
7072
* <p>Connection pool configuration:</p>
7173
* <ul>
7274
* <li>{@code maxPoolSize=n}: The maximum number of connections in the connection pool.</li>
75+
* <li>{@code minPoolSize=n}: The minimum number of connections in the connection pool.</li>
7376
* <li>{@code waitQueueMultiple=n} : this multiplier, multiplied with the maxPoolSize setting, gives the maximum number of
7477
* threads that may be waiting for a connection to become available from the pool. All further threads will get an
7578
* exception right away.</li>
@@ -262,10 +265,13 @@ public MongoClientURI(String uri, MongoClientOptions.Builder builder) {
262265
static Set<String> allKeys = new HashSet<String>();
263266

264267
static {
268+
generalOptionsKeys.add("minpoolsize");
265269
generalOptionsKeys.add("maxpoolsize");
266270
generalOptionsKeys.add("waitqueuemultiple");
267271
generalOptionsKeys.add("waitqueuetimeoutms");
268272
generalOptionsKeys.add("connecttimeoutms");
273+
generalOptionsKeys.add("maxidletimems");
274+
generalOptionsKeys.add("maxlifetimems");
269275
generalOptionsKeys.add("sockettimeoutms");
270276
generalOptionsKeys.add("sockettimeoutms");
271277
generalOptionsKeys.add("autoconnectretry");
@@ -309,6 +315,12 @@ private MongoClientOptions createOptions(Map<String, List<String>> optionsMap, M
309315

310316
if (key.equals("maxpoolsize")) {
311317
builder.connectionsPerHost(Integer.parseInt(value));
318+
} else if (key.equals("minpoolsize")) {
319+
builder.minConnectionsPerHost(Integer.parseInt(value));
320+
} else if (key.equals("maxidletimems")) {
321+
builder.maxConnectionIdleTime(Integer.parseInt(value));
322+
} else if (key.equals("maxlifetimems")) {
323+
builder.maxConnectionLifeTime(Integer.parseInt(value));
312324
} else if (key.equals("waitqueuemultiple")) {
313325
builder.threadsAllowedToBlockForConnectionMultiplier(Integer.parseInt(value));
314326
} else if (key.equals("waitqueuetimeoutms")) {

src/test/com/mongodb/MongoClientURITest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,21 @@ public void testSSLOption() {
201201
public void testOptions() {
202202
MongoClientURI uAmp = new MongoClientURI("mongodb://localhost/?" +
203203
"maxPoolSize=10&waitQueueMultiple=5&waitQueueTimeoutMS=150&" +
204+
"minPoolSize=7&maxIdleTimeMS=1000&maxLifeTimeMS=2000&" +
204205
"replicaSet=test&" +
205206
"connectTimeoutMS=2500&socketTimeoutMS=5500&autoConnectRetry=true&" +
206207
"slaveOk=true&safe=false&w=1&wtimeout=2500&fsync=true");
207208
assertOnOptions(uAmp.getOptions());
208209
MongoClientURI uSemi = new MongoClientURI("mongodb://localhost/?" +
209210
"maxPoolSize=10;waitQueueMultiple=5;waitQueueTimeoutMS=150;" +
211+
"minPoolSize=7;maxIdleTimeMS=1000;maxLifeTimeMS=2000;" +
210212
"replicaSet=test;" +
211213
"connectTimeoutMS=2500;socketTimeoutMS=5500;autoConnectRetry=true;" +
212214
"slaveOk=true;safe=false;w=1;wtimeout=2500;fsync=true");
213215
assertOnOptions(uSemi.getOptions());
214216
MongoClientURI uMixed = new MongoClientURI("mongodb://localhost/test?" +
215217
"maxPoolSize=10&waitQueueMultiple=5;waitQueueTimeoutMS=150;" +
218+
"minPoolSize=7&maxIdleTimeMS=1000;maxLifeTimeMS=2000&" +
216219
"replicaSet=test;" +
217220
"connectTimeoutMS=2500;socketTimeoutMS=5500&autoConnectRetry=true;" +
218221
"slaveOk=true;safe=false&w=1;wtimeout=2500;fsync=true");
@@ -307,6 +310,9 @@ public void testMultipleIPV6ServersWithPorts() {
307310
@SuppressWarnings("deprecation")
308311
private void assertOnOptions(MongoClientOptions options) {
309312
assertEquals(10, options.getConnectionsPerHost(), 10);
313+
assertEquals(7, options.getMinConnectionsPerHost());
314+
assertEquals(1000, options.getMaxConnectionIdleTime());
315+
assertEquals(2000, options.getMaxConnectionLifeTime());
310316
assertEquals(5, options.getThreadsAllowedToBlockForConnectionMultiplier());
311317
assertEquals(150, options.getMaxWaitTime());
312318
assertEquals(5500, options.getSocketTimeout());

0 commit comments

Comments
 (0)