Tune HTTP client timeouts and use Cloudflare DNSUnverified
6be8ad9 parent: 12e3cec modified
internal/feed/fetcher.go +2 -2 | @@ -25,10 +25,10 @@ type Fetcher struct { | ||
| 25 | 25 | func NewFetcher() *Fetcher { |
| 26 | 26 | return &Fetcher{ |
| 27 | 27 | httpClient: &http.Client{ |
| 28 | - Timeout: 30 * time.Second, | |
| 28 | + Timeout: 10 * time.Second, | |
| 29 | 29 | Transport: httpclient.NewTransport(), |
| 30 | 30 | CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 31 | - if len(via) >= 10 { | |
| 31 | + if len(via) >= 5 { | |
| 32 | 32 | return fmt.Errorf("too many redirects") |
| 33 | 33 | } |
| 34 | 34 | return nil |
| @@ -25,10 +25,10 @@ type Fetcher struct { | |||
| 25 | func NewFetcher() *Fetcher { | 25 | func NewFetcher() *Fetcher { |
| 26 | return &Fetcher{ | 26 | return &Fetcher{ |
| 27 | httpClient: &http.Client{ | 27 | httpClient: &http.Client{ |
| 28 | - Timeout: 30 * time.Second, | 28 | + Timeout: 10 * time.Second, |
| 29 | Transport: httpclient.NewTransport(), | 29 | Transport: httpclient.NewTransport(), |
| 30 | CheckRedirect: func(req *http.Request, via []*http.Request) error { | 30 | CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 31 | - if len(via) >= 10 { | 31 | + if len(via) >= 5 { |
| 32 | return fmt.Errorf("too many redirects") | 32 | return fmt.Errorf("too many redirects") |
| 33 | } | 33 | } |
| 34 | return nil | 34 | return nil |
modified
internal/httpclient/httpclient.go +17 -6 | @@ -16,16 +16,27 @@ const ( | ||
| 16 | 16 | AcceptHTML = "text/html,application/xhtml+xml;q=0.9" |
| 17 | 17 | ) |
| 18 | 18 | |
| 19 | +var dnsResolver = &net.Resolver{ | |
| 20 | + PreferGo: true, | |
| 21 | + Dial: func(ctx context.Context, network, address string) (net.Conn, error) { | |
| 22 | + d := net.Dialer{Timeout: 3 * time.Second} | |
| 23 | + return d.DialContext(ctx, "udp", "1.1.1.1:53") | |
| 24 | + }, | |
| 25 | +} | |
| 26 | + | |
| 19 | 27 | func NewTransport() *http.Transport { |
| 28 | + dialer := &net.Dialer{ | |
| 29 | + Timeout: 5 * time.Second, | |
| 30 | + KeepAlive: 15 * time.Second, | |
| 31 | + FallbackDelay: 300 * time.Millisecond, | |
| 32 | + Resolver: dnsResolver, | |
| 33 | + } | |
| 34 | + | |
| 20 | 35 | return &http.Transport{ |
| 21 | - DialContext: (&net.Dialer{ | |
| 22 | - Timeout: 10 * time.Second, | |
| 23 | - KeepAlive: 15 * time.Second, | |
| 24 | - FallbackDelay: 300 * time.Millisecond, | |
| 25 | - }).DialContext, | |
| 36 | + DialContext: dialer.DialContext, | |
| 26 | 37 | MaxIdleConns: 50, |
| 27 | 38 | IdleConnTimeout: 10 * time.Second, |
| 28 | - TLSHandshakeTimeout: 10 * time.Second, | |
| 39 | + TLSHandshakeTimeout: 5 * time.Second, | |
| 29 | 40 | ForceAttemptHTTP2: true, |
| 30 | 41 | } |
| 31 | 42 | } |
| @@ -16,16 +16,27 @@ const ( | |||
| 16 | AcceptHTML = "text/html,application/xhtml+xml;q=0.9" | 16 | AcceptHTML = "text/html,application/xhtml+xml;q=0.9" |
| 17 | ) | 17 | ) |
| 18 | 18 | ||
| 19 | +var dnsResolver = &net.Resolver{ | ||
| 20 | + PreferGo: true, | ||
| 21 | + Dial: func(ctx context.Context, network, address string) (net.Conn, error) { | ||
| 22 | + d := net.Dialer{Timeout: 3 * time.Second} | ||
| 23 | + return d.DialContext(ctx, "udp", "1.1.1.1:53") | ||
| 24 | + }, | ||
| 25 | +} | ||
| 26 | + | ||
| 19 | func NewTransport() *http.Transport { | 27 | func NewTransport() *http.Transport { |
| 28 | + dialer := &net.Dialer{ | ||
| 29 | + Timeout: 5 * time.Second, | ||
| 30 | + KeepAlive: 15 * time.Second, | ||
| 31 | + FallbackDelay: 300 * time.Millisecond, | ||
| 32 | + Resolver: dnsResolver, | ||
| 33 | + } | ||
| 34 | + | ||
| 20 | return &http.Transport{ | 35 | return &http.Transport{ |
| 21 | - DialContext: (&net.Dialer{ | 36 | + DialContext: dialer.DialContext, |
| 22 | - Timeout: 10 * time.Second, | ||
| 23 | - KeepAlive: 15 * time.Second, | ||
| 24 | - FallbackDelay: 300 * time.Millisecond, | ||
| 25 | - }).DialContext, | ||
| 26 | MaxIdleConns: 50, | 37 | MaxIdleConns: 50, |
| 27 | IdleConnTimeout: 10 * time.Second, | 38 | IdleConnTimeout: 10 * time.Second, |
| 28 | - TLSHandshakeTimeout: 10 * time.Second, | 39 | + TLSHandshakeTimeout: 5 * time.Second, |
| 29 | ForceAttemptHTTP2: true, | 40 | ForceAttemptHTTP2: true, |
| 30 | } | 41 | } |
| 31 | } | 42 | } |