-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.go
65 lines (55 loc) · 1.23 KB
/
tcp.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
package tcp
import (
"fmt"
"net"
"time"
monitoring_check "github.com/bborbe/monitoring/check"
"github.com/golang/glog"
)
type check struct {
host string
port int
timeout time.Duration
retryCounter int
}
const (
DEFAULT_TIMEOUT = time.Duration(5 * time.Second)
DEFAULT_RETRY_COUNTER = 3
)
func New(host string, port int) *check {
h := new(check)
h.host = host
h.port = port
h.timeout = DEFAULT_TIMEOUT
h.retryCounter = DEFAULT_RETRY_COUNTER
return h
}
func (c *check) Timeout(timeout time.Duration) *check {
c.timeout = timeout
return c
}
func (c *check) RetryCounter(retryCounter int) *check {
c.retryCounter = retryCounter
return c
}
func (h *check) Check() monitoring_check.CheckResult {
start := time.Now()
var err error
for i := 0; i < h.retryCounter; i++ {
err = h.check()
if err == nil {
break
}
}
return monitoring_check.NewCheckResult(h, err, time.Now().Sub(start))
}
func (c *check) check() error {
address := fmt.Sprintf("%s:%d", c.host, c.port)
var err error
_, err = net.DialTimeout("tcp", address, c.timeout)
glog.V(2).Infof("tcp check on %s: %v", address, err)
return err
}
func (c *check) Description() string {
return fmt.Sprintf("tcp check on %s:%d", c.host, c.port)
}