Skip to content

Commit

Permalink
Refactor mongo health check
Browse files Browse the repository at this point in the history
  • Loading branch information
minhduc140583 committed Aug 5, 2023
1 parent 3981754 commit e1a7bef
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 56 deletions.
45 changes: 0 additions & 45 deletions elasticsearch/health_checker.go

This file was deleted.

15 changes: 7 additions & 8 deletions mongo/health_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@ import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
"go.mongodb.org/mongo-driver/mongo/readpref"
"time"
)

type HealthChecker struct {
db *mongo.Database
client *mongo.Client
name string
timeout time.Duration
}

func NewMongoHealthChecker(db *mongo.Database, name string, timeouts ...time.Duration) *HealthChecker {
func NewMongoHealthChecker(client *mongo.Client, name string, timeouts ...time.Duration) *HealthChecker {
var timeout time.Duration
if len(timeouts) >= 1 {
timeout = timeouts[0]
} else {
timeout = 4 * time.Second
}
return &HealthChecker{db: db, name: name, timeout: timeout}
return &HealthChecker{client: client, name: name, timeout: timeout}
}
func NewHealthChecker(db *mongo.Database, options ...string) *HealthChecker {
func NewHealthChecker(client *mongo.Client, options ...string) *HealthChecker {
var name string
if len(options) >= 1 && len(options[0]) > 0 {
name = options[0]
} else {
name = "mongo"
}
return NewMongoHealthChecker(db, name, 4 * time.Second)
return NewMongoHealthChecker(client, name, 4*time.Second)
}

func (s *HealthChecker) Name() string {
Expand All @@ -45,10 +45,9 @@ func (s *HealthChecker) Check(ctx context.Context) (map[string]interface{}, erro
defer cancel()

res := make(map[string]interface{})
info := make(map[string]interface{})
checkerChan := make(chan error)
go func() {
checkerChan <- s.db.RunCommand(ctx, bsonx.Doc{{"ping", bsonx.Int32(1)}}).Decode(&info)
checkerChan <- s.client.Ping(ctx, readpref.Primary())
}()
select {
case err := <-checkerChan:
Expand Down
7 changes: 4 additions & 3 deletions mongo/server_info_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package mongo
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
"time"
)

Expand All @@ -24,7 +24,7 @@ func NewServerInfoChecker(db *mongo.Database, options ...string) *ServerInfoChec
} else {
name = "mongo"
}
return NewServerInfoCheckerWithTimeout(db, name, 4 * time.Second)
return NewServerInfoCheckerWithTimeout(db, name, 4*time.Second)
}

func (s *ServerInfoChecker) Name() string {
Expand All @@ -41,8 +41,9 @@ func (s *ServerInfoChecker) Check(ctx context.Context) (map[string]interface{},
res := make(map[string]interface{})
info := make(map[string]interface{})
checkerChan := make(chan error)
command := bson.D{{ "serverStatus", 1 }}
go func() {
checkerChan <- s.db.RunCommand(ctx, bsonx.Doc{{"serverStatus", bsonx.Int32(1)}}).Decode(&info)
checkerChan <- s.db.RunCommand(ctx, command).Decode(&info)
}()
select {
case err := <-checkerChan:
Expand Down

0 comments on commit e1a7bef

Please sign in to comment.