optimize(dns): dialer timeout

This commit is contained in:
源文雨 2024-04-17 00:31:31 +09:00
parent e842e5b521
commit da1b7aad34
4 changed files with 20 additions and 11 deletions

View File

@ -19,6 +19,9 @@ jobs:
- name: Setup Go Environment - name: Setup Go Environment
uses: actions/setup-go@master uses: actions/setup-go@master
- name: Run Go Test
run: go test $(go list ./...)
- name: Test Build - name: Test Build
env: env:
NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }} NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}

15
dns.go
View File

@ -5,10 +5,15 @@ import (
"crypto/tls" "crypto/tls"
"net" "net"
"sync" "sync"
"time"
"github.com/fumiama/terasu" "github.com/fumiama/terasu"
) )
var dnsdialer = net.Dialer{
Timeout: time.Second * 8,
}
type dnsstat struct { type dnsstat struct {
A string A string
E bool E bool
@ -49,15 +54,15 @@ func (ds *dnsservers) dial(ctx context.Context) (tlsConn *tls.Conn, err error) {
ds.RLock() ds.RLock()
defer ds.RUnlock() defer ds.RUnlock()
if dialer.Timeout != 0 { if dnsdialer.Timeout != 0 {
var cancel context.CancelFunc var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, dialer.Timeout) ctx, cancel = context.WithTimeout(ctx, dnsdialer.Timeout)
defer cancel() defer cancel()
} }
if !dialer.Deadline.IsZero() { if !dnsdialer.Deadline.IsZero() {
var cancel context.CancelFunc var cancel context.CancelFunc
ctx, cancel = context.WithDeadline(ctx, dialer.Deadline) ctx, cancel = context.WithDeadline(ctx, dnsdialer.Deadline)
defer cancel() defer cancel()
} }
@ -67,7 +72,7 @@ func (ds *dnsservers) dial(ctx context.Context) (tlsConn *tls.Conn, err error) {
if !addr.E { if !addr.E {
continue continue
} }
conn, err = dialer.DialContext(ctx, "tcp", addr.A) conn, err = dnsdialer.DialContext(ctx, "tcp", addr.A)
if err != nil { if err != nil {
addr.E = false // no need to acquire write lock addr.E = false // no need to acquire write lock
continue continue

12
http.go
View File

@ -20,7 +20,7 @@ import (
"golang.org/x/net/http2" "golang.org/x/net/http2"
) )
var dialer = net.Dialer{ var httpdialer = net.Dialer{
Timeout: time.Minute, Timeout: time.Minute,
} }
@ -31,15 +31,15 @@ type comandyClient http.Client
var cli = comandyClient(http.Client{ var cli = comandyClient(http.Client{
Transport: &http2.Transport{ Transport: &http2.Transport{
DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) { DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) {
if dialer.Timeout != 0 { if httpdialer.Timeout != 0 {
var cancel context.CancelFunc var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, dialer.Timeout) ctx, cancel = context.WithTimeout(ctx, httpdialer.Timeout)
defer cancel() defer cancel()
} }
if !dialer.Deadline.IsZero() { if !httpdialer.Deadline.IsZero() {
var cancel context.CancelFunc var cancel context.CancelFunc
ctx, cancel = context.WithDeadline(ctx, dialer.Deadline) ctx, cancel = context.WithDeadline(ctx, httpdialer.Deadline)
defer cancel() defer cancel()
} }
@ -65,7 +65,7 @@ var cli = comandyClient(http.Client{
} else { } else {
a += ":" + port a += ":" + port
} }
conn, err := dialer.DialContext(ctx, network, a) conn, err := httpdialer.DialContext(ctx, network, a)
if err != nil { if err != nil {
continue continue
} }

View File

@ -13,6 +13,7 @@ func TestClientGet(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
req.Header.Add("user-agent", "COPY/2.1.7")
resp, err := (*http.Client)(&cli).Do(req) resp, err := (*http.Client)(&cli).Do(req)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)