-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.go
62 lines (54 loc) · 1.27 KB
/
monitor.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package api
import (
"time"
"github.com/juju/clock"
)
// monitor performs regular pings of an API connection as well as
// monitoring the connection closed channel and the underlying
// rpc.Conn's dead channel. It will close `broken` if pings fail, or
// if `closed` or `dead` are closed.
type monitor struct {
clock clock.Clock
ping func() error
pingPeriod time.Duration
pingTimeout time.Duration
closed <-chan struct{}
dead <-chan struct{}
broken chan<- struct{}
}
func (m *monitor) run() {
defer close(m.broken)
for {
select {
case <-m.closed:
return
case <-m.dead:
logger.Debugf("RPC connection died")
return
case <-m.clock.After(m.pingPeriod):
if !m.pingWithTimeout() {
return
}
}
}
}
func (m *monitor) pingWithTimeout() bool {
result := make(chan error, 1)
go func() {
// Note that result is buffered so that we don't leak this
// goroutine when a timeout happens.
result <- m.ping()
}()
select {
case err := <-result:
if err != nil {
logger.Debugf("health ping failed: %v", err)
}
return err == nil
case <-m.clock.After(m.pingTimeout):
logger.Warningf("health ping timed out after %s", m.pingTimeout)
return false
}
}