random sleep duration before reconnecting (#2816)

This commit is contained in:
fatedier
2022-02-24 11:59:36 +08:00
committed by GitHub
parent 10100c28d9
commit 19739ed31a
2 changed files with 27 additions and 14 deletions

View File

@@ -19,9 +19,11 @@ import (
"crypto/rand"
"encoding/hex"
"fmt"
mathrand "math/rand"
"net"
"strconv"
"strings"
"time"
)
// RandID return a rand string used in frp.
@@ -109,3 +111,17 @@ func GenerateResponseErrorString(summary string, err error, detailed bool) strin
}
return summary
}
func RandomSleep(duration time.Duration, minRatio, maxRatio float64) time.Duration {
min := int64(minRatio * 1000.0)
max := int64(maxRatio * 1000.0)
var n int64
if max <= min {
n = min
} else {
n = mathrand.Int63n(max-min) + min
}
d := duration * time.Duration(n) / time.Duration(1000)
time.Sleep(d)
return d
}