Fix repair :)
This commit is contained in:
@@ -277,24 +277,25 @@ func (rd *RealDebrid) UnrestrictLink(link string) (*UnrestrictResponse, error) {
|
||||
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
// at this point, any errors mean that the link has expired and we need to repair it
|
||||
resp, err := rd.client.Do(req)
|
||||
if err != nil {
|
||||
rd.log.Errorf("Error when executing the unrestrict link request: %v", err)
|
||||
return nil, err
|
||||
// rd.log.Errorf("Error when executing the unrestrict link request: %v", err)
|
||||
return nil, fmt.Errorf("unrestrict link request failed so likely it has expired")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
rd.log.Errorf("Error when reading the body of unrestrict link response: %v", err)
|
||||
return nil, err
|
||||
// rd.log.Errorf("Error when reading the body of unrestrict link response: %v", err)
|
||||
return nil, fmt.Errorf("unreadable body so likely it has expired")
|
||||
}
|
||||
|
||||
var response UnrestrictResponse
|
||||
err = json.Unmarshal(body, &response)
|
||||
if err != nil {
|
||||
rd.log.Errorf("Error when decoding unrestrict link JSON: %v", err)
|
||||
return nil, err
|
||||
// rd.log.Errorf("Error when decoding unrestrict link JSON: %v", err)
|
||||
return nil, fmt.Errorf("undecodable response so likely it has expired")
|
||||
}
|
||||
|
||||
if !canFetchFirstByte(response.Download) {
|
||||
|
||||
@@ -58,7 +58,6 @@ type TorrentInfo struct {
|
||||
OriginalName string `json:"original_filename"` // from info
|
||||
OriginalBytes int64 `json:"original_bytes"` // from info
|
||||
Files []File `json:"files"` // from info
|
||||
ForRepair bool `json:"-"`
|
||||
Version string `json:"-"`
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user