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

net/http: Keep-Alive is not used #50777

Closed
waldreiter opened this issue Jan 24, 2022 · 3 comments
Closed

net/http: Keep-Alive is not used #50777

waldreiter opened this issue Jan 24, 2022 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@waldreiter
Copy link
Contributor

What version of Go are you using (go version)?

go1.17.6 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

A go server that responses with some text larger than 2 kB:

package main

import (
	"fmt"
	"log"
	"net/http"
	"strings"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprint(w, strings.Repeat("@", 2049))
	})

	log.Fatal(http.ListenAndServe(":8080", nil))
}

ApacheBench calls it many times:

ab -c 8 -n 100000 -s 3 -k http://127.0.0.1:8080/

What did you expect to see?

ApacheBench should run successfully.

What did you see instead?

This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 10000 requests
apr_pollset_poll: The timeout specified has expired (70007)
Total of 16366 requests completed

Some observations

  • If fewer requests are done, then it works. But ApacheBench shows that they are not Keep-Alive requests.
  • If the response lenth is 1 byte shorter, so the response is only 2048 bytes long, then the requests are all successful. ApacheBench shows that Keep-Alive requests is used on all of them.
  • I did run ApacheBench against another web server, Actix Web, without that problem.
@mknyszek mknyszek changed the title net/http: KeepAlive is not used net/http: Keep-Alive is not used Jan 24, 2022
@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 24, 2022
@mknyszek mknyszek added this to the Backlog milestone Jan 24, 2022
@mknyszek
Copy link
Contributor

CC @neild

@neild
Copy link
Contributor

neild commented Jan 26, 2022

I haven't dug into precisely what happens in this scenario, but I strongly suspect you'll get the behavior you want if you set a Content-Length header in your handler.

The reason for the change in behavior at 2048 bytes is that that's the cutoff for how much data the server will read from the response body before deciding it doesn't know the content length.

@waldreiter
Copy link
Contributor Author

I tested setting the Content-Length and it worked.

My Actix Web server does not set the Content-Length, but there it works anyway. But I guess that`s life.

Your explanation makes sense. Thank you!

@golang golang locked and limited conversation to collaborators Jan 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants