This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
plumbing.go
58 lines (52 loc) · 2.13 KB
/
plumbing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// just-install - The simple package installer for Windows
// Copyright (C) 2018 just-install authors.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package fetch
import (
"net"
"net/http"
"time"
)
// ConnectionPhaseTimeout is the timeout used as upper bound for various phases of the connection to
// a remote host (i.e. establishing a TCP connection, TLS handshake, etc). This is kept as short as
// possible to immediately catch transient network errors.
const ConnectionPhaseTimeout = 5 * time.Second
// RequestTimeout is the timeout used as the upper bound for an entire HTTP request, including the
// time needed to download the requested file.
const RequestTimeout = 30 * time.Minute
// Transport is an HTTP transport optimized to perform a sigle request to a single host, with short
// timeouts for various connection phases.
var Transport = &http.Transport{
DialContext: (&net.Dialer{
DualStack: true,
KeepAlive: 0,
Timeout: ConnectionPhaseTimeout,
}).DialContext,
DisableKeepAlives: true,
ExpectContinueTimeout: ConnectionPhaseTimeout,
IdleConnTimeout: ConnectionPhaseTimeout,
MaxConnsPerHost: 1,
Proxy: http.ProxyFromEnvironment,
ResponseHeaderTimeout: ConnectionPhaseTimeout,
TLSHandshakeTimeout: ConnectionPhaseTimeout,
}
// NewClient creates a new HTTP client with a default request timeout (see also `RequestTimeout`)
// that uses our `Transport`. Unlike Go stdlib's HTTP client, ours is to be closed and discarded
// after one request.
func NewClient() *http.Client {
return &http.Client{
Timeout: RequestTimeout,
Transport: Transport,
}
}