Skip to content

Commit

Permalink
metric: fix invalid type check for http.Flusher (#156)
Browse files Browse the repository at this point in the history
This commit fixes an issue similar to d51a099.
The type check:
```
var x {
   ...
   flusher: w.(http.Flusher),
}
```
panics if `w` does not implement the `http.Flusher`
interface.

The correct way to check whether `w` implements
`http.Flusher` is:
```
var x {
   ...
}
if flusher, ok := w.(http.Flusher); ok {
   x.flusher = flusher
}
```

Signed-off-by: Andreas Auernhammer <[email protected]>
  • Loading branch information
aead authored Sep 29, 2021
1 parent 576e761 commit 283e933
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions internal/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,16 @@ func (m *Metrics) Count(h http.HandlerFunc) http.HandlerFunc {
m.requestActive.Inc()
defer m.requestActive.Dec()

h(&countResponseWriter{
var rw = countResponseWriter{
ResponseWriter: w,
flusher: w.(http.Flusher),
succeeded: m.requestSucceeded,
errored: m.requestErrored,
failed: m.requestFailed,
}, r)
}
if flusher, ok := w.(http.Flusher); ok {
rw.flusher = flusher
}
h(&rw, r)
}
}

Expand All @@ -150,12 +153,15 @@ func (m *Metrics) Count(h http.HandlerFunc) http.HandlerFunc {
// the application can handle.
func (m *Metrics) Latency(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h(&latencyResponseWriter{
var rw = latencyResponseWriter{
ResponseWriter: w,
flusher: w.(http.Flusher),
start: time.Now(),
histogram: m.requestLatency,
}, r)
}
if flusher, ok := w.(http.Flusher); ok {
rw.flusher = flusher
}
h(&rw, r)
}
}

Expand Down

0 comments on commit 283e933

Please sign in to comment.