From 51d2da230de74fdf6fdb5653e8ddf14172e881f2 Mon Sep 17 00:00:00 2001 From: Jeremy Facchetti Date: Tue, 15 Mar 2022 10:27:41 +0100 Subject: [PATCH 1/2] cleanup: proxy now uses idiomatic waitgroup. --- pkg/proxy/proxy.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pkg/proxy/proxy.go b/pkg/proxy/proxy.go index dd9c7bef3..5f7f80f6f 100644 --- a/pkg/proxy/proxy.go +++ b/pkg/proxy/proxy.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "net" "net/http" + "sync" "github.com/sirupsen/logrus" @@ -144,12 +145,17 @@ func Proxy(log *logrus.Entry, w http.ResponseWriter, r *http.Request, sz int) { } defer c1.Close() - ch := make(chan struct{}) + var wg sync.WaitGroup + // Wait for the c1->c2 goroutine to complete before exiting. + //Then the deferred c1.Close() and c2.Close() will be called. + defer wg.Wait() + + wg.Add(1) go func() { // use a goroutine to copy from c1->c2. Call c2.CloseWrite() when done. defer recover.Panic(log) - defer close(ch) + defer wg.Done() defer func() { _ = c2.(*net.TCPConn).CloseWrite() }() @@ -164,7 +170,4 @@ func Proxy(log *logrus.Entry, w http.ResponseWriter, r *http.Request, sz int) { _, _ = io.Copy(c1, c2) }() - // wait for the c1->c2 goroutine to complete. Then the deferred c1.Close() - // and c2.Close() will be called. - <-ch } From 73c9f04c30d4a925c7dd3021d6341ad128103282 Mon Sep 17 00:00:00 2001 From: Jeremy Facchetti Date: Tue, 15 Mar 2022 10:52:31 +0100 Subject: [PATCH 2/2] cleanup: removed useless anonymous function definition. --- pkg/proxy/proxy.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pkg/proxy/proxy.go b/pkg/proxy/proxy.go index 5f7f80f6f..0730f24c7 100644 --- a/pkg/proxy/proxy.go +++ b/pkg/proxy/proxy.go @@ -162,12 +162,10 @@ func Proxy(log *logrus.Entry, w http.ResponseWriter, r *http.Request, sz int) { _, _ = io.Copy(c2, buf) }() - func() { - // copy from c2->c1. Call c1.CloseWrite() when done. - defer func() { - _ = c1.(interface{ CloseWrite() error }).CloseWrite() - }() - _, _ = io.Copy(c1, c2) + // copy from c2->c1. Call c1.CloseWrite() when done. + defer func() { + _ = c1.(interface{ CloseWrite() error }).CloseWrite() }() + _, _ = io.Copy(c1, c2) }