forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
55 lines (47 loc) · 1.06 KB
/
time.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
package kafka
import (
"math"
"time"
)
const (
maxTimeout = time.Duration(math.MaxInt32) * time.Millisecond
minTimeout = time.Duration(math.MinInt32) * time.Millisecond
defaultRTT = 1 * time.Second
)
func timestamp(t time.Time) int64 {
if t.IsZero() {
return 0
}
return t.UnixNano() / int64(time.Millisecond)
}
func timestampToTime(t int64) time.Time {
return time.Unix(t/1000, (t%1000)*int64(time.Millisecond))
}
func duration(ms int32) time.Duration {
return time.Duration(ms) * time.Millisecond
}
func milliseconds(d time.Duration) int32 {
switch {
case d > maxTimeout:
d = maxTimeout
case d < minTimeout:
d = minTimeout
}
return int32(d / time.Millisecond)
}
func deadlineToTimeout(deadline time.Time, now time.Time) time.Duration {
if deadline.IsZero() {
return maxTimeout
}
return deadline.Sub(now)
}
func adjustDeadlineForRTT(deadline time.Time, now time.Time, rtt time.Duration) time.Time {
if !deadline.IsZero() {
timeout := deadline.Sub(now)
if timeout < rtt {
rtt = timeout / 4
}
deadline = deadline.Add(-rtt)
}
return deadline
}