Hi gophers,

I'm using netutil.LimitListener to limit the number of concurrent requests
that can be handled in a server to limit the memory usage of the service.
I've noticed that when I send a bunch of requests it serves the number
passed to the LimitListerner but after that it serves n-2 requests. I
wonder what the problem might be, not a big deal but maybe there is some
kind of problem in the library.

Here is the code that I've created to test this problem:

Server (waits 5 seconds before replying with concurrency limited to 10):
package main

import (
"golang.org/x/net/netutil"
"log"
"net"
"net/http"
"time"
)

type fooHandler struct {
}

func (fh *fooHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
time.Sleep(5 * time.Second)
w.Write([]byte("Done"))
}

func main() {
lis, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
lis = netutil.LimitListener(lis, 10)
fH := &fooHandler{}

s := &http.Server{
Handler:        fH,
ReadTimeout:    60 * time.Second,
WriteTimeout:   60 * time.Second,
MaxHeaderBytes: 1 << 20,
}

if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}


Client (Sends 40 concurrent requests to the server):
package main

import (
"fmt"
"io/ioutil"
"net/http"
"sync"
)

func Ask(wg *sync.WaitGroup, n int) {
resp, err := http.Get("http://localhost:8080";)
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(err, string(body), n)
wg.Done()
}
func main() {
wg := &sync.WaitGroup{}
for i := 0; i < 40; i++ {
wg.Add(1)
go Ask(wg, i)
}
wg.Wait()
}

In this example I see 10 responses after the first 5 seconds and then 8
responses every five seconds.

go version go1.8 darwin/amd64

Thanks,
Pablo

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to