proxy: fix zero duration for non streaming responses (#723)
Updates #654
This commit is contained in:
@@ -578,15 +578,11 @@ func newBodyCopier(w gin.ResponseWriter) *responseBodyCopier {
|
|||||||
ResponseWriter: w,
|
ResponseWriter: w,
|
||||||
body: bodyBuffer,
|
body: bodyBuffer,
|
||||||
tee: io.MultiWriter(w, bodyBuffer),
|
tee: io.MultiWriter(w, bodyBuffer),
|
||||||
|
start: time.Now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *responseBodyCopier) Write(b []byte) (int, error) {
|
func (w *responseBodyCopier) Write(b []byte) (int, error) {
|
||||||
if w.start.IsZero() {
|
|
||||||
w.start = time.Now()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Single write operation that writes to both the response and buffer
|
|
||||||
return w.tee.Write(b)
|
return w.tee.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -490,15 +490,11 @@ func TestMetricsMonitor_ResponseBodyCopier(t *testing.T) {
|
|||||||
assert.Equal(t, string(testData), rec.Body.String())
|
assert.Equal(t, string(testData), rec.Body.String())
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("sets start time on first write", func(t *testing.T) {
|
t.Run("sets start time on creation", func(t *testing.T) {
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
ginCtx, _ := gin.CreateTestContext(rec)
|
ginCtx, _ := gin.CreateTestContext(rec)
|
||||||
copier := newBodyCopier(ginCtx.Writer)
|
copier := newBodyCopier(ginCtx.Writer)
|
||||||
|
|
||||||
assert.True(t, copier.StartTime().IsZero())
|
|
||||||
|
|
||||||
copier.Write([]byte("test"))
|
|
||||||
|
|
||||||
assert.False(t, copier.StartTime().IsZero())
|
assert.False(t, copier.StartTime().IsZero())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user