Golang带有连接池支持的Cassandra客户端

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

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.

huangapple
  • 本文由 发表于 2017年3月6日 13:43:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/42618342.html
匿名

发表评论

匿名网友

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

确定