avoid overflow in various timeouts (#505)
This commit is contained in:
parent
9e3e083f29
commit
d00c28104b
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
@ -457,7 +458,7 @@ func main() {
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
if *maxTime > 0 {
|
if *maxTime > 0 {
|
||||||
timeout := time.Duration(*maxTime * float64(time.Second))
|
timeout := floatSecondsToDuration(*maxTime)
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
@ -468,13 +469,13 @@ func main() {
|
||||||
defer dialTiming.Done()
|
defer dialTiming.Done()
|
||||||
dialTime := 10 * time.Second
|
dialTime := 10 * time.Second
|
||||||
if *connectTimeout > 0 {
|
if *connectTimeout > 0 {
|
||||||
dialTime = time.Duration(*connectTimeout * float64(time.Second))
|
dialTime = floatSecondsToDuration(*connectTimeout)
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithTimeout(ctx, dialTime)
|
ctx, cancel := context.WithTimeout(ctx, dialTime)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
var opts []grpc.DialOption
|
var opts []grpc.DialOption
|
||||||
if *keepaliveTime > 0 {
|
if *keepaliveTime > 0 {
|
||||||
timeout := time.Duration(*keepaliveTime * float64(time.Second))
|
timeout := floatSecondsToDuration(*keepaliveTime)
|
||||||
opts = append(opts, grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
opts = append(opts, grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
||||||
Time: timeout,
|
Time: timeout,
|
||||||
Timeout: timeout,
|
Timeout: timeout,
|
||||||
|
|
@ -971,3 +972,12 @@ func (f *optionalBoolFlag) Set(s string) error {
|
||||||
func (f *optionalBoolFlag) IsBoolFlag() bool {
|
func (f *optionalBoolFlag) IsBoolFlag() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func floatSecondsToDuration(seconds float64) time.Duration {
|
||||||
|
durationFloat := seconds * float64(time.Second)
|
||||||
|
if durationFloat > math.MaxInt64 {
|
||||||
|
// Avoid overflow
|
||||||
|
return math.MaxInt64
|
||||||
|
}
|
||||||
|
return time.Duration(durationFloat)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue