Skip to content

Commit

Permalink
fix(timeout): make set/read timeout and write/put buffer atomic (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
youzhixiaomutou authored Feb 18, 2024
1 parent 51275ed commit c6e7905
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 c6e7905

Please sign in to comment.