php mongo 连接池,关于mongodb:谈谈mongo-driver的连接池

前言

对服务器来说, 在单条链接达到吞吐下限之前. 更少的链接意味着更少的上下文切换, 更少的内存耗费(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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值