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

http: record the number of bytes read when response writer is hijacked #6173

Merged
merged 3 commits into from
Apr 17, 2024

Conversation

WeidiDeng
Copy link
Member

Closes 5728.

@WeidiDeng
Copy link
Member Author

@Mygod. What do you think of this?

@Mygod
Copy link

Mygod commented Mar 15, 2024

I no longer uses caddy now but my old use case would really prefer a raw connection for kernel space io.Copy. However, this would probably suffice for caddyserver/forwardproxy and maintainers for that could take a look. :)

Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! Just did a quick first pass and had a question.

modules/caddyhttp/responsewriter.go Show resolved Hide resolved
@mholt
Copy link
Member

mholt commented Mar 21, 2024

It sounds like this is no longer needed... is this a bug fix or mostly a feature? If it's a feature I'm not sure it's worth the complexity especially if it's not needed.

That said, I thought we do kernel-space transfers when possible... 🤔

@WeidiDeng
Copy link
Member Author

I think it's a bug fix. For websockets, number of bytes written is logged but not the number of bytes read. And I can't think of a non-invasive way to record that without adding a new field to the log.

@@ -240,6 +242,12 @@ func (rr *responseRecorder) FlushError() error {
return nil
}

// Private interface so it can only be used in this package
// #TODO: maybe export it later
func (rr *responseRecorder) setReadSize(size *int) {
Copy link
Member

Choose a reason for hiding this comment

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

Passing in a *int is a bit weird. Can we just pass in a int? Any reason we need a pointer at all, actually?

Copy link
Member Author

Choose a reason for hiding this comment

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

This pointer points to the member of the wrapped request body. So when it's updated, the corresponding log entry is updated as well. int requires more changes to the code.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see. Is that thread-safe? (Would it ever be used across goroutines?)

Copy link
Member Author

Choose a reason for hiding this comment

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

The net.Conn interface is not thread-safe by default unless specified explicitly. But the only race I know of is when caddy sends websocket goaway frame, which is not thread-safe in the first place. But clients are expected to reconnect anyway, the stats are a bit off (goaway frames are small), nothing breaking.

Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

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

Thanks for working on this Weidi -- let's give it a shot but I may need your help if something comes up :)

@mholt mholt enabled auto-merge (squash) April 17, 2024 14:55
@mholt mholt merged commit e0daa39 into master Apr 17, 2024
24 of 25 checks passed
@mholt mholt deleted the hijack-read-size branch April 17, 2024 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ⚙️ New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bytes_read for hijacked connections?
4 participants