When we run out of connections, handle an extra one synchronously

This commit is contained in:
Frank Denis 2021-08-04 13:35:33 +02:00
parent d996e3424d
commit da69583bd2
1 changed files with 11 additions and 10 deletions

View File

@ -369,14 +369,14 @@ func (proxy *Proxy) udpListener(clientPc *net.UDPConn) {
return
}
packet := buffer[:length]
if !proxy.clientsCountInc() {
dlog.Warnf("Too many incoming connections (max=%d)", proxy.maxClients)
proxy.processIncomingQuery("udp", proxy.mainProto, packet, &clientAddr, clientPc, time.Now()) // handle synchronously
continue
}
go func() {
if !proxy.clientsCountInc() {
dlog.Warnf("Too many incoming connections (max=%d)", proxy.maxClients)
return
}
defer proxy.clientsCountDec()
start := time.Now()
proxy.processIncomingQuery("udp", proxy.mainProto, packet, &clientAddr, clientPc, start)
proxy.processIncomingQuery("udp", proxy.mainProto, packet, &clientAddr, clientPc, time.Now())
}()
}
}
@ -388,12 +388,13 @@ func (proxy *Proxy) tcpListener(acceptPc *net.TCPListener) {
if err != nil {
continue
}
if !proxy.clientsCountInc() {
dlog.Warnf("Too many incoming connections (max=%d)", proxy.maxClients)
clientPc.Close()
continue
}
go func() {
defer clientPc.Close()
if !proxy.clientsCountInc() {
dlog.Warnf("Too many incoming connections (max=%d)", proxy.maxClients)
return
}
defer proxy.clientsCountDec()
if err := clientPc.SetDeadline(time.Now().Add(proxy.timeout)); err != nil {
return