英文:
Golang cassandra client with connection pool support
问题
我们正在使用gocql
(https://github.com/gocql/gocql)驱动程序从我们的Golang服务器连接到Cassandra。对于每个HTTP请求,我们都会创建一个新的会话并将行插入到Cassandra中。我们觉得为每个请求创建一个会话非常耗费资源。
典型代码
func NewSession() (*gocql.Session, error) {
config := NewClusterConfig()
if config == nil {
return nil, &CassandraError{"Oops!集群初始化失败。"}
}
return config.CreateSession()
}
是否有办法在gocql
或其他用于Golang的Cassandra驱动程序中池化连接?
英文:
We are using gocql
(https://github.com/gocql/gocql) driver to connect to Cassandra from our golang server. For every http request, we are creating a new session and inserting rows into cassandra. We feel it is very much resource intensive to create a session for every request.
Typical Code
func NewSession() (*gocql.Session, error) {
config := NewClusterConfig()
if config == nil {
return nil, &CassandraError{"Oops! Cluster initialization failed."}
}
return config.CreateSession()
}
Is there any way to pool the connections in gocql
or any other cassandra drivers for golang?
答案1
得分: 5
你不需要一个池。创建一个全局的Session
。从https://godoc.org/github.com/gocql/gocql#Session:
> 它可以安全地被多个goroutine并发使用,一个典型的使用场景是拥有一个全局的session对象来与整个Cassandra集群进行交互。
英文:
You don't need a pool. Create a global Session
. From https://godoc.org/github.com/gocql/gocql#Session:
> It's safe for concurrent use by multiple goroutines and a typical usage scenario is to have one global session object to interact with the whole Cassandra cluster.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论