Fix repair :)

This commit is contained in:
Ben Sarmiento
2023-11-18 23:39:42 +01:00
parent 34b1a19478
commit ff93baa6c1
7 changed files with 264 additions and 348 deletions

View File

@@ -20,14 +20,31 @@ func (rd *RealDebrid) UnrestrictUntilOk(link string) *UnrestrictResponse {
}
func retryUntilOk[T any](fn func() (T, error)) T {
const initialDelay = 1 * time.Second
const initialDelay = 2 * time.Second
const maxDelay = 128 * time.Second
for i := 0; ; i++ {
result, err := fn()
if err == nil || !strings.Contains(err.Error(), "429") {
const maxRetries = 5 // Maximum retries for non-429 errors
var result T
var err error
var retryCount int
for {
result, err = fn()
if err == nil {
return result
}
delay := time.Duration(math.Min(float64(initialDelay*time.Duration(math.Pow(2, float64(i)))), float64(maxDelay)))
// If error is 429, we retry indefinitely, hence no condition to break the loop.
if !strings.Contains(err.Error(), "429") {
retryCount++
if retryCount >= maxRetries {
// If we've reached the maximum retries for errors other than 429, return the last result.
return result
}
}
// Calculate delay with exponential backoff
delay := time.Duration(math.Min(float64(initialDelay)*math.Pow(2, float64(retryCount)), float64(maxDelay)))
time.Sleep(delay)
}
}