Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make set/read timeout and write/put buffer atomic #65

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading