github.com/rbisecke/kafka-go@v0.4.27/time.go (about) 1 package kafka 2 3 import ( 4 "math" 5 "time" 6 ) 7 8 const ( 9 maxTimeout = time.Duration(math.MaxInt32) * time.Millisecond 10 minTimeout = time.Duration(math.MinInt32) * time.Millisecond 11 defaultRTT = 1 * time.Second 12 ) 13 14 func makeTime(t int64) time.Time { 15 if t <= 0 { 16 return time.Time{} 17 } 18 return time.Unix(t/1000, (t%1000)*int64(time.Millisecond)).UTC() 19 } 20 21 func timestamp(t time.Time) int64 { 22 if t.IsZero() { 23 return 0 24 } 25 return t.UnixNano() / int64(time.Millisecond) 26 } 27 28 func makeDuration(ms int32) time.Duration { 29 return time.Duration(ms) * time.Millisecond 30 } 31 32 func milliseconds(d time.Duration) int32 { 33 switch { 34 case d > maxTimeout: 35 d = maxTimeout 36 case d < minTimeout: 37 d = minTimeout 38 } 39 return int32(d / time.Millisecond) 40 } 41 42 func deadlineToTimeout(deadline time.Time, now time.Time) time.Duration { 43 if deadline.IsZero() { 44 return maxTimeout 45 } 46 return deadline.Sub(now) 47 } 48 49 func adjustDeadlineForRTT(deadline time.Time, now time.Time, rtt time.Duration) time.Time { 50 if !deadline.IsZero() { 51 timeout := deadline.Sub(now) 52 if timeout < rtt { 53 rtt = timeout / 4 54 } 55 deadline = deadline.Add(-rtt) 56 } 57 return deadline 58 }