Repair optimizations

This commit is contained in:
Ben Sarmiento
2023-11-19 02:00:50 +01:00
parent 0b21b8d58c
commit 2923f3918d
4 changed files with 43 additions and 27 deletions

View File

@@ -1,7 +1,6 @@
package realdebrid
import (
"math"
"net/http"
"strings"
"time"
@@ -20,8 +19,8 @@ func (rd *RealDebrid) UnrestrictUntilOk(link string) *UnrestrictResponse {
}
func retryUntilOk[T any](fn func() (T, error)) T {
const initialDelay = 1 * time.Second
const maxDelay = 128 * time.Second
// const initialDelay = 1 * time.Second
// const maxDelay = 128 * time.Second
const maxRetries = 2 // Maximum retries for non-429 errors
var result T
@@ -34,7 +33,9 @@ func retryUntilOk[T any](fn func() (T, error)) T {
return result
}
// If error is 429, we retry indefinitely, hence no condition to break the loop.
if strings.Contains(err.Error(), "first byte") || strings.Contains(err.Error(), "expired") {
return result
}
if !strings.Contains(err.Error(), "429") {
retryCount++
if retryCount >= maxRetries {
@@ -44,7 +45,8 @@ func retryUntilOk[T any](fn func() (T, error)) T {
}
// Calculate delay with exponential backoff
delay := time.Duration(math.Min(float64(initialDelay)*math.Pow(2, float64(retryCount)), float64(maxDelay)))
// delay := time.Duration(math.Min(float64(initialDelay)*math.Pow(2, float64(retryCount)), float64(maxDelay)))
delay := 500 * time.Millisecond
time.Sleep(delay)
}
}
@@ -85,7 +87,7 @@ func canFetchFirstByte(url string) bool {
return true
}
}
time.Sleep(1 * time.Second) // Add a delay before the next retry
time.Sleep(500 * time.Millisecond) // Add a delay before the next retry
}
return false
}