-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.go
69 lines (58 loc) · 1.7 KB
/
ping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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(),
})
}
}