Search code examples
gocassandradatastaxgocql

Golang cassandra client with connection pool support


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?


Solution

  • 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.