I have this server in golang :
package main
import (
"fmt"
"net/http"
)
func hello(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(204)
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
w.Header().Set("Connection", "close")
fmt.Println(r.Close)
}
func main() {
http.HandleFunc("/", hello)
http.ListenAndServe(":8080", nil)
}
Then I wanted to try to quickly benchmark how it can handle requests whit this python script:
import requests
payload = "test"
while True:
r = requests.post("http://127.0.0.1:8080/test", data = payload)
r.connection.close()
After multiple loops it cannot assign new ports. I deduce that my server doesn't close connection (or my client).
How should I manage connection from server side?
You're running out of ports.
Since you're setting w.Header().Set("Connection", "close")
each request is going to take a new connection entirely. If you don't want to use up ports, re-use the connections.
In your client, you're closing entire connection pool, which also will use up more resources. Call r.close()
to return the connection to the pool and it will be reused if possible. Even though the response is small, it's still good practice to ensure the client consumes the entire response body. If the server can't flush its send buffers, it won't be able to efficiently handle the connections either, and it may take longer to close them.