Browse Source

Retry source image request if http connection was lost

DarthSim 2 years ago
parent
commit
168f6f6043
1 changed files with 20 additions and 4 deletions
  1. 20 4
      imagedata/download.go

+ 20 - 4
imagedata/download.go

@@ -7,6 +7,7 @@ import (
 	"io"
 	"net/http"
 	"net/http/cookiejar"
+	"strings"
 	"time"
 
 	"github.com/imgproxy/imgproxy/v3/config"
@@ -164,12 +165,27 @@ func BuildImageRequest(ctx context.Context, imageURL string, header http.Header,
 }
 
 func SendRequest(req *http.Request) (*http.Response, error) {
-	res, err := downloadClient.Do(req)
-	if err != nil {
+	for {
+		res, err := downloadClient.Do(req)
+		if err == nil {
+			return res, nil
+		}
+
+		if res != nil && res.Body != nil {
+			res.Body.Close()
+		}
+
+		if strings.Contains(err.Error(), "client connection lost") {
+			select {
+			case <-req.Context().Done():
+				return nil, err
+			case <-time.After(100 * time.Microsecond):
+				continue
+			}
+		}
+
 		return nil, wrapError(err)
 	}
-
-	return res, nil
 }
 
 func requestImage(ctx context.Context, imageURL string, opts DownloadOptions) (*http.Response, context.CancelFunc, error) {