Skip to content

Commit f9a6e0e

Browse files
authored
Merge pull request #141 from gifflet/fix/mongodb-reconnection
Enhance MongoDB Connection Management with Reconnection Logic
2 parents dd3e829 + f6f5f34 commit f9a6e0e

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed

common/db/db.go

+50-13
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ import (
2020
log "github.com/sirupsen/logrus"
2121
)
2222

23-
var mongoClient *mongo.Client
24-
var mongoClientCtx context.Context
25-
var mongoClientCtxCancel context.CancelFunc
23+
var (
24+
mongoClient *mongo.Client
25+
mongoClientCtx context.Context
26+
mongoClientCtxCancel context.CancelFunc
27+
connectionString string
28+
)
2629

2730
func InitMongoClient(mongoDb string) {
2831
var err error
29-
connectionString := "mongodb://" + mongoDb + "/?keepAlive=true"
32+
connectionString = "mongodb://" + mongoDb + "/?keepAlive=true"
3033

3134
// Set up a context for the connection.
3235
mongoClientCtx, mongoClientCtxCancel = context.WithCancel(context.Background())
@@ -66,20 +69,54 @@ func CloseMongoConn() {
6669
func checkDBConnection() {
6770
errorCounter := 0
6871
for {
69-
if errorCounter < 10 {
70-
time.Sleep(1 * time.Second)
71-
err := mongoClient.Ping(mongoClientCtx, nil)
72-
if err != nil {
73-
fmt.Println("FAILED PINGING MONGO")
74-
errorCounter++
75-
continue
72+
time.Sleep(1 * time.Second)
73+
err := mongoClient.Ping(mongoClientCtx, nil)
74+
75+
if err != nil {
76+
errorCounter++
77+
log.WithFields(log.Fields{
78+
"error_count": errorCounter,
79+
"error": err,
80+
}).Warn("Failed to ping MongoDB")
81+
82+
if err := reconnectMongo(); err != nil {
83+
log.WithFields(log.Fields{
84+
"error_count": errorCounter,
85+
"error": err,
86+
}).Error("Failed to reconnect to MongoDB")
87+
88+
if errorCounter >= 30 {
89+
log.Fatal("Lost connection to MongoDB server and failed to reconnect after 30 attempts!")
90+
}
91+
} else {
92+
log.Info("MongoDB connection restored")
93+
errorCounter = 0
7694
}
77-
} else {
78-
log.Fatal("Lost connection to MongoDB server for more than 10 seconds!")
7995
}
8096
}
8197
}
8298

99+
func reconnectMongo() error {
100+
if mongoClient != nil {
101+
if err := mongoClient.Disconnect(mongoClientCtx); err != nil {
102+
log.WithError(err).Error("Failed to disconnect from MongoDB")
103+
}
104+
}
105+
106+
clientOptions := options.Client().ApplyURI(connectionString)
107+
newClient, err := mongo.Connect(mongoClientCtx, clientOptions)
108+
if err != nil {
109+
return fmt.Errorf("failed to reconnect to MongoDB: %v", err)
110+
}
111+
112+
if err := newClient.Ping(mongoClientCtx, nil); err != nil {
113+
return fmt.Errorf("failed to ping new MongoDB connection: %v", err)
114+
}
115+
116+
mongoClient = newClient
117+
return nil
118+
}
119+
83120
func GetProviderFromDB(nickname string) (models.Provider, error) {
84121
var provider models.Provider
85122
coll := mongoClient.Database("gads").Collection("providers")

0 commit comments

Comments
 (0)