Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
reverseproxy: Minor logging improvements
Matthew Holt committed
May 5, 2021 at 14:52 UTC
a17c3b568d635ea45deb204d06e251810e94a7d6
3 files changed
+16
-6
modules/caddyhttp/reverseproxy/reverseproxy.go
+5
-3
@@ -568,9 +568,11 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, repl *
568
logger := h.logger.With(
569
zap.String("upstream", di.Upstream.String()),
570
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: req}),
571
- zap.Duration("duration", duration))
571
+ )
572
if err != nil {
573
- logger.Debug("upstream roundtrip", zap.Error(err))
573
+ logger.Debug("upstream roundtrip",
574
+ zap.Duration("duration", duration),
575
+ zap.Error(err))
576
return err
577
}
578
logger.Debug("upstream roundtrip",
@@ -642,7 +644,7 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, repl *
644
645
// deal with 101 Switching Protocols responses: (WebSocket, h2c, etc)
646
if res.StatusCode == http.StatusSwitchingProtocols {
645
- h.handleUpgradeResponse(rw, req, res)
647
+ h.handleUpgradeResponse(logger, rw, req, res)
648
return nil
649
}
650
modules/caddyhttp/reverseproxy/selectionpolicies_test.go
+1
-1
@@ -366,7 +366,7 @@ func TestCookieHashPolicy(t *testing.T) {
366
h := cookieHashPolicy.Select(pool, request, w)
367
cookieServer1 := w.Result().Cookies()[0]
368
if cookieServer1 == nil {
369
- t.Error("cookieHashPolicy should set a cookie")
369
+ t.Fatal("cookieHashPolicy should set a cookie")
370
}
371
if cookieServer1.Name != "lb" {
372
t.Error("cookieHashPolicy should set a cookie with name lb")
modules/caddyhttp/reverseproxy/streaming.go
+10
-2
@@ -29,7 +29,7 @@ import (
29
"go.uber.org/zap"
30
)
31
32
-func (h Handler) handleUpgradeResponse(rw http.ResponseWriter, req *http.Request, res *http.Response) {
32
+func (h Handler) handleUpgradeResponse(logger *zap.Logger, rw http.ResponseWriter, req *http.Request, res *http.Response) {
33
reqUpType := upgradeType(req.Header)
34
resUpType := upgradeType(res.Header)
35
if reqUpType != resUpType {
@@ -65,12 +65,19 @@ func (h Handler) handleUpgradeResponse(rw http.ResponseWriter, req *http.Request
65
}()
66
defer close(backConnCloseCh)
67
68
+ logger.Debug("upgrading connection")
69
conn, brw, err := hj.Hijack()
70
if err != nil {
70
- h.logger.Error("Hijack failed on protocol switch", zap.Error(err))
71
+ h.logger.Error("hijack failed on protocol switch", zap.Error(err))
72
return
73
}
74
defer conn.Close()
75
+
76
+ start := time.Now()
77
+ defer func() {
78
+ logger.Debug("connection closed", zap.Duration("duration", time.Since(start)))
79
+ }()
80
+
81
res.Body = nil // so res.Write only writes the headers; we have res.Body in backConn above
82
if err := res.Write(brw); err != nil {
83
h.logger.Debug("response write", zap.Error(err))
@@ -80,6 +87,7 @@ func (h Handler) handleUpgradeResponse(rw http.ResponseWriter, req *http.Request
87
h.logger.Debug("response flush", zap.Error(err))
88
return
89
}
90
+
91
errc := make(chan error, 1)
92
spc := switchProtocolCopier{user: conn, backend: backConn}
93
go spc.copyToBackend(errc)