forked from jjeffery/stomp
/
heartbeat.go
44 lines (40 loc) · 1.17 KB
/
heartbeat.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
package frame
import (
"math"
"regexp"
"strconv"
"strings"
"time"
)
var (
// Regexp for heart-beat header value
heartBeatRegexp = regexp.MustCompile("^[0-9]+,[0-9]+$")
)
const (
// Maximum number of milliseconds that can be represented
// in a time.Duration.
maxMilliseconds = math.MaxInt64 / int64(time.Millisecond)
)
// ParseHeartBeat parses the value of a STOMP heart-beat entry and
// returns two time durations. Returns an error if the heart-beat
// value is not in the correct format, or if the time durations are
// too big to be represented by the time.Duration type.
func ParseHeartBeat(heartBeat string) (time.Duration, time.Duration, error) {
if !heartBeatRegexp.MatchString(heartBeat) {
return 0, 0, ErrInvalidHeartBeat
}
slice := strings.Split(heartBeat, ",")
value1, err := strconv.ParseInt(slice[0], 10, 64)
if err != nil {
return 0, 0, ErrInvalidHeartBeat
}
value2, err := strconv.ParseInt(slice[1], 10, 64)
if err != nil {
return 0, 0, ErrInvalidHeartBeat
}
if value1 > maxMilliseconds || value2 > maxMilliseconds {
return 0, 0, ErrInvalidHeartBeat
}
return time.Duration(value1) * time.Millisecond,
time.Duration(value2) * time.Millisecond, nil
}