mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 12:42:34 +02:00
refactor: improved reverse proxy performance, reduce memory allocation calls
This commit is contained in:
parent
3424cc4e51
commit
d315710310
4 changed files with 140 additions and 69 deletions
|
@ -4,12 +4,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
ConnectionTimeout = 5 * time.Second
|
|
||||||
DialTimeout = 3 * time.Second
|
|
||||||
KeepAlive = 60 * time.Second
|
|
||||||
)
|
|
||||||
|
|
||||||
// file, folder structure
|
// file, folder structure
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -30,6 +24,8 @@ const (
|
||||||
ComposeExampleFileName = "compose.example.yml"
|
ComposeExampleFileName = "compose.example.yml"
|
||||||
|
|
||||||
ErrorPagesBasePath = "error_pages"
|
ErrorPagesBasePath = "error_pages"
|
||||||
|
|
||||||
|
AgentCertsBasePath = "certs"
|
||||||
)
|
)
|
||||||
|
|
||||||
var RequiredDirectories = []string{
|
var RequiredDirectories = []string{
|
||||||
|
@ -48,5 +44,3 @@ const (
|
||||||
StopTimeoutDefault = "30s"
|
StopTimeoutDefault = "30s"
|
||||||
StopMethodDefault = "stop"
|
StopMethodDefault = "stop"
|
||||||
)
|
)
|
||||||
|
|
||||||
const HeaderCheckRedirect = "X-Goproxy-Check-Redirect"
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package http
|
package httpheaders
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -17,13 +17,15 @@ const (
|
||||||
HeaderXForwardedURI = "X-Forwarded-Uri"
|
HeaderXForwardedURI = "X-Forwarded-Uri"
|
||||||
HeaderXRealIP = "X-Real-IP"
|
HeaderXRealIP = "X-Real-IP"
|
||||||
|
|
||||||
HeaderUpstreamName = "X-GoDoxy-Upstream-Name"
|
|
||||||
HeaderUpstreamScheme = "X-GoDoxy-Upstream-Scheme"
|
|
||||||
HeaderUpstreamHost = "X-GoDoxy-Upstream-Host"
|
|
||||||
HeaderUpstreamPort = "X-GoDoxy-Upstream-Port"
|
|
||||||
|
|
||||||
HeaderContentType = "Content-Type"
|
HeaderContentType = "Content-Type"
|
||||||
HeaderContentLength = "Content-Length"
|
HeaderContentLength = "Content-Length"
|
||||||
|
|
||||||
|
HeaderUpstreamName = "X-Godoxy-Upstream-Name"
|
||||||
|
HeaderUpstreamScheme = "X-Godoxy-Upstream-Scheme"
|
||||||
|
HeaderUpstreamHost = "X-Godoxy-Upstream-Host"
|
||||||
|
HeaderUpstreamPort = "X-Godoxy-Upstream-Port"
|
||||||
|
|
||||||
|
HeaderGoDoxyCheckRedirect = "X-Godoxy-Check-Redirect"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Hop-by-hop headers. These are removed when sent to the backend.
|
// Hop-by-hop headers. These are removed when sent to the backend.
|
|
@ -26,8 +26,8 @@ import (
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
gphttp "github.com/yusing/go-proxy/internal/net/http"
|
"github.com/yusing/go-proxy/internal/net/gphttp/accesslog"
|
||||||
"github.com/yusing/go-proxy/internal/net/http/accesslog"
|
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
|
||||||
"github.com/yusing/go-proxy/internal/net/types"
|
"github.com/yusing/go-proxy/internal/net/types"
|
||||||
U "github.com/yusing/go-proxy/internal/utils"
|
U "github.com/yusing/go-proxy/internal/utils"
|
||||||
"golang.org/x/net/http/httpguts"
|
"golang.org/x/net/http/httpguts"
|
||||||
|
@ -168,7 +168,7 @@ func copyHeader(dst, src http.Header) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ReverseProxy) errorHandler(rw http.ResponseWriter, r *http.Request, err error, writeHeader bool) {
|
func (p *ReverseProxy) errorHandler(rw http.ResponseWriter, r *http.Request, err error, writeHeader bool) {
|
||||||
reqURL := r.Host + r.RequestURI
|
reqURL := r.Host + r.URL.Path
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, context.Canceled),
|
case errors.Is(err, context.Canceled),
|
||||||
errors.Is(err, io.EOF),
|
errors.Is(err, io.EOF),
|
||||||
|
@ -266,14 +266,14 @@ func (p *ReverseProxy) handler(rw http.ResponseWriter, req *http.Request) {
|
||||||
p.rewriteRequestURL(outreq)
|
p.rewriteRequestURL(outreq)
|
||||||
outreq.Close = false
|
outreq.Close = false
|
||||||
|
|
||||||
reqUpType := gphttp.UpgradeType(outreq.Header)
|
reqUpType := httpheaders.UpgradeType(outreq.Header)
|
||||||
if !IsPrint(reqUpType) {
|
if !IsPrint(reqUpType) {
|
||||||
p.errorHandler(rw, req, fmt.Errorf("client tried to switch to invalid protocol %q", reqUpType), true)
|
p.errorHandler(rw, req, fmt.Errorf("client tried to switch to invalid protocol %q", reqUpType), true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Del("Forwarded")
|
req.Header.Del("Forwarded")
|
||||||
gphttp.RemoveHopByHopHeaders(outreq.Header)
|
httpheaders.RemoveHopByHopHeaders(outreq.Header)
|
||||||
|
|
||||||
// Issue 21096: tell backend applications that care about trailer support
|
// Issue 21096: tell backend applications that care about trailer support
|
||||||
// that we support trailers. (We do, but we don't go out of our way to
|
// that we support trailers. (We do, but we don't go out of our way to
|
||||||
|
@ -298,7 +298,7 @@ func (p *ReverseProxy) handler(rw http.ResponseWriter, req *http.Request) {
|
||||||
// If we aren't the first proxy retain prior
|
// If we aren't the first proxy retain prior
|
||||||
// X-Forwarded-For information as a comma+space
|
// X-Forwarded-For information as a comma+space
|
||||||
// separated list and fold multiple headers into one.
|
// separated list and fold multiple headers into one.
|
||||||
prior, ok := outreq.Header[gphttp.HeaderXForwardedFor]
|
prior, ok := outreq.Header[httpheaders.HeaderXForwardedFor]
|
||||||
omit := ok && prior == nil // Issue 38079: nil now means don't populate the header
|
omit := ok && prior == nil // Issue 38079: nil now means don't populate the header
|
||||||
|
|
||||||
xff, _, err := net.SplitHostPort(req.RemoteAddr)
|
xff, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||||
|
@ -309,7 +309,7 @@ func (p *ReverseProxy) handler(rw http.ResponseWriter, req *http.Request) {
|
||||||
xff = strings.Join(prior, ", ") + ", " + xff
|
xff = strings.Join(prior, ", ") + ", " + xff
|
||||||
}
|
}
|
||||||
if !omit {
|
if !omit {
|
||||||
outreq.Header.Set(gphttp.HeaderXForwardedFor, xff)
|
outreq.Header.Set(httpheaders.HeaderXForwardedFor, xff)
|
||||||
}
|
}
|
||||||
|
|
||||||
var reqScheme string
|
var reqScheme string
|
||||||
|
@ -319,10 +319,10 @@ func (p *ReverseProxy) handler(rw http.ResponseWriter, req *http.Request) {
|
||||||
reqScheme = "http"
|
reqScheme = "http"
|
||||||
}
|
}
|
||||||
|
|
||||||
outreq.Header.Set(gphttp.HeaderXForwardedMethod, req.Method)
|
outreq.Header.Set(httpheaders.HeaderXForwardedMethod, req.Method)
|
||||||
outreq.Header.Set(gphttp.HeaderXForwardedProto, reqScheme)
|
outreq.Header.Set(httpheaders.HeaderXForwardedProto, reqScheme)
|
||||||
outreq.Header.Set(gphttp.HeaderXForwardedHost, req.Host)
|
outreq.Header.Set(httpheaders.HeaderXForwardedHost, req.Host)
|
||||||
outreq.Header.Set(gphttp.HeaderXForwardedURI, req.RequestURI)
|
outreq.Header.Set(httpheaders.HeaderXForwardedURI, req.RequestURI)
|
||||||
|
|
||||||
if _, ok := outreq.Header["User-Agent"]; !ok {
|
if _, ok := outreq.Header["User-Agent"]; !ok {
|
||||||
// If the outbound request doesn't have a User-Agent header set,
|
// If the outbound request doesn't have a User-Agent header set,
|
||||||
|
@ -389,7 +389,7 @@ func (p *ReverseProxy) handler(rw http.ResponseWriter, req *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
gphttp.RemoveHopByHopHeaders(res.Header)
|
httpheaders.RemoveHopByHopHeaders(res.Header)
|
||||||
|
|
||||||
if !p.modifyResponse(rw, res, req, outreq) {
|
if !p.modifyResponse(rw, res, req, outreq) {
|
||||||
return
|
return
|
||||||
|
@ -410,15 +410,13 @@ func (p *ReverseProxy) handler(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
rw.WriteHeader(res.StatusCode)
|
rw.WriteHeader(res.StatusCode)
|
||||||
|
|
||||||
_, err = io.Copy(rw, res.Body)
|
err = U.CopyCloseWithContext(ctx, rw, res.Body) // close now, instead of defer, to populate res.Trailer
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !errors.Is(err, context.Canceled) {
|
if !errors.Is(err, context.Canceled) {
|
||||||
p.errorHandler(rw, req, err, true)
|
p.errorHandler(rw, req, err, false)
|
||||||
}
|
}
|
||||||
res.Body.Close()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res.Body.Close() // close now, instead of defer, to populate res.Trailer
|
|
||||||
|
|
||||||
if len(res.Trailer) > 0 {
|
if len(res.Trailer) > 0 {
|
||||||
// Force chunking if we saw a response trailer.
|
// Force chunking if we saw a response trailer.
|
||||||
|
@ -460,8 +458,8 @@ func cleanWebsocketHeaders(req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ReverseProxy) handleUpgradeResponse(rw http.ResponseWriter, req *http.Request, res *http.Response) {
|
func (p *ReverseProxy) handleUpgradeResponse(rw http.ResponseWriter, req *http.Request, res *http.Response) {
|
||||||
reqUpType := gphttp.UpgradeType(req.Header)
|
reqUpType := httpheaders.UpgradeType(req.Header)
|
||||||
resUpType := gphttp.UpgradeType(res.Header)
|
resUpType := httpheaders.UpgradeType(res.Header)
|
||||||
if !IsPrint(resUpType) { // We know reqUpType is ASCII, it's checked by the caller.
|
if !IsPrint(resUpType) { // We know reqUpType is ASCII, it's checked by the caller.
|
||||||
p.errorHandler(rw, req, fmt.Errorf("backend tried to switch to invalid protocol %q", resUpType), true)
|
p.errorHandler(rw, req, fmt.Errorf("backend tried to switch to invalid protocol %q", resUpType), true)
|
||||||
return
|
return
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
@ -37,6 +38,14 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func NewContextReader(ctx context.Context, r io.Reader) *ContextReader {
|
||||||
|
return &ContextReader{ctx: ctx, Reader: r}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewContextWriter(ctx context.Context, w io.Writer) *ContextWriter {
|
||||||
|
return &ContextWriter{ctx: ctx, Writer: w}
|
||||||
|
}
|
||||||
|
|
||||||
func (r *ContextReader) Read(p []byte) (int, error) {
|
func (r *ContextReader) Read(p []byte) (int, error) {
|
||||||
select {
|
select {
|
||||||
case <-r.ctx.Done():
|
case <-r.ctx.Done():
|
||||||
|
@ -63,7 +72,7 @@ func NewPipe(ctx context.Context, r io.ReadCloser, w io.WriteCloser) *Pipe {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pipe) Start() (err error) {
|
func (p *Pipe) Start() (err error) {
|
||||||
err = Copy(&p.w, &p.r)
|
err = CopyClose(&p.w, &p.r)
|
||||||
switch {
|
switch {
|
||||||
case
|
case
|
||||||
// NOTE: ignoring broken pipe and connection reset by peer
|
// NOTE: ignoring broken pipe and connection reset by peer
|
||||||
|
@ -97,28 +106,86 @@ func (p BidirectionalPipe) Start() gperr.Error {
|
||||||
return b.Error()
|
return b.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var copyBufPool = sync.Pool{
|
||||||
|
New: func() any {
|
||||||
|
return make([]byte, copyBufSize)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
type httpFlusher interface {
|
||||||
|
Flush() error
|
||||||
|
}
|
||||||
|
|
||||||
|
func getHttpFlusher(dst io.Writer) httpFlusher {
|
||||||
|
if rw, ok := dst.(http.ResponseWriter); ok {
|
||||||
|
return http.NewResponseController(rw)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
copyBufSize = 32 * 1024
|
||||||
|
)
|
||||||
|
|
||||||
|
var copyBufPool = sync.Pool{
|
||||||
|
New: func() any {
|
||||||
|
return make([]byte, copyBufSize)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// Copyright 2009 The Go Authors. All rights reserved.
|
// Copyright 2009 The Go Authors. All rights reserved.
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// This is a copy of io.Copy with context handling
|
// This is a copy of io.Copy with context and HTTP flusher handling
|
||||||
// Author: yusing <yusing@6uo.me>.
|
// Author: yusing <yusing@6uo.me>.
|
||||||
func Copy(dst *ContextWriter, src *ContextReader) (err error) {
|
func CopyClose(dst *ContextWriter, src *ContextReader) (err error) {
|
||||||
size := 32 * 1024
|
var buf []byte
|
||||||
if l, ok := src.Reader.(*io.LimitedReader); ok && int64(size) > l.N {
|
if l, ok := src.Reader.(*io.LimitedReader); ok {
|
||||||
|
size := copyBufSize
|
||||||
|
if int64(size) > l.N {
|
||||||
if l.N < 1 {
|
if l.N < 1 {
|
||||||
size = 1
|
size = 1
|
||||||
} else {
|
} else {
|
||||||
size = int(l.N)
|
size = int(l.N)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buf := make([]byte, size)
|
buf = make([]byte, 0, size)
|
||||||
|
} else {
|
||||||
|
buf = copyBufPool.Get().([]byte)
|
||||||
|
defer copyBufPool.Put(buf)
|
||||||
|
}
|
||||||
|
// close both as soon as one of them is done
|
||||||
|
wCloser, wCanClose := dst.Writer.(io.Closer)
|
||||||
|
rCloser, rCanClose := src.Reader.(io.Closer)
|
||||||
|
if wCanClose || rCanClose {
|
||||||
|
if src.ctx == dst.ctx {
|
||||||
|
go func() {
|
||||||
|
<-src.ctx.Done()
|
||||||
|
if wCanClose {
|
||||||
|
wCloser.Close()
|
||||||
|
}
|
||||||
|
if rCanClose {
|
||||||
|
rCloser.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
} else {
|
||||||
|
if wCloser != nil {
|
||||||
|
go func() {
|
||||||
|
<-src.ctx.Done()
|
||||||
|
wCloser.Close()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
if rCloser != nil {
|
||||||
|
go func() {
|
||||||
|
<-dst.ctx.Done()
|
||||||
|
rCloser.Close()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
flusher := getHttpFlusher(dst.Writer)
|
||||||
|
canFlush := flusher != nil
|
||||||
for {
|
for {
|
||||||
select {
|
nr, er := src.Reader.Read(buf[:copyBufSize])
|
||||||
case <-src.ctx.Done():
|
|
||||||
return src.ctx.Err()
|
|
||||||
case <-dst.ctx.Done():
|
|
||||||
return dst.ctx.Err()
|
|
||||||
default:
|
|
||||||
nr, er := src.Reader.Read(buf)
|
|
||||||
if nr > 0 {
|
if nr > 0 {
|
||||||
nw, ew := dst.Writer.Write(buf[0:nr])
|
nw, ew := dst.Writer.Write(buf[0:nr])
|
||||||
if nw < 0 || nr < nw {
|
if nw < 0 || nr < nw {
|
||||||
|
@ -135,6 +202,17 @@ func Copy(dst *ContextWriter, src *ContextReader) (err error) {
|
||||||
err = io.ErrShortWrite
|
err = io.ErrShortWrite
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if canFlush {
|
||||||
|
err = flusher.Flush()
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, http.ErrNotSupported) {
|
||||||
|
canFlush = false
|
||||||
|
err = nil
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if er != nil {
|
if er != nil {
|
||||||
if er != io.EOF {
|
if er != io.EOF {
|
||||||
|
@ -144,8 +222,7 @@ func Copy(dst *ContextWriter, src *ContextReader) (err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func Copy2(ctx context.Context, dst io.Writer, src io.Reader) error {
|
func CopyCloseWithContext(ctx context.Context, dst io.Writer, src io.Reader) (err error) {
|
||||||
return Copy(&ContextWriter{ctx: ctx, Writer: dst}, &ContextReader{ctx: ctx, Reader: src})
|
return CopyClose(NewContextWriter(ctx, dst), NewContextReader(ctx, src))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue