Merge pull request #114 from skeswa/feature/consistency-in-conn-str

Added consistency parameter to cassandra connection string
This commit is contained in:
Matthias Kadenbach 2016-12-21 20:01:06 -08:00 committed by GitHub
commit 9c7172291e

View File

@ -41,10 +41,12 @@ const (
)
// Cassandra Driver URL format:
// cassandra://host:port/keyspace?protocol=version
// cassandra://host:port/keyspace?protocol=version&consistency=level
//
// Example:
// Examples:
// cassandra://localhost/SpaceOfKeys?protocol=4
// cassandra://localhost/SpaceOfKeys?protocol=4&consistency=all
// cassandra://localhost/SpaceOfKeys?consistency=quorum
func (driver *Driver) Initialize(rawurl string) error {
u, err := url.Parse(rawurl)
@ -53,6 +55,15 @@ func (driver *Driver) Initialize(rawurl string) error {
cluster.Consistency = gocql.All
cluster.Timeout = 1 * time.Minute
if len(u.Query().Get("consistency")) > 0 {
consistency, err := parseConsistency(u.Query().Get("consistency"))
if err != nil {
return err
}
cluster.Consistency = consistency
}
if len(u.Query().Get("protocol")) > 0 {
protoversion, err := strconv.Atoi(u.Query().Get("protocol"))
if err != nil {
@ -169,3 +180,20 @@ func (driver *Driver) Version() (uint64, error) {
func init() {
driver.RegisterDriver("cassandra", &Driver{})
}
// ParseConsistency wraps gocql.ParseConsistency to return an error
// instead of a panicing.
func parseConsistency(consistencyStr string) (consistency gocql.Consistency, err error) {
defer func() {
if r := recover(); r != nil {
var ok bool
err, ok = r.(error)
if !ok {
err = fmt.Errorf("Failed to parse consistency \"%s\": %v", consistencyStr, r)
}
}
}()
consistency = gocql.ParseConsistency(consistencyStr)
return consistency, nil
}