MongoDB | maxWaitQueueSize 是不是越大越好?

#130 · closed · 1 comments

View on GitHub ↗

kun-song

https://github.com/brettwooldridge/HikariCP/wiki/About-Pool-Sizing

Comments

kun-song

首先,连接线程池是客户端行为,不同驱动的实现可能不同,在 [Java 驱动](http://mongodb.github.io/mongo-java-driver/3.10/javadoc/?com/mongodb/MongoClientURI.html) 中,与连接池相关的配置有: * `maxPoolSize`:连接池中的最大连接数量; * `waitQueueMultiple`:该配置 * `maxPoolSize` => `maxWaitQueueSize`,计算出等待连接的线程队列的最大长度,若超出限制,则抛出异常,丢弃该线程; * `waitQueueTimeoutMS`:暂无空闲连接时,允许线程等待的最长时间(ms); 对应到 Java 客户端,`maxWaitQueueSize` 计算: ```Java connectionPoolSettings = ConnectionPoolSettings.builder() .minSize(getMinConnectionsPerHost()) .maxSize(getConnectionsPerHost()) .maxWaitQueueSize(getThreadsAllowedToBlockForConnectionMultiplier() * getConnectionsPerHost()) .maxWaitTime(getMaxWaitTime(), MILLISECONDS) .maxConnectionIdleTime(getMaxConnectionIdleTime(), MILLISECONDS) .maxConnectionLifeTime(getMaxConnectionLifeTime(), MILLISECONDS) .build(); ``` 这里 `maxConnectionsPerHost` 对应 `maxPoolSize` 默认 100,`threadsAllowedToBlockForConnectionMultiplier` 对应 `waitQueueMultiple`,默认 5 ```Java The maximum number of connections allowed per host for this MongoClient instance. Those connections will be kept in a pool when idle. Once the pool is exhausted, any operation requiring a connection will block waiting for an available connection. This multiplier, multiplied with the connectionsPerHost setting, gives the maximum number of threads that may be waiting for a connection to become available from the pool. All further threads will get an exception right away. For example if connectionsPerHost is 10 and threadsAllowedToBlockForConnectionMultiplier is 5, then up to 50 threads can wait for a connection.< ```