)
func (cresp ConnectionResponse) ServeHTTP(w http.ResponseWriter, req *http.Request) {
+ defer cresp.Conn.Close()
hj, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "ResponseWriter does not support connection upgrade", http.StatusInternalServerError)
defer conn.Close()
var bytesIn, bytesOut int64
- var wg sync.WaitGroup
ctx, cancel := context.WithCancel(req.Context())
+ var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
ctxlog.FromContext(ctx).WithError(err).Error("error copying upstream")
}
}()
- wg.Wait()
- if cresp.Logger != nil {
- cresp.Logger.WithFields(logrus.Fields{
- "bytesIn": bytesIn,
- "bytesOut": bytesOut,
- }).Info("closed connection")
- }
+ <-ctx.Done()
+ go func() {
+ // Wait for both io.Copy goroutines to finish and increment
+ // their byte counters.
+ wg.Wait()
+ if cresp.Logger != nil {
+ cresp.Logger.WithFields(logrus.Fields{
+ "bytesIn": bytesIn,
+ "bytesOut": bytesOut,
+ }).Info("closed connection")
+ }
+ }()
}