E
E
Evgeny Mamonov2017-03-09 14:28:13
go
Evgeny Mamonov, 2017-03-09 14:28:13

Why are net/http requests not processed or how to increase the listen queue?

Good afternoon.
I take a simple script

package main

import (
    "fmt"
    "strconv"
    "net/http"
)

var cnt = 0

func handler(w http.ResponseWriter, r *http.Request) {
    cnt = cnt + 1
    fmt.Fprintf(w, "cnt: " + strconv.Itoa(cnt))
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8100", nil)
}

I run and check # curl http://127.0.0.1:8100/
cnt: 1 - everything works correctly
I do the usual test # ab -c 50 -n 5000 http://127.0.0.1:8100/
again check # curl http://127.0.0.1:8100/
cnt: 4298, but it should be 5002!
At the same time, after the launch, ab - cnt is always different, always less than 5002.
If you reduce the number of simultaneous requests, say to 1-2, then everything is OK, cnt = 5002, as it should be.
I am just starting to learn Go, before that such tasks were done by perl+fastcgi.
The problem is the same there, but if you increase the length of the connection queue (backlog) in the FCGI::OpenSocket(path, backlog) function, say, up to 200, the script can easily withstand 200-300 simultaneous connections during the test ab -c 200 -n 100000 http://127.0.0.1:8000/and cnt returns correctly (100002) I
spent several hours, looked at documentation, net/http source (https://golang.org/src/net/http/server.go) but never found a way to fix it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rustler2000, 2017-03-09
@EvgenyMamonov

Because the race condition.
Use ```atomic.AddUint64(&cnt, 1)```
And yes - you need to look at pending requests by stat ab.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question