-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
72 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"go.mongodb.org/mongo-driver/mongo/readpref" | ||
) | ||
|
||
func ping(c *gin.Context) { | ||
c.JSON(200, gin.H{ | ||
"message": "pong", | ||
}) | ||
} | ||
|
||
func ping_mongodb_primary() (time.Duration, error) { | ||
PrimaryStartTime := time.Now() | ||
// ping primary | ||
PrimaryErr := mongoClient.Ping(context.Background(), readpref.Primary()) | ||
PrimaryEalapsedTime := time.Since(PrimaryStartTime) | ||
|
||
return PrimaryEalapsedTime, PrimaryErr | ||
} | ||
|
||
func ping_mongodb_nearest() (time.Duration, error) { | ||
NearestStartTime := time.Now() | ||
// ping nearest | ||
NearestErr := mongoClient.Ping(context.Background(), readpref.Nearest()) | ||
NearestEalapsedTime := time.Since(NearestStartTime) | ||
|
||
return NearestEalapsedTime, NearestErr | ||
} | ||
func ping_mongodb(c *gin.Context) { | ||
primaryChan := make(chan time.Duration) | ||
nearestChan := make(chan time.Duration) | ||
errChan := make(chan error) | ||
|
||
go func() { | ||
PrimaryEalapsedTime, PrimaryErr := ping_mongodb_primary() | ||
primaryChan <- PrimaryEalapsedTime | ||
errChan <- PrimaryErr | ||
}() | ||
|
||
go func() { | ||
NearestEalapsedTime, NearestErr := ping_mongodb_nearest() | ||
nearestChan <- NearestEalapsedTime | ||
errChan <- NearestErr | ||
}() | ||
|
||
PrimaryEalapsedTime := <-primaryChan | ||
NearestEalapsedTime := <-nearestChan | ||
PrimaryErr := <-errChan | ||
NearestErr := <-errChan | ||
|
||
if PrimaryErr != nil || NearestErr != nil { | ||
c.JSON(500, gin.H{ | ||
"message": "MongoDB is down?", | ||
"PrimaryEalapsedTime": PrimaryEalapsedTime.Milliseconds(), | ||
"NearestEalapsedTime": NearestEalapsedTime.Milliseconds(), | ||
}) | ||
} else { | ||
c.JSON(200, gin.H{ | ||
"message": "MongoDB is up", | ||
"PrimaryEalapsedTime": PrimaryEalapsedTime.Milliseconds(), | ||
"NearestEalapsedTime": NearestEalapsedTime.Milliseconds(), | ||
}) | ||
} | ||
} |