networking - "localhost: no such host" after 250 connections in Go when using ResponseWriter.Write -



networking - "localhost: no such host" after 250 connections in Go when using ResponseWriter.Write -

i have next http client/server code:

server

func main() { http.handlefunc("/", func(w http.responsewriter, r *http.request) { fmt.println("req: ", r.url) w.write([]byte("ok")) // <== problematic line // w.writeheader(200) // works expected }) log.fatal(http.listenandserve(":5008", nil)) }

client

func main() { client := &http.client{} := 0; < 500; i++ { url := fmt.sprintf("http://localhost:5008/%02d", i) req, _ := http.newrequest("get", url, nil) _, err := client.do(req) if err != nil { fmt.println("error: ", err) } else { fmt.println("success: ", i) } time.sleep(10 * time.millisecond) } }

when run client above against server, after 250 connections next error client.do: error: http://localhost:5008/250: dial tcp: lookup localhost: no such host , no more connections succeed.

if alter line in server w.write([]byte("ok")) ==> w.writeheader(200) there no limit amount of connections , works expected.

what missing here?

you not closing body. when writes server, connection left open because response has not been read yet. when writeheader, response done , connection can reused or closed.

to honest, not know why leaving open connections causes domain lookups fail. based on fact 250 awfully close round number 256, guess there artificial limitation placed os hitting. perhaps max fds allowed 256? seem low, explain problem.

func main() { client := &http.client{} := 0; < 500; i++ { url := fmt.sprintf("http://localhost:5008/%02d", i) req, _ := http.newrequest("get", url, nil) resp, err := client.do(req) if err != nil { fmt.println("error: ", err) } else { fmt.println("success: ", i) resp.body.close() } time.sleep(10 * time.millisecond) } }

networking go

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -