WriteHeader should be called once
Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
This commit is contained in:
parent
1cd636a24b
commit
697d318a6b
2 changed files with 19 additions and 14 deletions
|
@ -41,10 +41,10 @@ type (
|
|||
}
|
||||
|
||||
responseWrapper struct {
|
||||
sync.Once
|
||||
http.ResponseWriter
|
||||
|
||||
statusCode int
|
||||
headWritten bool
|
||||
startTime time.Time
|
||||
}
|
||||
)
|
||||
|
@ -202,12 +202,10 @@ func (st *HTTPStats) updateStats(api string, w http.ResponseWriter, r *http.Requ
|
|||
|
||||
// WriteHeader - writes http status code
|
||||
func (w *responseWrapper) WriteHeader(code int) {
|
||||
if !w.headWritten {
|
||||
w.Do(func() {
|
||||
w.statusCode = code
|
||||
w.headWritten = true
|
||||
|
||||
w.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Flush - Calls the underlying Flush.
|
||||
|
|
|
@ -3,6 +3,7 @@ package api
|
|||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/mux"
|
||||
|
@ -81,7 +82,9 @@ type (
|
|||
mimeType string
|
||||
|
||||
logResponseWriter struct {
|
||||
sync.Once
|
||||
http.ResponseWriter
|
||||
|
||||
statusCode int
|
||||
}
|
||||
)
|
||||
|
@ -100,8 +103,10 @@ const (
|
|||
var _ = logErrorResponse
|
||||
|
||||
func (lrw *logResponseWriter) WriteHeader(code int) {
|
||||
lrw.Do(func() {
|
||||
lrw.statusCode = code
|
||||
lrw.ResponseWriter.WriteHeader(code)
|
||||
})
|
||||
}
|
||||
|
||||
func setRequestID(h http.Handler) http.Handler {
|
||||
|
@ -139,12 +144,14 @@ func logErrorResponse(l *zap.Logger) mux.MiddlewareFunc {
|
|||
zap.Int("status", lw.statusCode),
|
||||
zap.String("method", mux.CurrentRoute(r).GetName()),
|
||||
zap.String("description", http.StatusText(lw.statusCode)))
|
||||
} else {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
l.Info("call method",
|
||||
zap.Int("status", lw.statusCode),
|
||||
zap.String("method", mux.CurrentRoute(r).GetName()),
|
||||
zap.String("description", http.StatusText(lw.statusCode)))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue