Skip to content

Commit

Permalink
proxy: add timeouts to client and server
Browse files Browse the repository at this point in the history
  • Loading branch information
dullgiulio committed Mar 13, 2020
1 parent 6cc6f28 commit 66dfccb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 3 additions & 1 deletion main.go
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"os/signal"
"strings"
"time"
)

func makeSelfTestServers() (string, string) {
Expand Down Expand Up @@ -71,6 +72,7 @@ func main() {
dump := flag.String("dump", "", "Dump request/responses from mirrors in file; empty disables dumping, '-' means stdout")
dumpProxy := flag.Bool("dump-proxy", false, "Also dump proxied request to -dump file")
maxConn := flag.Int("max-conn", 200, "Maximum number of connections to upstream servers")
timeout := flag.Duration("mirror-timeout", 10*time.Second, "Timeout to send requests to mirrors")
retries := flag.Int("mirror-retries", 3, "Maximum number of retries against mirrors")

flag.VisitAll(prefixEnv("PRISM", os.Getenv))
Expand Down Expand Up @@ -100,7 +102,7 @@ func main() {
<-healthStarted
}

client := makeClient(*insecure, *maxConn)
client := makeClient(*insecure, *maxConn, *timeout)
proxy := newProxy(metrics, ms, *listen, *retries, client, *dump, *dumpProxy, proxyURL, proxyBuf)

exited := handleSigterm(func() {
Expand Down
11 changes: 8 additions & 3 deletions proxy.go
Expand Up @@ -15,6 +15,7 @@ import (
"os"
"strings"
"sync"
"time"
)

func hostWithoutPort(host string) string {
Expand Down Expand Up @@ -227,14 +228,15 @@ func (m *mirrorTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return resp, nil
}

func makeClient(insecure bool, maxConn int) *http.Client {
func makeClient(insecure bool, maxConn int, timeout time.Duration) *http.Client {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.ForceAttemptHTTP2 = false
transport.MaxIdleConns = maxConn
if insecure {
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
return &http.Client{
Timeout: timeout,
Transport: transport,
}
}
Expand Down Expand Up @@ -271,8 +273,11 @@ func newProxy(metrics *metrics, ms map[string]*url.URL, listen string, retries i
upstream := httputil.NewSingleHostReverseProxy(proxyURL)
upstream.Transport = mt
srv := &http.Server{
Addr: listen,
Handler: upstream,
Addr: listen,
Handler: upstream,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 10 * time.Second,
}
return &proxy{
srv: srv,
Expand Down

0 comments on commit 66dfccb

Please sign in to comment.