-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.go
40 lines (33 loc) · 798 Bytes
/
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
package main
import (
"fmt"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gopkg.in/mgo.v2"
)
func ping(member string) error {
session, err := mgo.DialWithTimeout(fmt.Sprintf(
"%v?connect=direct", member), 5*time.Second)
if err != nil {
return errors.Wrap(err, "Connection failed")
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
err = session.Ping()
return errors.Wrap(err, "Connection failed")
}
func pingWithRetry(member string, retry int, wait int) error {
var err error
for retry > 0 {
err = ping(member)
if err != nil {
retry--
logrus.Warnf("%v is offline retrying in %v seconds", member, wait)
time.Sleep(time.Duration(wait) * time.Second)
} else {
return nil
}
}
return errors.Wrapf(err, "%v ping failed", member)
}