Skip to content

Commit

Permalink
cleanup: proxy now uses idiomatic waitgroup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Facchetti committed Mar 15, 2022
1 parent 648c860 commit 51d2da2
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io/ioutil"
"net"
"net/http"
"sync"

"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -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()
}()
Expand All @@ -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
}

0 comments on commit 51d2da2

Please sign in to comment.