mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-06-13 15:47:38 +08:00
feat: add robust transport cloning and improve header handling in reverse proxy
This commit is contained in:
parent
20dc6e4047
commit
dcdb1504a3
3 changed files with 68 additions and 6 deletions
|
|
@ -6,8 +6,10 @@ package touka
|
|||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
_ "unsafe"
|
||||
|
||||
"golang.org/x/net/http2"
|
||||
|
|
@ -34,7 +36,7 @@ func configureHTTP2ExtendedConnectServer(srv *http.Server) error {
|
|||
|
||||
func newHTTP2ExtendedConnectTransport() http.RoundTripper {
|
||||
enableHTTP2ExtendedConnectProtocol()
|
||||
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
transport := cloneDefaultTransport()
|
||||
transport.Protocols = new(http.Protocols)
|
||||
transport.Protocols.SetHTTP1(true)
|
||||
transport.Protocols.SetHTTP2(true)
|
||||
|
|
@ -46,7 +48,7 @@ func newHTTP1BridgeTransport() http.RoundTripper {
|
|||
}
|
||||
|
||||
func newHTTP1BridgeTransportWithTLSConfig(tlsConfig *tls.Config) http.RoundTripper {
|
||||
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
transport := cloneDefaultTransport()
|
||||
transport.Protocols = new(http.Protocols)
|
||||
transport.Protocols.SetHTTP1(true)
|
||||
transport.TLSClientConfig = tlsConfig
|
||||
|
|
@ -60,8 +62,26 @@ func newHTTP1BridgeTransportWithTLSConfig(tlsConfig *tls.Config) http.RoundTripp
|
|||
}
|
||||
|
||||
func newH2CTransport() http.RoundTripper {
|
||||
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
transport := cloneDefaultTransport()
|
||||
transport.Protocols = new(http.Protocols)
|
||||
transport.Protocols.SetUnencryptedHTTP2(true)
|
||||
return transport
|
||||
}
|
||||
|
||||
func cloneDefaultTransport() *http.Transport {
|
||||
if transport, ok := http.DefaultTransport.(*http.Transport); ok {
|
||||
return transport.Clone()
|
||||
}
|
||||
return &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext,
|
||||
ForceAttemptHTTP2: true,
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue