Skip to content
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

Merged
merged 3 commits into from
Oct 17, 2023

Conversation

boguth
Copy link
Contributor

@boguth boguth commented Oct 5, 2023

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.

@boguth
Copy link
Contributor Author

boguth commented Oct 5, 2023

@lucasmdrs Slight addition to the original implementation. Let me know if I can answer any questions here.

@boguth boguth closed this Oct 5, 2023
@boguth boguth reopened this Oct 5, 2023
@boguth
Copy link
Contributor Author

boguth commented Oct 12, 2023

@fabioserra / @lucasmdrs Gentle ping whenever y'all have a moment :)

Comment on lines 25 to 31
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()
}

Copy link
Contributor

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()

Copy link
Contributor Author

@boguth boguth Oct 16, 2023

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

@boguth boguth force-pushed the cassandra-update-config branch from 16477ab to 26cb020 Compare October 16, 2023 16:52
@boguth boguth requested a review from lucasmdrs October 16, 2023 16:53
@lucasmdrs
Copy link
Contributor

Thank you for the contribution! 🚀

@boguth
Copy link
Contributor Author

boguth commented Oct 16, 2023

@lucasmdrs Happy to contribute and thank you for open-sourcing this! Any ETA on when this'll get merged?

@lucasmdrs lucasmdrs merged commit dce9ffa into hellofresh:master Oct 17, 2023
@lucasmdrs
Copy link
Contributor

Done! :shipit:

I'll merge some other dependencies bump first and release a new version by the end of the week 👍

@boguth
Copy link
Contributor Author

boguth commented Oct 24, 2023

@lucasmdrs Awesome. I'll keep an eye on it.

@boguth
Copy link
Contributor Author

boguth commented Nov 2, 2023

@lucasmdrs - very gentle nudge here. Any idea on when the new version with these changes might be released?

@lucasmdrs
Copy link
Contributor

My apologies @boguth, forgot to notify you, this change was already made available in the release v5.4.0

@boguth
Copy link
Contributor Author

boguth commented Nov 2, 2023

No apologies necessary @lucasmdrs. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants