Retry on failed download of plugins from marketplace. (#14176)
* Retry on failed download of plugins from marketplace. * minor polishing. * Handling http errors during downloading Adding unit test for DownloadFromURL * Adding suggested changes from the PR Close response body before returning error during progressive retry Remove separate struct for the downloadURL parameter in download_test Adding comment to clarify the share retries variable Changing to NoError and Error in the test assertion. * Added license header to download_test.go Co-authored-by: mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
e03fca5032
Коммит
288ed40e8f
@@ -4,11 +4,15 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mattermost/mattermost-server/v5/model"
|
"github.com/mattermost/mattermost-server/v5/model"
|
||||||
|
"github.com/mattermost/mattermost-server/v5/utils"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,10 +38,26 @@ func (a *App) DownloadFromURL(downloadURL string) ([]byte, error) {
|
|||||||
client := a.HTTPService().MakeClient(true)
|
client := a.HTTPService().MakeClient(true)
|
||||||
client.Timeout = HTTP_REQUEST_TIMEOUT
|
client.Timeout = HTTP_REQUEST_TIMEOUT
|
||||||
|
|
||||||
resp, err := client.Get(downloadURL)
|
var resp *http.Response
|
||||||
|
err = utils.ProgressiveRetry(func() error {
|
||||||
|
resp, err = client.Get(downloadURL)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "failed to fetch from %s", downloadURL)
|
return errors.Wrapf(err, "failed to fetch from %s", downloadURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
|
||||||
|
_, _ = io.Copy(ioutil.Discard, resp.Body)
|
||||||
|
_ = resp.Body.Close()
|
||||||
|
return errors.Errorf("failed to fetch from %s", downloadURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "download failed after multiple retries.")
|
||||||
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
return ioutil.ReadAll(resp.Body)
|
return ioutil.ReadAll(resp.Body)
|
||||||
|
|||||||
72
app/download_test.go
Обычный файл
72
app/download_test.go
Обычный файл
@@ -0,0 +1,72 @@
|
|||||||
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See LICENSE.txt for license information.
|
||||||
|
|
||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/mattermost/mattermost-server/v5/model"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDownloadFromURL(t *testing.T) {
|
||||||
|
th := Setup(t).InitBasic()
|
||||||
|
defer th.TearDown()
|
||||||
|
|
||||||
|
app := th.App
|
||||||
|
app.Config().PluginSettings.AllowInsecureDownloadUrl = model.NewBool(true)
|
||||||
|
|
||||||
|
// To keep track of how many times an endpoint is retried. This needs to be reset
|
||||||
|
// for each test run.
|
||||||
|
retries := 0
|
||||||
|
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/succeeds-after-retry", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if retries < 2 {
|
||||||
|
http.Error(w, "Request Timed out", http.StatusGatewayTimeout)
|
||||||
|
retries++
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _ = w.Write([]byte("Your request is successful."))
|
||||||
|
})
|
||||||
|
|
||||||
|
mux.HandleFunc("/fails-forever", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
http.Error(w, "This would fail forever", http.StatusInternalServerError)
|
||||||
|
})
|
||||||
|
|
||||||
|
testServer := httptest.NewServer(mux)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
downloadURL string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Should succeed after two retries",
|
||||||
|
downloadURL: fmt.Sprintf("%s/succeeds-after-retry", testServer.URL),
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Should not retry forever",
|
||||||
|
downloadURL: fmt.Sprintf("%s/fails-forever", testServer.URL),
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
retries = 0 // reset the retires
|
||||||
|
_, err := th.App.DownloadFromURL(tt.downloadURL)
|
||||||
|
|
||||||
|
if tt.wantErr {
|
||||||
|
require.Error(t, err)
|
||||||
|
} else {
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Ссылка в новой задаче
Block a user