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

Open
wants to merge 23 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f466c70
✨ adding context support
mfreeman451 Oct 16, 2024
677406b
🔧 updated datasources
mfreeman451 Oct 16, 2024
20bc479
Merge branch 'development' into bug/mongodb_context
mfreeman451 Oct 17, 2024
acf5da6
Merge branch 'development' into bug/mongodb_context
mfreeman451 Oct 17, 2024
19e7ee3
Merge branch 'development' into bug/mongodb_context
mfreeman451 Oct 18, 2024
52426b8
Merge branch 'development' into bug/mongodb_context
vipul-rawat Oct 18, 2024
a325526
Return custom errors
Oct 18, 2024
9592158
updated tests and fixed linter issues
Oct 18, 2024
ca8b6ed
Merge branch 'development' into bug/mongodb_context
mfreeman451 Oct 18, 2024
2dfddc3
updated connect and test to handle when mongodb isnt available for test
Oct 18, 2024
159bc3e
Merge branch 'bug/mongodb_context' of github.com:mfreeman451/gofr int…
Oct 18, 2024
66f37a1
fixing potential issues where res might be nil
Oct 18, 2024
bd2b1c6
updating go.mod
Oct 18, 2024
c6770a2
go mod tidy
Oct 18, 2024
e862fd8
passing context to StartSession
Oct 18, 2024
89195ed
Updating cassandra Connect()
Oct 19, 2024
e2b065f
Merge branch 'development' into bug/mongodb_context
mfreeman451 Oct 19, 2024
f09485e
Merge branch 'development' into bug/mongodb_context
Umang01-hash Oct 23, 2024
48e0c2b
✅ updating external db test
mfreeman451 Oct 25, 2024
eed99a1
Merge branch 'development' into bug/mongodb_context
mfreeman451 Oct 25, 2024
d1352cd
Merge branch 'development' into bug/mongodb_context
mfreeman451 Nov 2, 2024
de57a78
Merge branch 'development' into bug/mongodb_context
mfreeman451 Nov 10, 2024
9150379
gofmt/goimport
Nov 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/gofr/container/datasources.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ type Mongo interface {
CreateCollection(ctx context.Context, name string) error

// StartSession starts a session and provide methods to run commands in a transaction.
StartSession() (any, error)
StartSession(ctx context.Context) (any, error)

HealthChecker
}
Expand Down Expand Up @@ -285,8 +285,8 @@ type provider interface {
// UseTracer sets the tracer for the Cassandra client.
UseTracer(tracer any)

// Connect establishes a connection to Cassandra and registers metrics using the provided configuration when the client was Created.
Connect()
// Connect establishes a connection to a DB and registers metrics using the provided configuration when the client was Created.
Connect(ctx context.Context) error
aryanmehrotra marked this conversation as resolved.
Show resolved Hide resolved
}

type HealthChecker interface {
Expand Down
96 changes: 56 additions & 40 deletions pkg/gofr/container/mock_datasources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pkg/gofr/datasource/cassandra/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ func New(conf Config) *Client {
}

// Connect establishes a connection to Cassandra and registers metrics using the provided configuration when the client was Created.
func (c *Client) Connect() {
func (c *Client) Connect(_ context.Context) error {
c.logger.Logf("connecting to cassandra at %v on port %v to keyspace %v", c.config.Hosts, c.config.Port, c.config.Keyspace)

sess, err := c.cassandra.clusterConfig.createSession()
if err != nil {
c.logger.Error("error connecting to cassandra: ", err)

return
return err
}

cassandraBucktes := []float64{.05, .075, .1, .125, .15, .2, .3, .5, .75, 1, 2, 3, 4, 5, 7.5, 10}
Expand All @@ -77,6 +77,8 @@ func (c *Client) Connect() {
c.logger.Logf("connected to '%s' keyspace at host '%s' and port '%d'", c.config.Keyspace, c.config.Hosts, c.config.Port)

c.cassandra.session = sess

return nil
}

// UseLogger sets the logger for the Cassandra client which asserts the Logger interface.
Expand Down
2 changes: 1 addition & 1 deletion pkg/gofr/datasource/cassandra/cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func Test_Connect(t *testing.T) {

client.cassandra.clusterConfig = mockClusterConfig

client.Connect()
client.Connect(context.Background())

assert.Equal(t, tc.expSession, client.cassandra.session, "TEST[%d], Failed.\n%s", i, tc.desc)
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/gofr/datasource/mongo/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package mongo

import "errors"

var (
// ErrInvalidURI is returned when the MongoDB URI is invalid or cannot be parsed.
ErrInvalidURI = errors.New("invalid MongoDB URI")

// ErrAuthentication is returned when authentication fails.
ErrAuthentication = errors.New("authentication failed")

// ErrDatabaseConnection is returned when the client fails to connect to the specified database.
ErrDatabaseConnection = errors.New("failed to connect to database")

// ErrGenericConnection is returned for general connection issues.
ErrGenericConnection = errors.New("MongoDB connection error")
Comment on lines +6 to +16
Copy link
Contributor

Choose a reason for hiding this comment

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

Here is a suggestion, feel free to consider it or ignore it

Suggested change
// ErrInvalidURI is returned when the MongoDB URI is invalid or cannot be parsed.
ErrInvalidURI = errors.New("invalid MongoDB URI")
// ErrAuthentication is returned when authentication fails.
ErrAuthentication = errors.New("authentication failed")
// ErrDatabaseConnection is returned when the client fails to connect to the specified database.
ErrDatabaseConnection = errors.New("failed to connect to database")
// ErrGenericConnection is returned for general connection issues.
ErrGenericConnection = errors.New("MongoDB connection error")
// ErrInvalidURI is returned when the MongoDB URI is invalid or cannot be parsed.
ErrInvalidURI = fmt.Errorf("%w: invalid MongoDB URI", ErrConnection)
// ErrAuthentication is returned when authentication fails.
ErrAuthentication = fmt.Errorf("%w: authentication failed", ErrConnection)
// ErrDatabaseConnection is returned when the client fails to connect to the specified database.
ErrDatabaseConnection = fmt.Errorf("%w: failed to connect to database", ErrConnection)
// ErrGenericConnection is returned for general connection issues.
ErrConnection = errors.New("MongoDB connection error")

This way the caller can catch all mongodb error with an errors.Is(err, ErrConnection)

)
20 changes: 10 additions & 10 deletions pkg/gofr/datasource/mongo/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ go 1.22

require (
github.com/stretchr/testify v1.9.0
go.mongodb.org/mongo-driver v1.15.1
go.opentelemetry.io/otel v1.30.0
go.opentelemetry.io/otel/trace v1.30.0
go.uber.org/mock v0.4.0
go.mongodb.org/mongo-driver v1.17.1
go.opentelemetry.io/otel v1.31.0
go.opentelemetry.io/otel/trace v1.31.0
go.uber.org/mock v0.5.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 // indirect
go.opentelemetry.io/otel/metric v1.30.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/text v0.15.0 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
go.opentelemetry.io/otel/metric v1.31.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/text v0.19.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading
Loading