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

Adding context to database provider #1118

Closed

Conversation

mfreeman451
Copy link
Contributor

@mfreeman451 mfreeman451 commented Oct 17, 2024

Pull Request Template

Description:

  • Added the ability to pass a context down through the DB provider to the Connect() call
  • Connect() now returns an error.
  • Currently the Connect() interface doesn't accept a context, invalid connections to databases will not be known.

Breaking Changes (if applicable):

  • Connect() calls in the DB provider will now return an error and require a context to be passed.
  • This breaks the ability to daisy chain method calls.

Additional Information:

Checklist:

  • I have formatted my code using goimport and golangci-lint.
  • All new code is covered by unit tests.
  • This PR does not decrease the overall code coverage.
  • I have reviewed the code comments and documentation for clarity.

Thank you for your contribution!

@mfreeman451 mfreeman451 marked this pull request as draft October 17, 2024 00:58
@mfreeman451 mfreeman451 marked this pull request as ready for review October 17, 2024 01:01
@vipul-rawat
Copy link
Member

Hey @mfreeman451, the mocks have not been updated, but before that can we discuss some approaches and implementations?

I feel like AddMongo, AddClickhouse, etc... should not return any error but log the error, as we do with the SQL, Redis, etc...

One other train of thought would be to FATAL log the errors, as it would panic the code even if it starts as the handler might expect the databases to be connected.

@aryanmehrotra
Copy link
Member

@mfreeman451 can you fix the reveiw comments?
We would not want to keep a PR open for more than 2 weeks - for better maintainability.

pkg/gofr/datasource/cassandra/cassandra.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/mongo/mongo.go Outdated Show resolved Hide resolved
Comment on lines 139 to 141
func (*Client) isTimeoutError(err error) bool {
return strings.Contains(err.Error(), "connection timeout") || mongo.IsTimeout(err)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pkg/gofr/datasource/mongo/mongo.go Show resolved Hide resolved
mfreeman451 and others added 10 commits December 12, 2024 18:58
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
@mfreeman451
Copy link
Contributor Author

cleaned up and updated

pkg/gofr/datasource/mongo/mongo.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/mongo/mongo.go Outdated Show resolved Hide resolved
pkg/gofr/external_db.go Outdated Show resolved Hide resolved
mfreeman451 and others added 4 commits December 14, 2024 22:10
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
@coolwednesday
Copy link
Contributor

coolwednesday commented Jan 7, 2025

Hey @mfreeman451,

I was going through the code and I believe the current implementation might introduce a breaking change. To prevent that, here's what I suggest:

  • Let's focus solely on MongoDB for now. We should avoid making changes in other datasources for now. Please revert those changes.

  • We can create an interface that embeds the existing provider interface, say providerWithContext and introduces an additional method, ConnectWithContext(ctx context.Context). This can help us to handle, only MongoDB-specific logic without affecting other providers in this PR.

  • Replace the current provider implementation with this new interface for MongoProvider, enabling the optional use of the ConnectWithContext method.

  • In externaldb.go, where we are injecting the MongoDB provider using AddMongo, we will replace the call to Connect() with ConnectWithContext(ctx context.Context).

  • To handle context passing efficiently, we can use the functional options pattern. This way, context can be optionally passed to the connection method without breaking the existing flow.

I believe the approach I suggested is one way to address this. However, if you have a better or more efficient solution, feel free to go ahead and use it to resolve the issue. I’m open to exploring alternatives that might work better for the use case.

Let me know your thoughts on this!

@Umang01-hash
Copy link
Contributor

@mfreeman451 are you proceeding towards the review-changes suggested by @coolwednesday ?

@aryanmehrotra
Copy link
Member

aryanmehrotra commented Jan 20, 2025

Please reopen once the changes are fixed and comments are resolved as it has been more than 2 weeks of last activity.

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.

mongo driver doesnt support contexts
7 participants