How to set http connection limit in Go language

Viewed 45

I have set a limit to the number of simultaneous HTTP connections to be made at any given time. I am using netutil for the same. Below is the code.

func Listen() {
    connectionCount := 10  //Set limit to number of connections
    l, err := net.Listen("tcp", sshListen)
    if err != nil {
        panic(err)
    }

    if useProxyProtocol {
        l = &proxyproto.Listener{
            Listener:           l,
            ProxyHeaderTimeout: time.Second * 10,
        }
    }

    for {
        //Accept connections
        conn, err := l.Accept()
        connectionCount ++
        if err != nil {
            atomic.AddInt64(&stats.Stats.FailedConnections, 1)
            continue
        }
        l = netutil.LimitListener(l, connectionCount)
        log.Infow("Number of connections", log.Fields{"Connections": connectionCount})
        go neconn(conn)
    }
}

This is still accepting more number of connections when trying out the code. Can someone please help with what else can be done?

0 Answers
Related