File size: 998 Bytes
ea775bd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package proxy
import (
"net/http/httptrace"
"sync/atomic"
)
// upstreamRequestTrace captures request-progress milestones reported by
// net/http transport so request-log egress bytes can be committed only when
// the request has actually been written to upstream.
type upstreamRequestTrace struct {
gotConn atomic.Bool
wroteRequest atomic.Bool
}
func newUpstreamRequestTrace() *upstreamRequestTrace {
return &upstreamRequestTrace{}
}
func (t *upstreamRequestTrace) clientTrace() *httptrace.ClientTrace {
return &httptrace.ClientTrace{
GotConn: func(httptrace.GotConnInfo) {
t.gotConn.Store(true)
},
WroteRequest: func(info httptrace.WroteRequestInfo) {
// Only mark as written when transport reports write success.
// WroteRequest can also fire with Err!=nil for failed write attempts.
if info.Err == nil {
t.wroteRequest.Store(true)
}
},
}
}
func (t *upstreamRequestTrace) shouldCommitEgress() bool {
return t.gotConn.Load() && t.wroteRequest.Load()
}
|