Skip to content

Commit

Permalink
fix: guard timeout with sync.Mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
youzhixiaomutou committed Dec 7, 2023
1 parent c9f1fc9 commit 86d7f88
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,23 @@ func NewWriter(w gin.ResponseWriter, buf *bytes.Buffer) *Writer {

// Write will write data to response body
func (w *Writer) Write(data []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()

if w.timeout || w.body == nil {
return 0, nil
}

w.mu.Lock()
defer w.mu.Unlock()

return w.body.Write(data)
}

// WriteHeader sends an HTTP response header with the provided status code.
// If the response writer has already written headers or if a timeout has occurred,
// this method does nothing.
func (w *Writer) WriteHeader(code int) {
w.mu.Lock()
defer w.mu.Unlock()

if w.timeout || w.wroteHeaders {
return
}
Expand All @@ -53,9 +56,6 @@ func (w *Writer) WriteHeader(code int) {

checkWriteHeaderCode(code)

w.mu.Lock()
defer w.mu.Unlock()

w.writeHeader(code)
w.ResponseWriter.WriteHeader(code)
}
Expand Down

0 comments on commit 86d7f88

Please sign in to comment.