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

#215 - Response: add support for flushing #218

Merged
merged 4 commits into from
Jul 23, 2024
Merged

Conversation

System-Glitch
Copy link
Member

References

Issue(s): closes #215

Description

  • Make Response implement http.Flusher
  • Call PreWrite only once on the first Write
  • Add CommonWriter to reduce chained writers boilerplate
  • Use CommonWriter for log and compress middleware

@System-Glitch System-Glitch added the enhancement Enhancement of existing feature label Jul 12, 2024
@System-Glitch System-Glitch self-assigned this Jul 12, 2024
@coveralls
Copy link

Pull Request Test Coverage Report for Build 9910117267

Details

  • 33 of 61 (54.1%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.4%) to 96.918%

Changes Missing Coverage Covered Lines Changed/Added Lines %
middleware/compress/compress.go 4 14 28.57%
response.go 23 41 56.1%
Totals Coverage Status
Change from base Build 9807990221: -0.4%
Covered Lines: 6133
Relevant Lines: 6328

💛 - Coveralls

@go-goyave go-goyave deleted a comment from coveralls Jul 12, 2024
response.go Show resolved Hide resolved
middleware/compress/compress.go Show resolved Hide resolved
- Response implement http.Flusher
- Call PreWrite only once on the first Write
- Add CommonWriter to reduce chained writers boilerplate
- Use CommonWriter for log and compress middleware
@System-Glitch System-Glitch changed the title Response: add support for flushing #215 #215 - Response: add support for flushing Jul 16, 2024
@System-Glitch System-Glitch marked this pull request as ready for review July 16, 2024 13:56
@EdmondFrank
Copy link

Awesome, This is exactly what I need right now!

// be called with an empty byte slice.
func (r *Response) Flush() {
if !r.wroteHeader {
r.PreWrite([]byte{})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit surprised by this line, because it will call prewrite, that will indeed send the header, but I feel strange to PreWrite during flush

Shouldn't you simply call WriteHeader ?

Suggested change
r.PreWrite([]byte{})
r.WriteHeader(r.status)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A flush will trigger a write so we want to trigger PreWrite as well. It's only relevant if you call Flush without having written anything (no prior call to Write).

Comment on lines 147 to 149
if !r.wroteHeader {
if r.status == 0 {
r.status = http.StatusOK

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if !r.wroteHeader {
if r.status == 0 {
r.status = http.StatusOK
if !r.IsHeaderWritten {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move the check if 0, then OK into r.WriteHeader

if status == 0 {
   status =  http.StatusOK
}

@System-Glitch System-Glitch merged commit 21f3c8c into master Jul 23, 2024
8 checks passed
@System-Glitch System-Glitch deleted the feature/flusher branch July 23, 2024 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement of existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

http.Flusher implementation
4 participants