Просмотр исходного кода

Fix: don't attempt second lookup when context is cancelled

Rod Hynes 4 лет назад
Родитель
Сommit
83a68bcb5a
1 измененных файлов с 14 добавлено и 6 удалено
  1. 14 6
      psiphon/LookupIP.go

+ 14 - 6
psiphon/LookupIP.go

@@ -50,23 +50,31 @@ func LookupIP(ctx context.Context, host string, config *DialConfig) ([]net.IP, e
 
 		ips, err := bindLookupIP(ctx, host, dnsServer, config)
 		if err == nil {
-			if len(ips) == 0 {
-				err = std_errors.New("empty address list")
-			} else {
-				return ips, err
+			if len(ips) > 0 {
+				return ips, nil
 			}
+			err = std_errors.New("empty address list")
+		}
+
+		if ctx.Err() != nil {
+			// Don't fall through to secondary when the context is cancelled.
+			return ips, errors.Trace(err)
 		}
 
 		dnsServer = config.DnsServerGetter.GetSecondaryDnsServer()
 		if dnsServer == "" {
-			return ips, err
+			return ips, errors.Trace(err)
 		}
 
 		if GetEmitNetworkParameters() {
 			NoticeWarning("retry resolve host %s: %s", host, err)
 		}
 
-		return bindLookupIP(ctx, host, dnsServer, config)
+		ips, err = bindLookupIP(ctx, host, dnsServer, config)
+		if err != nil {
+			return nil, errors.Trace(err)
+		}
+		return ips, nil
 	}
 
 	addrs, err := net.DefaultResolver.LookupIPAddr(ctx, host)