From 4a89f0c355acc1c546d9cf1ce7cb826cb10ebbbc Mon Sep 17 00:00:00 2001 From: DarthSim Date: Mon, 28 Jan 2019 22:19:59 +0600 Subject: [PATCH] Limit idle conections; Close response body on error --- download.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/download.go b/download.go index 9284628d..20f70987 100644 --- a/download.go +++ b/download.go @@ -8,6 +8,7 @@ import ( "image" "io" "io/ioutil" + "net" "net/http" "strconv" "time" @@ -56,7 +57,11 @@ func (lr *limitReader) Close() error { func initDownloading() { transport := &http.Transport{ - Proxy: http.ProxyFromEnvironment, + Proxy: http.ProxyFromEnvironment, + MaxIdleConns: conf.Concurrency, + MaxIdleConnsPerHost: conf.Concurrency, + DisableCompression: true, + Dial: (&net.Dialer{KeepAlive: 600 * time.Second}).Dial, } if conf.IgnoreSslVerification { @@ -176,10 +181,12 @@ func downloadImage(ctx context.Context) (context.Context, context.CancelFunc, er req.Header.Set("User-Agent", conf.UserAgent) res, err := downloadClient.Do(req) + if res != nil { + defer res.Body.Close() + } if err != nil { return ctx, func() {}, newError(404, err.Error(), msgSourceImageIsUnreachable) } - defer res.Body.Close() if res.StatusCode != 200 { body, _ := ioutil.ReadAll(res.Body)