Skip to content

Commit

Permalink
Poling translations infinite loop fix
Browse files Browse the repository at this point in the history
If downloading translations fails due
to reasons such as the inability to
create the downloaded file
the  "redirect" field in the
response is empty and satus of task pending.
An infinite loop can occur.
Based on backoff functionality, a request will be retried after
1s, 1s, 1s, 2s, 3s, 5s, 8s, 13s and then after 13s forever
To avoid this we can break the loop after a certain number of retries

Introduce max retries on polling
  • Loading branch information
foteinigk committed Jul 31, 2023
1 parent 5acb442 commit fd077f8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/txapi/resource_strings_async_downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func CreateResourceStringsAsyncDownload(

func PollResourceStringsDownload(download *jsonapi.Resource, filePath string) error {
backoff := getBackoff(nil)
maxRetries := 20
retries := 0
for {
time.Sleep(time.Duration(backoff()) * time.Second)
err := download.Reload()
Expand Down Expand Up @@ -76,5 +78,9 @@ func PollResourceStringsDownload(download *jsonapi.Resource, filePath string) er
} else if download.Attributes["status"] == "succeeded" {
return nil
}
retries++
if retries >= maxRetries {
return errors.New("maximum retries reached, download failed")
}
}
}
11 changes: 11 additions & 0 deletions pkg/txapi/resource_translations_async_downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func CreateTranslationsAsyncDownload(

func PollTranslationDownload(download *jsonapi.Resource, filePath string) error {
backoff := getBackoff(nil)
maxRetries := 20
retries := 0
for {
time.Sleep(time.Duration(backoff()) * time.Second)
err := download.Reload()
Expand All @@ -49,6 +51,15 @@ func PollTranslationDownload(download *jsonapi.Resource, filePath string) error
}
if download.Redirect != "" {
break
} else if download.Attributes["status"] == "failed" {
return fmt.Errorf(
"download of translation '%s' failed",
download.Relationships["resource"].DataSingular.Id,
)
}
retries++
if retries >= maxRetries {
return errors.New("maximum retries reached, download failed")
}
}
resp, err := http.Get(download.Redirect)
Expand Down

0 comments on commit fd077f8

Please sign in to comment.