| 1 |
package ext |
| 2 |
|
| 3 |
// XXX: There's no logging around heartbeats - how can we do this in a way that is useful |
| 4 |
// as a library? |
| 5 |
// |
| 6 |
// XXX: When we close the session because of a lost heartbeat or because of an error in the |
| 7 |
// heartbeating, there is no way to tell that; a Session will just appear to stop working |
| 8 |
|
| 9 |
import ( |
| 10 |
"encoding/binary" |
| 11 |
proto "github.com/inconshreveable/muxado/proto" |
| 12 |
"github.com/inconshreveable/muxado/proto/frame" |
| 13 |
"io" |
| 14 |
"time" |
| 15 |
) |
| 16 |
|
| 17 |
const ( |
| 18 |
defaultHeartbeatInterval = 3 * time.Second |
| 19 |
defaultHeartbeatTolerance = 10 * time.Second |
| 20 |
) |
| 21 |
|
| 22 |
type Heartbeat struct { |
| 23 |
sess proto.ISession |
| 24 |
accept proto.ExtAccept |
| 25 |
|
| 26 |
mark chan int |
| 27 |
interval time.Duration |
| 28 |
tolerance time.Duration |
| 29 |
} |
| 30 |
|
| 31 |
func NewDefaultHeartbeat() *Heartbeat { |
| 32 |
return NewHeartbeat(defaultHeartbeatInterval, defaultHeartbeatTolerance) |
| 33 |
} |
| 34 |
|
| 35 |
func NewHeartbeat(interval, tolerance time.Duration) *Heartbeat { |
| 36 |
return &Heartbeat{ |
| 37 |
mark: make(chan int), |
| 38 |
interval: interval, |
| 39 |
tolerance: tolerance, |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func (h *Heartbeat) Start(sess proto.ISession, accept proto.ExtAccept) frame.StreamType { |
| 44 |
h.sess = sess |
| 45 |
h.accept = accept |
| 46 |
go h.respond() |
| 47 |
go h.request() |
| 48 |
go h.check() |
| 49 |
|
| 50 |
return heartbeatExtensionType |
| 51 |
} |
| 52 |
|
| 53 |
func (h *Heartbeat) check() { |
| 54 |
t := time.NewTimer(h.interval + h.tolerance) |
| 55 |
|
| 56 |
for { |
| 57 |
select { |
| 58 |
case <-t.C: |
| 59 |
// time out waiting for a response! |
| 60 |
h.sess.Close() |
| 61 |
return |
| 62 |
|
| 63 |
case <-h.mark: |
| 64 |
t.Reset(h.interval + h.tolerance) |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func (h *Heartbeat) respond() { |
| 70 |
// close the session on any errors |
| 71 |
defer h.sess.Close() |
| 72 |
|
| 73 |
stream, err := h.accept() |
| 74 |
if err != nil { |
| 75 |
return |
| 76 |
} |
| 77 |
|
| 78 |
// read the next heartbeat id and respond |
| 79 |
buf := make([]byte, 4) |
| 80 |
for { |
| 81 |
_, err := io.ReadFull(stream, buf) |
| 82 |
if err != nil { |
| 83 |
return |
| 84 |
} |
| 85 |
|
| 86 |
_, err = stream.Write(buf) |
| 87 |
if err != nil { |
| 88 |
return |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func (h *Heartbeat) request() { |
| 94 |
// close the session on any errors |
| 95 |
defer h.sess.Close() |
| 96 |
|
| 97 |
// request highest possible priority for heartbeats |
| 98 |
priority := frame.StreamPriority(0x7FFFFFFF) |
| 99 |
stream, err := h.sess.OpenStream(priority, heartbeatExtensionType, false) |
| 100 |
if err != nil { |
| 101 |
return |
| 102 |
} |
| 103 |
|
| 104 |
// send heartbeats and then check that we got them back |
| 105 |
var id uint32 |
| 106 |
for { |
| 107 |
time.Sleep(h.interval) |
| 108 |
|
| 109 |
if err := binary.Write(stream, binary.BigEndian, id); err != nil { |
| 110 |
return |
| 111 |
} |
| 112 |
|
| 113 |
var respId uint32 |
| 114 |
if err := binary.Read(stream, binary.BigEndian, &respId); err != nil { |
| 115 |
return |
| 116 |
} |
| 117 |
|
| 118 |
if id != respId { |
| 119 |
return |
| 120 |
} |
| 121 |
|
| 122 |
// record the time |
| 123 |
h.mark <- 1 |
| 124 |
} |
| 125 |
} |