// New db adaptor func NewAdaptor(urls []string, username, password string) *Adaptor { cluster := gocql.NewCluster(urls...) cluster.Keyspace = "events" cluster.ProtoVersion = 3 // cluster.Authenticator = gocql.PasswordAuthenticator{ // Username: username, // Password: password, // } session, err := cluster.CreateSession() if err != nil { log.Fatalf("Cassandra conn error: %s", err.Error()) } qe := gocassa.GoCQLSessionToQueryExecutor(session) conn := gocassa.NewConnection(qe) gocassaSession := conn.KeySpace("events") return &Adaptor{ Session: gocassaSession, urls: urls, eventTimeDateRange: 7 * 24 * time.Hour, Username: username, Password: password, } }
func (a *Adaptor) ReestablishConnection() { log.Println("Reestablishing cql connection") cluster := gocql.NewCluster(a.urls[0]) cluster.ProtoVersion = 3 cluster.Authenticator = gocql.PasswordAuthenticator{ Username: a.Username, Password: a.Password, } session, err := cluster.CreateSession() if err != nil { log.Fatalf("Cassandra conn error: %s", err.Error()) } qe := gocassa.GoCQLSessionToQueryExecutor(session) a.Session = gocassa.NewConnection(qe).KeySpace("events") }
func gocqlConnector(ks string) gocassa.Connection { ksConnectionsMtx.RLock() conn, ok := ksConnections[ks] ksConnectionsMtx.RUnlock() if ok { return conn } ksConnectionsMtx.Lock() defer ksConnectionsMtx.Unlock() if conn, ok = ksConnections[ks]; !ok { // Guard against race conn = gocassa.NewConnection(&gocqlExecutor{ ks: ks, }) ksConnections[ks] = conn } return conn }