1
0
mirror of https://github.com/DNSCrypt/dnscrypt-proxy.git synced 2024-12-11 22:35:40 +01:00

Optimize stopping CaptivePortalHandler - 2 (#2155)

1. Fix early return that triggers port rebinding error by 8e46f447.
2. Reduce waiting time while there are multiple listen_addresses.
This commit is contained in:
lifenjoiner 2022-07-19 18:35:52 +08:00 committed by GitHub
parent 8e46f44799
commit 91388b148c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,13 +15,17 @@ type CaptivePortalEntryIPs []net.IP
type CaptivePortalMap map[string]CaptivePortalEntryIPs type CaptivePortalMap map[string]CaptivePortalEntryIPs
type CaptivePortalHandler struct { type CaptivePortalHandler struct {
cancelChannels []chan struct{} cancelChannel chan struct{}
countChannel chan struct{}
channelCount int
} }
func (captivePortalHandler *CaptivePortalHandler) Stop() { func (captivePortalHandler *CaptivePortalHandler) Stop() {
for _, cancelChannel := range captivePortalHandler.cancelChannels { close(captivePortalHandler.cancelChannel)
close(cancelChannel) for len(captivePortalHandler.countChannel) < captivePortalHandler.channelCount {
time.Sleep(time.Millisecond)
} }
close(captivePortalHandler.countChannel)
} }
func (ipsMap *CaptivePortalMap) GetEntry(msg *dns.Msg) (*dns.Question, *CaptivePortalEntryIPs) { func (ipsMap *CaptivePortalMap) GetEntry(msg *dns.Msg) (*dns.Question, *CaptivePortalEntryIPs) {
@ -119,7 +123,7 @@ func addColdStartListener(
proxy *Proxy, proxy *Proxy,
ipsMap *CaptivePortalMap, ipsMap *CaptivePortalMap,
listenAddrStr string, listenAddrStr string,
cancelChannel chan struct{}, captivePortalHandler *CaptivePortalHandler,
) error { ) error {
listenUDPAddr, err := net.ResolveUDPAddr("udp", listenAddrStr) listenUDPAddr, err := net.ResolveUDPAddr("udp", listenAddrStr)
if err != nil { if err != nil {
@ -130,9 +134,10 @@ func addColdStartListener(
return err return err
} }
go func() { go func() {
for !handleColdStartClient(clientPc, cancelChannel, ipsMap) { for !handleColdStartClient(clientPc, captivePortalHandler.cancelChannel, ipsMap) {
} }
clientPc.Close() clientPc.Close()
captivePortalHandler.countChannel <- struct{}{}
}() }()
return nil return nil
} }
@ -178,15 +183,15 @@ func ColdStart(proxy *Proxy) (*CaptivePortalHandler, error) {
ipsMap[name] = ips ipsMap[name] = ips
} }
listenAddrStrs := proxy.listenAddresses listenAddrStrs := proxy.listenAddresses
cancelChannels := make([]chan struct{}, 0)
for _, listenAddrStr := range listenAddrStrs {
cancelChannel := make(chan struct{})
if err := addColdStartListener(proxy, &ipsMap, listenAddrStr, cancelChannel); err == nil {
cancelChannels = append(cancelChannels, cancelChannel)
}
}
captivePortalHandler := CaptivePortalHandler{ captivePortalHandler := CaptivePortalHandler{
cancelChannels: cancelChannels, cancelChannel: make(chan struct{}),
countChannel: make(chan struct{}, len(listenAddrStrs)),
channelCount: 0,
}
for _, listenAddrStr := range listenAddrStrs {
if err := addColdStartListener(proxy, &ipsMap, listenAddrStr, &captivePortalHandler); err == nil {
captivePortalHandler.channelCount++
}
} }
proxy.captivePortalMap = &ipsMap proxy.captivePortalMap = &ipsMap
return &captivePortalHandler, nil return &captivePortalHandler, nil