Skip to content

Commit 4d70677

Browse files
authored
Upgrade to stdlib http (#304)
* Use separate ReverseProxy instances for TCP and IPC apps * Switch to stdlib http.Transport for TCP proxy * Switch to stdlib http.Transport for Unix Domain Socket proxy http.Transport requires a custom DialContext function to be specified in order to create a connection using Unix Domain Sockets. * Update TCP transport to use DialContext Dial is deprecated * Remove unused vendored httpu package * Extract constants for timeout values
1 parent 1952ced commit 4d70677

File tree

9 files changed

+55
-3396
lines changed

9 files changed

+55
-3396
lines changed

dev/http.go

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"io"
@@ -14,7 +15,6 @@ import (
1415
"time"
1516

1617
"github.com/bmizerany/pat"
17-
"github.com/puma/puma-dev/httpu"
1818
)
1919

2020
type HTTPServer struct {
@@ -26,29 +26,60 @@ type HTTPServer struct {
2626
IgnoredStaticPaths []string
2727
Domains []string
2828

29-
mux *pat.PatternServeMux
30-
transport *httpu.Transport
31-
proxy *httputil.ReverseProxy
29+
mux *pat.PatternServeMux
30+
unixTransport *http.Transport
31+
unixProxy *httputil.ReverseProxy
32+
tcpTransport *http.Transport
33+
tcpProxy *httputil.ReverseProxy
3234
}
3335

36+
const dialerTimeout = 5 * time.Second
37+
const keepAlive = 10 * time.Second
38+
const tlsHandshakeTimeout = 10 * time.Second
39+
const expectContinueTimeout = 1 * time.Second
40+
const proxyFlushInternal = 1 * time.Second
41+
3442
func (h *HTTPServer) Setup() {
35-
h.transport = &httpu.Transport{
36-
Dial: (&net.Dialer{
37-
Timeout: 5 * time.Second,
38-
KeepAlive: 10 * time.Second,
39-
}).Dial,
40-
TLSHandshakeTimeout: 10 * time.Second,
41-
ExpectContinueTimeout: 1 * time.Second,
43+
h.unixTransport = &http.Transport{
44+
DialContext: func(ctx context.Context, _, addr string) (net.Conn, error) {
45+
socketPath, _, err := net.SplitHostPort(addr)
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
dialer := net.Dialer{
51+
Timeout: dialerTimeout,
52+
KeepAlive: keepAlive,
53+
}
54+
return dialer.DialContext(ctx, "unix", socketPath)
55+
},
56+
TLSHandshakeTimeout: tlsHandshakeTimeout,
57+
ExpectContinueTimeout: expectContinueTimeout,
4258
}
4359

44-
h.Pool.AppClosed = h.AppClosed
60+
h.unixProxy = &httputil.ReverseProxy{
61+
Director: func(_ *http.Request) {},
62+
Transport: h.unixTransport,
63+
FlushInterval: proxyFlushInternal,
64+
}
4565

46-
h.proxy = &httputil.ReverseProxy{
66+
h.tcpTransport = &http.Transport{
67+
DialContext: (&net.Dialer{
68+
Timeout: dialerTimeout,
69+
KeepAlive: keepAlive,
70+
}).DialContext,
71+
TLSHandshakeTimeout: tlsHandshakeTimeout,
72+
ExpectContinueTimeout: expectContinueTimeout,
73+
}
74+
75+
h.tcpProxy = &httputil.ReverseProxy{
4776
Director: func(_ *http.Request) {},
48-
Transport: h.transport,
49-
FlushInterval: 1 * time.Second,
77+
Transport: h.tcpTransport,
78+
FlushInterval: proxyFlushInternal,
5079
}
5180

81+
h.Pool.AppClosed = h.AppClosed
82+
5283
h.mux = pat.New()
5384

5485
h.mux.Get("/status", http.HandlerFunc(h.status))
@@ -59,7 +90,8 @@ func (h *HTTPServer) AppClosed(app *App) {
5990
// Whenever an app is closed, wipe out all idle conns. This
6091
// obviously closes down more than just this one apps connections
6192
// but that's ok.
62-
h.transport.CloseIdleConnections()
93+
h.unixTransport.CloseIdleConnections()
94+
h.tcpTransport.CloseIdleConnections()
6395
}
6496

6597
func (h *HTTPServer) removeTLD(host string) string {
@@ -151,7 +183,13 @@ func (h *HTTPServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
151183
}
152184

153185
req.URL.Scheme, req.URL.Host = app.Scheme, app.Address()
154-
h.proxy.ServeHTTP(w, req)
186+
if app.Scheme == "httpu" {
187+
req.URL.Scheme, req.URL.Host = "http", app.Address()
188+
h.unixProxy.ServeHTTP(w, req)
189+
} else {
190+
req.URL.Scheme, req.URL.Host = app.Scheme, app.Address()
191+
h.tcpProxy.ServeHTTP(w, req)
192+
}
155193
}
156194

157195
func (h *HTTPServer) shouldServePublicPathForApp(a *App, req *http.Request) bool {

httpu/extra.go

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)