mirror of
https://github.com/hanzo-ml/kuberay.git
synced 2026-10-30 19:48:47 +00:00
* fix: missing timeout deadline in context Signed-off-by: Cheng-Yeh Chung <[email protected]> * feat: merge v1/v2 RetryConfig Signed-off-by: Cheng-Yeh Chung <[email protected]> * feat: merge v1/v2 backoff logic and timeout machenism Signed-off-by: Cheng-Yeh Chung <[email protected]> * update util function Signed-off-by: Cheng-Yeh Chung <[email protected]> * naming consistency Signed-off-by: Cheng-Yeh Chung <[email protected]> * update naming in util func Signed-off-by: Cheng-Yeh Chung <[email protected]> * Update apiserversdk/proxy.go Co-authored-by: Nary Yeh <[email protected]> Signed-off-by: Cheng-Yeh Chung <[email protected]> * simplify http util Signed-off-by: Cheng-Yeh Chung <[email protected]> --------- Signed-off-by: Cheng-Yeh Chung <[email protected]> Co-authored-by: Nary Yeh <[email protected]>
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package util
|
|
|
|
import (
|
|
"context"
|
|
"math"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func Sleep(ctx context.Context, sleepDuration time.Duration) error {
|
|
select {
|
|
case <-time.After(sleepDuration):
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func CheckContextDeadline(ctx context.Context, sleepDuration time.Duration) bool {
|
|
if deadline, ok := ctx.Deadline(); ok {
|
|
remaining := time.Until(deadline)
|
|
if sleepDuration > remaining {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func GetRetryBackoff(attempt int, initBackoff time.Duration, backoffFactor float64, maxBackoff time.Duration) time.Duration {
|
|
sleepDuration := min(initBackoff*time.Duration(math.Pow(backoffFactor, float64(attempt))), maxBackoff)
|
|
return sleepDuration
|
|
}
|
|
|
|
func IsSuccessfulStatusCode(statusCode int) bool {
|
|
return 200 <= statusCode && statusCode < 300
|
|
}
|
|
|
|
func IsRetryableHTTPStatusCodes(statusCode int) bool {
|
|
switch statusCode {
|
|
case http.StatusRequestTimeout, // 408
|
|
http.StatusTooManyRequests, // 429
|
|
http.StatusInternalServerError, // 500
|
|
http.StatusBadGateway, // 502
|
|
http.StatusServiceUnavailable, // 503
|
|
http.StatusGatewayTimeout: // 504
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|