Couchbase连接池

huangapple go评论83阅读模式
英文:

Couchbase connection pool

问题

我正在使用Couchbase作为我的主要数据库构建一个应用程序。
我希望使应用程序具备足够的可扩展性,以便在某些时候能够同时处理多个请求。

在Go中,如何创建Couchbase的连接池?
Postgres有pgxpool

英文:

I am building an application using couchbase as my primary db.
I want to make the application scalable enough to handle multiple requests at times concurrently.

How do you create connection pools for couchbase in Go?
Postgres has pgxpool.

答案1

得分: 4

我将详细介绍一下gocb的工作原理。在gocb的内部,有一个名为gocbcore的SDK(不支持直接使用gocbcore),它是一个完全异步的API。Gocb提供了对gocbcore的同步API,并且使API更加用户友好。

这意味着,如果您在多个goroutine中发出请求,那么可以同时将多个请求写入网络。这实际上就是gocb批量API的工作原理-https://github.com/couchbase/gocb/blob/master/collection_bulk.go。这两种方法都在https://docs.couchbase.com/go-sdk/current/howtos/concurrent-async-apis.html中有文档记录。

如果您仍然无法获得足够的吞吐量,您可以尝试使用这些方法之一,并通过在连接字符串中使用kv_pool_size查询字符串选项来增加SDK与每个节点建立的连接数,例如couchbases://10.112.212.101?kv_pool_size=2。但我建议只有在上述方法无法提供所需吞吐量时才更改此设置。SDK本身已经被设计为高性能的。

英文:

I'll give a bit more detail about how gocb works. Under the hood of gocb is another SDK called gocbcore (direct usage of gocbcore is not supported) which is a fully asynchronous API. Gocb provides a synchronous API over gocbcore as well as making the API quite a lot more user friendly.

What this means is that if you issue requests across multiple goroutines then you can get multiple requests written to the network at a time. This is effectively how the gocb bulk API works - https://github.com/couchbase/gocb/blob/master/collection_bulk.go. Both of these approaches are documented at https://docs.couchbase.com/go-sdk/current/howtos/concurrent-async-apis.html.

If you still don't get enough throughput then you can look at using one of these approaches alongside increasing the number of connections that the SDK makes to each node by using the kv_pool_size query string option in your connection string, i.e. couchbases://10.112.212.101?kv_pool_size=2 however I'd recommend only changing this if the above approaches are not providing the throughput that you need. The SDK is designed to be highly performant anyway.

答案2

得分: 3

go-couchbase已经有一个连接池机制:conn_pool.go(尽管与之相关的问题有一些,比如issue 91)。

你可以在conn_pool_test.gopools.go中看到它被测试和使用。

dnault评论中指出了更新的couchbase/gocb,它使用Cluster而不是连接池。

英文:

go-couchbase has already have a connection pool mechanism: conn_pool.go (even though there are a few issues linked to it, like issue 91).

You can see it tested in conn_pool_test.go, and in pools.go itself.

dnault points out in the comments to the more recent couchbase/gocb, which does have a Cluster instead of pool of connections.

huangapple
  • 本文由 发表于 2021年10月5日 10:50:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/69444392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定