1

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?

frogatto
  • 28,539
  • 11
  • 83
  • 129
Jerome Guiard
  • 131
  • 1
  • 1
  • 4

1 Answers1

1

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.

JimB
  • 104,193
  • 13
  • 262
  • 255
  • Thank you for your answer : fmt.Println(r.Close) is used in order to check if the connection is close or not, the result is that as you mentionned I am running out of available ports. I was looking for a method to just release the connection after the request. – Jerome Guiard May 25 '15 at 08:06
  • @JeromeGuiard: Are you certain the ports are still open? If so, what state are they in? What OS are you using? You're probably just cycling though ephemeral ports faster than the OS will return them. – JimB May 25 '15 at 13:13
  • I am using Ubuntu. when I use netstat there is a lot of connection to my port 8080 (http_alt) as Foreign Address with the status TIME_WAIT – Jerome Guiard May 25 '15 at 15:14
  • @JeromeGuiard: Your ports are closed, and in TIME_WAIT. You can prevent that by allowing allowing both sides to keep the connections open so they can reused. – JimB May 25 '15 at 18:27
  • If you're doing http client requests, see https://stackoverflow.com/questions/33238518/what-could-happen-if-i-dont-close-response-body – bithavoc May 25 '22 at 14:00