Added a retry for network requests

This commit is contained in:
Eduard Urbach 2017-06-15 17:45:59 +02:00
parent eb10ec15e2
commit aeac20d7ad

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"image" "image"
"net/http" "net/http"
"time"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/parnurzeal/gorequest" "github.com/parnurzeal/gorequest"
@ -28,11 +29,19 @@ func AvatarFromURL(url string, user *arn.User) *Avatar {
// Download // Download
response, data, networkErr := gorequest.New().Get(url).EndBytes() response, data, networkErr := gorequest.New().Get(url).EndBytes()
// Retry after 5 seconds if service unavailable
if response.StatusCode == http.StatusServiceUnavailable {
time.Sleep(5 * time.Second)
response, data, networkErr = gorequest.New().Get(url).EndBytes()
}
// Network errors
if networkErr != nil { if networkErr != nil {
avatarLog.Error("NET", user.Nick, url, networkErr) avatarLog.Error("NET", user.Nick, url, networkErr)
return nil return nil
} }
// Bad status codes
if response.StatusCode != http.StatusOK { if response.StatusCode != http.StatusOK {
avatarLog.Error("NET", user.Nick, url, response.StatusCode) avatarLog.Error("NET", user.Nick, url, response.StatusCode)
return nil return nil