Skip to content

Commit

Permalink
Merge branch 'master' of github.com:DNSCrypt/dnscrypt-proxy
Browse files Browse the repository at this point in the history
* 'master' of github.com:DNSCrypt/dnscrypt-proxy:
  add timeout for udp and tcp dialer
  Use blocking channel instead of looping sleep for less CPU usage
  • Loading branch information
jedisct1 committed Dec 13, 2023
2 parents e782207 + 0f1f635 commit 898ded9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 6 additions & 3 deletions dnscrypt-proxy/coldstart.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ type CaptivePortalMap map[string]CaptivePortalEntryIPs
type CaptivePortalHandler struct {
cancelChannel chan struct{}
countChannel chan struct{}
waitChannel chan struct{}
channelCount int
}

func (captivePortalHandler *CaptivePortalHandler) Stop() {
close(captivePortalHandler.cancelChannel)
for len(captivePortalHandler.countChannel) < captivePortalHandler.channelCount {
time.Sleep(10 * time.Millisecond)
}
<-captivePortalHandler.waitChannel
close(captivePortalHandler.countChannel)
}

Expand Down Expand Up @@ -138,6 +137,9 @@ func addColdStartListener(
}
clientPc.Close()
captivePortalHandler.countChannel <- struct{}{}
if len(captivePortalHandler.countChannel) == captivePortalHandler.channelCount {
close(captivePortalHandler.waitChannel)
}
}()
return nil
}
Expand Down Expand Up @@ -186,6 +188,7 @@ func ColdStart(proxy *Proxy) (*CaptivePortalHandler, error) {
captivePortalHandler := CaptivePortalHandler{
cancelChannel: make(chan struct{}),
countChannel: make(chan struct{}, len(listenAddrStrs)),
waitChannel: make(chan struct{}),
channelCount: 0,
}
for _, listenAddrStr := range listenAddrStrs {
Expand Down
4 changes: 2 additions & 2 deletions dnscrypt-proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ func (proxy *Proxy) exchangeWithUDPServer(
var pc net.Conn
proxyDialer := proxy.xTransport.proxyDialer
if proxyDialer == nil {
pc, err = net.DialUDP("udp", nil, upstreamAddr)
pc, err = net.DialTimeout("udp", upstreamAddr.String(), serverInfo.Timeout)
} else {
pc, err = (*proxyDialer).Dial("udp", upstreamAddr.String())
}
Expand Down Expand Up @@ -560,7 +560,7 @@ func (proxy *Proxy) exchangeWithTCPServer(
var pc net.Conn
proxyDialer := proxy.xTransport.proxyDialer
if proxyDialer == nil {
pc, err = net.DialTCP("tcp", nil, upstreamAddr)
pc, err = net.DialTimeout("tcp", upstreamAddr.String(), serverInfo.Timeout)
} else {
pc, err = (*proxyDialer).Dial("tcp", upstreamAddr.String())
}
Expand Down

0 comments on commit 898ded9

Please sign in to comment.