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
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 }}

15
dns.go
View File

@ -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

12
http.go
View File

@ -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
}

View File

@ -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)