From da69583bd210c9c110279314ff2d85fca808710b Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 4 Aug 2021 13:35:33 +0200 Subject: [PATCH] When we run out of connections, handle an extra one synchronously --- dnscrypt-proxy/proxy.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/dnscrypt-proxy/proxy.go b/dnscrypt-proxy/proxy.go index 891f83a1..e438a8cd 100644 --- a/dnscrypt-proxy/proxy.go +++ b/dnscrypt-proxy/proxy.go @@ -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