-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add optional gocql config for customized cassandra connections #135
Conversation
@lucasmdrs Slight addition to the original implementation. Let me know if I can answer any questions here. |
@fabioserra / @lucasmdrs Gentle ping whenever y'all have a moment :) |
checks/cassandra/check.go
Outdated
var session *gocql.Session | ||
var err error | ||
if config.Session == nil && (len(config.Hosts) < 1 || len(config.Keyspace) < 1) { | ||
return errors.New("cassandra cluster config or keyspace name and hosts are required to initialize cassandra health check") | ||
} | ||
|
||
cluster := gocql.NewCluster(config.Hosts...) | ||
cluster.Keyspace = config.Keyspace | ||
if config.Session != nil { | ||
session = config.Session | ||
} else { | ||
cluster := gocql.NewCluster(config.Hosts...) | ||
cluster.Keyspace = config.Keyspace | ||
session, err = cluster.CreateSession() | ||
defer session.Close() | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic became quite complex and it's a bit unsafe to defer a method before checking the returned error, what do you think about extracting this part into a separate private function that safely returns what you need like this:
func initSession(c Config) (func(), *gocql.Session, error) {
if c.Session != nil {
return func() {
// do not close provided session
}, c.Session, nil
}
if len(c.Hosts) < 1 || len(c.Keyspace) < 1 {
return nil, nil, errors.New("cassandra cluster config or keyspace name and hosts are required to initialize cassandra health check")
}
cluster := gocql.NewCluster(c.Hosts...)
cluster.Keyspace = c.Keyspace
session, err := cluster.CreateSession()
if err != nil {
return nil, nil, err
}
return session.Close, session, err
}
that way you can call it:
shutdown, session, err := initSession(config)
if err != nil {
return fmt.Errorf("cassandra health check failed on connect: %w", err)
}
defer shutdown()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Completely agreed. I refactored per your suggestions and tested it - all's working. Thanks @lucasmdrs
16477ab
to
26cb020
Compare
Thank you for the contribution! 🚀 |
@lucasmdrs Happy to contribute and thank you for open-sourcing this! Any ETA on when this'll get merged? |
Done! I'll merge some other dependencies bump first and release a new version by the end of the week 👍 |
@lucasmdrs Awesome. I'll keep an eye on it. |
@lucasmdrs - very gentle nudge here. Any idea on when the new version with these changes might be released? |
No apologies necessary @lucasmdrs. Thank you! |
For Cassandra connections that might require more information to connect -for example, auth info or SSL options - it's best to use a session established by the user.