From b60ae43bf3cfd2f5ea5f1f817317f307a76bb0aa Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 3 Mar 2024 12:22:49 +0100 Subject: [PATCH] remove defer close in a loop --- internal/piwigo/files.go | 2 +- internal/piwigo/post.go | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/internal/piwigo/files.go b/internal/piwigo/files.go index 349f78c..c45704e 100644 --- a/internal/piwigo/files.go +++ b/internal/piwigo/files.go @@ -68,7 +68,7 @@ func (p *Piwigo) Upload(file *piwigotools.FileToUpload, stat *piwigotools.FileTo ok := true wg.Add(nbJobs) - for j := 0; j < nbJobs; j++ { + for range nbJobs { go p.UploadChunk(file, chunks, wg, stat, &ok) } wg.Wait() diff --git a/internal/piwigo/post.go b/internal/piwigo/post.go index bc0b3e7..4d405cc 100644 --- a/internal/piwigo/post.go +++ b/internal/piwigo/post.go @@ -52,10 +52,14 @@ func (p *Piwigo) Post(method string, form *url.Values, resp interface{}) error { raw := bytes.NewBuffer([]byte{}) - for i := 0; i < 3; i++ { + for i := range 3 { if i > 0 { time.Sleep(time.Second) // wait 1 sec before retry } + + func() { + + }() req, err := http.NewRequest("POST", Url, strings.NewReader(encodedForm)) if err != nil { return err @@ -70,15 +74,16 @@ func (p *Piwigo) Post(method string, form *url.Values, resp interface{}) error { if err != nil { continue } - defer r.Body.Close() _, err = io.Copy(raw, r.Body) if err != nil { + _ = r.Body.Close() continue } err = json.Unmarshal(raw.Bytes(), &Result) if err != nil { + _ = r.Body.Close() continue } @@ -89,6 +94,7 @@ func (p *Piwigo) Post(method string, form *url.Values, resp interface{}) error { } } + _ = r.Body.Close() break }