Skip to content

Commit 7e703be

Browse files
committed
feat: tune number of connections while proxying
1 parent ad80bd7 commit 7e703be

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ clean:
3636
rm -f ./vproxy*
3737
rm -rf ./dist/
3838

39-
install: vproxy
39+
install: build
4040
sudo cp -a ./vproxy /usr/local/bin/vproxy

simpleproxy.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ var badGatewayMessage = `<html>
2323
// proxyTransport is a simple http.RoundTripper implementation which returns a
2424
// 503 on any error making a request to the upstream (backend) service
2525
type proxyTransport struct {
26-
errMsg string
26+
transport *http.Transport
27+
errMsg string
2728
}
2829

2930
func (t *proxyTransport) RoundTrip(request *http.Request) (*http.Response, error) {
@@ -34,7 +35,7 @@ func (t *proxyTransport) RoundTrip(request *http.Request) (*http.Response, error
3435
)
3536

3637
operation := func() error {
37-
response, err = http.DefaultTransport.RoundTrip(request)
38+
response, err = t.transport.RoundTrip(request)
3839
// Handle Retry-After here, if you wish...
3940
// If err is nil, no retry will occur
4041
return err
@@ -59,7 +60,13 @@ func (t *proxyTransport) RoundTrip(request *http.Request) (*http.Response, error
5960
}
6061

6162
func createProxyTransport(targetURL url.URL, vhost string) *proxyTransport {
62-
return &proxyTransport{errMsg: fmt.Sprintf(badGatewayMessage, targetURL.String(), vhost)}
63+
fmt.Println("creating new transport")
64+
t := &proxyTransport{errMsg: fmt.Sprintf(badGatewayMessage, targetURL.String(), vhost)}
65+
t.transport = http.DefaultTransport.(*http.Transport).Clone()
66+
t.transport.MaxConnsPerHost = 0 // unlim
67+
t.transport.MaxIdleConns = 800
68+
t.transport.MaxIdleConnsPerHost = 8
69+
return t
6370
}
6471

6572
// CreateProxy with custom http.RoundTripper impl. Sets proper host headers

0 commit comments

Comments
 (0)