前言
对服务器来说, 在单条链接达到吞吐下限之前. 更少的链接意味着更少的上下文切换, 更少的内存耗费 (tcp 协定栈内存耗费, 应用层的 buffer). 所以, 在惯例的索引, 分片, 读写拆散之外, 连接池的设计, 对数据库性能也有很重要的影响.
咱们应用的语言没有官网 driver, 连接池的实现采纳了固定大小. 在节点不多的状况下问题不大, 当节点数量越来越多时, 问题变得不容忽视. 于是, 参考一下官网 driver 的实现.
mongo offical driver
offical drivers
c driver
source
https://github.com/mongodb/mo…
commit c99f53b9bcdf5b3756f6c70d8148fbb86015b6f4
document
http://mongoc.org/libmongoc/current/connection-pooling.html
c driver 有 single mode / pooled mode, 显然, 调用线程和链接不会是一对一的关系. 更举荐应用 pooled mode.
参考连接池选项
http://mongoc.org/libmongoc/current/mongoc_uri_t.html#connection-pool-options
c driver 废除了 min_pool_size, 从 mongoc-client-pool.c:333 代码中能够看到
void
mongoc_client_pool_push (mongoc_client_pool_t *pool, mongoc_client_t *client)
{
ENTRY;
BSON_ASSERT (pool);
BSON_ASSERT (client);
bson_mutex_lock (&pool->mutex);
_mongoc_queue_push_head (&pool->queue, client);
if (pool->min_pool_size &&
_mongoc_queue_get_length (&pool->queue) > pool->min_pool_size) {
mongoc_client_t *old_client;
old_client = (mongoc_client_t *) _mongoc_queue_pop_tail (&pool->queue);
if (old_client) {mongoc_client_destroy (old_client);
pool->size--;
}
}
mongoc_cond_signal (&pool->cond);
bson_mutex_unlock (&pool->mutex);
EXIT;
}
min_pool_size 理论含意是, max_pool_size 决定了最大链接数, min_pool_size 决定了同时维持的最小链接数. 显然, 这个在较大的工作负载时, 会导致频繁的链接创立, 断开, 反而让性能降落. 故不举荐应用.
c++ driver
source
https://github.com/mongodb/mo…
commit dfe361bf672809beba0f6164fafad9b088d55fef
document
http://mongocxx.org/mongocxx-v3/connection-pools/
能够看到 minPoolSize 的默认值是 0, 既然创立链接的工夫足够短, 用时再创立是很正当的.
maxPoolSize | The maximum number of clients created by a mongocxx::pool (both in the pool and checked out). The default value is 100. Once it is reached, mongocxx::pool::acquire blocks until another thread returns a client to the pool. |
minPoolSize | Sets a target size for the pool when idle. Once this many clients have been created, there will never be fewer than this many clients in the pool. If additional clients above minPoolSize are created, they will be destroyed when returned to the pool. The default value is“0”, which disables this feature. When disabled, clients are never destroyed. |
没发现 c ++ 有实现闲暇断开. 留神 c ++ driver 重用了 c driver 的连接池代码, 只是用智能指针做了 RAII. 所以对于 min_pool_size 能够参考 c driver 的文档.
java driver
source
https://github.com/mongodb/mo…
6d20b9128bd6966b31c23f7aab681c056aaefc72
document
https://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/MongoClientOptions.html
java driver 写得很罗嗦. 从 api 来看, 连接池选项绝对较丰盛:
- 没有 minPoolSize 只有 maxPoolSize
- 有 idle time 设计.
- MaxConnectionLifeTime SocketTimeout, HeartbeatFrequency 等.
elixir DBCollection
https://hexdocs.pm/db_connection/DBConnection.html
elixir 的 db 连接池为固定大小. 不可用.
总结
- 倡议应用共享式的连接池.
- 通过踢掉闲暇链接, 尽量减少链接数量. 不应用时, pool size 能够是 0.