optimize: dns delay & fallback

This commit is contained in:
源文雨 2024-04-18 02:17:02 +09:00
parent 737bec0d2e
commit 4861503f16
2 changed files with 14 additions and 3 deletions

View File

@ -55,7 +55,10 @@ var cli = comandyClient(http.Client{
if len(addrs) == 0 { if len(addrs) == 0 {
addrs, err = resolver.LookupHost(ctx, host) addrs, err = resolver.LookupHost(ctx, host)
if err != nil { if err != nil {
return nil, err addrs, err = net.DefaultResolver.LookupHost(ctx, host)
if err != nil {
return nil, err
}
} }
lookupTable.Set(host, addrs) lookupTable.Set(host, addrs)
} }
@ -80,7 +83,7 @@ var cli = comandyClient(http.Client{
} }
_ = tlsConn.Close() _ = tlsConn.Close()
} }
return tlsConn, nil return tlsConn, err
}, },
}, },
}) })

10
ipv6.go
View File

@ -1,13 +1,21 @@
package main package main
import ( import (
"context"
"net/http" "net/http"
"time"
"github.com/RomiChan/syncx" "github.com/RomiChan/syncx"
) )
var canUseIPv6 = syncx.Lazy[bool]{Init: func() bool { var canUseIPv6 = syncx.Lazy[bool]{Init: func() bool {
resp, err := http.Get("http://v6.ipv6-test.com/json/widgetdata.php?callback=?") ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", "http://v6.ipv6-test.com/json/widgetdata.php?callback=?", nil)
if err != nil {
return false
}
resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
return false return false
} }