From da1b7aad34ad847d389333769d6f9b4acde90148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Wed, 17 Apr 2024 00:31:31 +0900 Subject: [PATCH] optimize(dns): dialer timeout --- .github/workflows/test.yml | 3 +++ dns.go | 15 ++++++++++----- http.go | 12 ++++++------ http_test.go | 1 + 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 16eb592..b10c83a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -19,6 +19,9 @@ jobs: - name: Setup Go Environment uses: actions/setup-go@master + - name: Run Go Test + run: go test $(go list ./...) + - name: Test Build env: NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }} diff --git a/dns.go b/dns.go index f371305..8ec4633 100644 --- a/dns.go +++ b/dns.go @@ -5,10 +5,15 @@ import ( "crypto/tls" "net" "sync" + "time" "github.com/fumiama/terasu" ) +var dnsdialer = net.Dialer{ + Timeout: time.Second * 8, +} + type dnsstat struct { A string E bool @@ -49,15 +54,15 @@ func (ds *dnsservers) dial(ctx context.Context) (tlsConn *tls.Conn, err error) { ds.RLock() defer ds.RUnlock() - if dialer.Timeout != 0 { + if dnsdialer.Timeout != 0 { var cancel context.CancelFunc - ctx, cancel = context.WithTimeout(ctx, dialer.Timeout) + ctx, cancel = context.WithTimeout(ctx, dnsdialer.Timeout) defer cancel() } - if !dialer.Deadline.IsZero() { + if !dnsdialer.Deadline.IsZero() { var cancel context.CancelFunc - ctx, cancel = context.WithDeadline(ctx, dialer.Deadline) + ctx, cancel = context.WithDeadline(ctx, dnsdialer.Deadline) defer cancel() } @@ -67,7 +72,7 @@ func (ds *dnsservers) dial(ctx context.Context) (tlsConn *tls.Conn, err error) { if !addr.E { continue } - conn, err = dialer.DialContext(ctx, "tcp", addr.A) + conn, err = dnsdialer.DialContext(ctx, "tcp", addr.A) if err != nil { addr.E = false // no need to acquire write lock continue diff --git a/http.go b/http.go index ee7d003..bf6b1e0 100644 --- a/http.go +++ b/http.go @@ -20,7 +20,7 @@ import ( "golang.org/x/net/http2" ) -var dialer = net.Dialer{ +var httpdialer = net.Dialer{ Timeout: time.Minute, } @@ -31,15 +31,15 @@ type comandyClient http.Client var cli = comandyClient(http.Client{ Transport: &http2.Transport{ 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 - ctx, cancel = context.WithTimeout(ctx, dialer.Timeout) + ctx, cancel = context.WithTimeout(ctx, httpdialer.Timeout) defer cancel() } - if !dialer.Deadline.IsZero() { + if !httpdialer.Deadline.IsZero() { var cancel context.CancelFunc - ctx, cancel = context.WithDeadline(ctx, dialer.Deadline) + ctx, cancel = context.WithDeadline(ctx, httpdialer.Deadline) defer cancel() } @@ -65,7 +65,7 @@ var cli = comandyClient(http.Client{ } else { a += ":" + port } - conn, err := dialer.DialContext(ctx, network, a) + conn, err := httpdialer.DialContext(ctx, network, a) if err != nil { continue } diff --git a/http_test.go b/http_test.go index 28a580c..e42f3cb 100644 --- a/http_test.go +++ b/http_test.go @@ -13,6 +13,7 @@ func TestClientGet(t *testing.T) { if err != nil { t.Fatal(err) } + req.Header.Add("user-agent", "COPY/2.1.7") resp, err := (*http.Client)(&cli).Do(req) if err != nil { t.Fatal(err)