Skip to content

Commit

Permalink
acquire the lock only once when writing
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu committed Jul 24, 2024
1 parent e197b1d commit 6946ddd
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func NewRespWriterWrapper(w http.ResponseWriter, onWrite func(int64)) *RespWrite
// Write writes the bytes array into the [ResponseWriter], and tracks the
// number of bytes written and last error.
func (w *RespWriterWrapper) Write(p []byte) (int, error) {
w.WriteHeader(http.StatusOK)

w.mu.RLock()
defer w.mu.RUnlock()

w.writeHeader(http.StatusOK)

n, err := w.ResponseWriter.Write(p)
n1 := int64(n)
w.OnWrite(n1)
Expand All @@ -63,6 +63,14 @@ func (w *RespWriterWrapper) WriteHeader(statusCode int) {
w.mu.Lock()
defer w.mu.Unlock()

w.writeHeader(statusCode)
}

// writeHeader persists the status code for span attribution, and propagates
// the call to the underlying ResponseWriter.
// It does not acquire a lock, and therefore assumes that is being handled by a
// parent method.
func (w *RespWriterWrapper) writeHeader(statusCode int) {
if !w.wroteHeader {
w.wroteHeader = true
w.statusCode = statusCode
Expand Down

0 comments on commit 6946ddd

Please sign in to comment.