Skip to content

Commit

Permalink
Merge pull request #140 from transifex/devel
Browse files Browse the repository at this point in the history
v1.6.2
  • Loading branch information
Konstantinos Bairaktaris authored Nov 24, 2022
2 parents 5477c5f + 0ca242c commit ea995c8
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 28 deletions.
8 changes: 2 additions & 6 deletions internal/txlib/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,7 @@ func (task *FilePullTask) Run(send func(string), abort func()) {

err = handleThrottling(
func() error {
return txapi.PollResourceStringsDownload(
download,
time.Second,
sourceFile,
)
return txapi.PollResourceStringsDownload(download, sourceFile)
},
"",
func(msg string) { sendMessage(msg, false) },
Expand Down Expand Up @@ -534,7 +530,7 @@ func (task *FilePullTask) Run(send func(string), abort func()) {

err = handleThrottling(
func() error {
return txapi.PollTranslationDownload(download, time.Second, filePath)
return txapi.PollTranslationDownload(download, filePath)
},
"",
func(msg string) { sendMessage(msg, false) },
Expand Down
4 changes: 2 additions & 2 deletions internal/txlib/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ func (task *SourceFilePushTask) Run(send func(string), abort func()) {

err = handleThrottling(
func() error {
return txapi.PollSourceUpload(sourceUpload, time.Second)
return txapi.PollSourceUpload(sourceUpload)
},
"",
func(msg string) { sendMessage(msg, false) },
Expand Down Expand Up @@ -734,7 +734,7 @@ func (task *TranslationFileTask) Run(send func(string), abort func()) {
// Polling
err = handleThrottling(
func() error {
return txapi.PollTranslationUpload(upload, time.Second)
return txapi.PollTranslationUpload(upload)
},
"",
func(msg string) { sendMessage(msg, false) },
Expand Down
15 changes: 6 additions & 9 deletions pkg/txapi/resource_strings_async_downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package txapi
import (
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -33,13 +33,10 @@ func CreateResourceStringsAsyncDownload(
return download, err
}

func PollResourceStringsDownload(
download *jsonapi.Resource,
duration time.Duration,
filePath string,
) error {
func PollResourceStringsDownload(download *jsonapi.Resource, filePath string) error {
backoff := getBackoff(nil)
for {
time.Sleep(duration)
time.Sleep(time.Duration(backoff()) * time.Second)
err := download.Reload()
if err != nil {
return err
Expand All @@ -53,7 +50,7 @@ func PollResourceStringsDownload(
if resp.StatusCode != 200 {
return errors.New("file download error")
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand All @@ -64,7 +61,7 @@ func PollResourceStringsDownload(
return err
}

err = ioutil.WriteFile(filePath, bodyBytes, 0644)
err = os.WriteFile(filePath, bodyBytes, 0644)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/txapi/resource_strings_async_uploads.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ func UploadSource(
return &upload, nil
}

func PollSourceUpload(upload *jsonapi.Resource, duration time.Duration) error {
func PollSourceUpload(upload *jsonapi.Resource) error {
backoff := getBackoff(nil)
for {
time.Sleep(duration)
time.Sleep(time.Duration(backoff()) * time.Second)
err := upload.Reload()
if err != nil {
return err
Expand Down
11 changes: 6 additions & 5 deletions pkg/txapi/resource_translations_async_downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package txapi
import (
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -39,9 +39,10 @@ func CreateTranslationsAsyncDownload(
return download, err
}

func PollTranslationDownload(download *jsonapi.Resource, duration time.Duration, filePath string) error {
func PollTranslationDownload(download *jsonapi.Resource, filePath string) error {
backoff := getBackoff(nil)
for {
time.Sleep(duration)
time.Sleep(time.Duration(backoff()) * time.Second)
err := download.Reload()
if err != nil {
return err
Expand All @@ -57,7 +58,7 @@ func PollTranslationDownload(download *jsonapi.Resource, duration time.Duration,
if resp.StatusCode != 200 {
return errors.New("file download error")
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return err
Expand All @@ -67,7 +68,7 @@ func PollTranslationDownload(download *jsonapi.Resource, duration time.Duration,
if err != nil {
return err
}
err = ioutil.WriteFile(filePath, bodyBytes, 0644)
err = os.WriteFile(filePath, bodyBytes, 0644)
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/txapi/resource_translations_async_uploads.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ func (err *ResourceTranslationsAsyncUploadAttributes) Error() string {
return strings.Join(parts, ", ")
}

func PollTranslationUpload(
upload *jsonapi.Resource, duration time.Duration,
) error {
func PollTranslationUpload(upload *jsonapi.Resource) error {
backoff := getBackoff(nil)
for {
time.Sleep(duration)
time.Sleep(time.Duration(backoff()) * time.Second)
err := upload.Reload()
if err != nil {
return err
Expand Down
33 changes: 33 additions & 0 deletions pkg/txapi/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package txapi

/*
Return a function that returns the next item from 'pool' every time. When 'pool' runs
out, keep returning the last item forever.
backoff := getBackoff([]int{1, 2, 3})
fmt.Println(backoff())
// <<< 1
fmt.Println(backoff())
// <<< 2
fmt.Println(backoff())
// <<< 3
fmt.Println(backoff())
// <<< 3
fmt.Println(backoff())
// <<< 3
// ...
*/
func getBackoff(pool []int) func() int {
if pool == nil {
pool = []int{1, 1, 1, 2, 3, 5, 8, 13}
}
i := -1
return func() int {
i++
if i < len(pool) {
return pool[i]
} else {
return pool[len(pool)-1]
}
}
}

0 comments on commit ea995c8

Please sign in to comment.