Skip to content

Commit cdac79a

Browse files
AchoArnoldCopilot
andcommitted
fix: address PR review comments
- Remove MongoDB URI from Ping error message to prevent credential exposure in logs and error aggregators - Use separate contexts for Ping (10s) and index creation (30s) so they don't share a timeout budget Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1d291f3 commit cdac79a

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

api/pkg/repositories/mongodb.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ func NewMongoDB(uri string) (*mongo.Database, error) {
5656
return nil, stacktrace.Propagate(err, "cannot parse database name from MongoDB URI")
5757
}
5858

59-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
60-
defer cancel()
59+
pingCtx, pingCancel := context.WithTimeout(context.Background(), 10*time.Second)
60+
defer pingCancel()
6161

6262
serverAPI := options.ServerAPI(options.ServerAPIVersion1)
6363
registry := newMongoRegistry()
@@ -72,13 +72,16 @@ func NewMongoDB(uri string) (*mongo.Database, error) {
7272
return nil, stacktrace.Propagate(err, "cannot connect to MongoDB Atlas")
7373
}
7474

75-
if err = client.Ping(ctx, nil); err != nil {
76-
return nil, stacktrace.Propagate(err, fmt.Sprintf("cannot ping MongoDB with URI [%s]", uri))
75+
if err = client.Ping(pingCtx, nil); err != nil {
76+
return nil, stacktrace.Propagate(err, "cannot ping MongoDB Atlas")
7777
}
7878

7979
db := client.Database(dbName)
8080

81-
if err = createMongoIndexes(ctx, db); err != nil {
81+
indexCtx, indexCancel := context.WithTimeout(context.Background(), 30*time.Second)
82+
defer indexCancel()
83+
84+
if err = createMongoIndexes(indexCtx, db); err != nil {
8285
return nil, stacktrace.Propagate(err, "cannot create MongoDB indexes")
8386
}
8487

0 commit comments

Comments
 (0)