refactor(misc): enhance performance on bytes pool, entrypoint, access log and route context handling

- Introduced benchmark tests for Entrypoint and ReverseProxy to evaluate performance.
- Updated Entrypoint's ServeHTTP method to improve route context management.
- Added new test file for entrypoint benchmarks and refined existing tests for route handling.
This commit is contained in:
yusing
2025-09-14 00:03:27 +08:00
parent e0d25e475c
commit f31b1b5ed3
11 changed files with 623 additions and 33 deletions

View File

@@ -2,18 +2,42 @@ package routes
import (
"context"
"crypto/tls"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"reflect"
"unsafe"
"github.com/yusing/go-proxy/internal/types"
)
type RouteContext struct{}
type RouteContextKey struct{}
var routeContextKey = RouteContext{}
type RouteContext struct {
context.Context
Route types.HTTPRoute
}
var routeContextKey = RouteContextKey{}
func (r *RouteContext) Value(key any) any {
if key == routeContextKey {
return r.Route
}
return r.Context.Value(key)
}
func WithRouteContext(r *http.Request, route types.HTTPRoute) *http.Request {
return r.WithContext(context.WithValue(r.Context(), routeContextKey, route))
// we don't want to copy the request object every fucking requests
// return r.WithContext(context.WithValue(r.Context(), routeContextKey, route))
(*requestInternal)(unsafe.Pointer(r)).ctx = &RouteContext{
Context: r.Context(),
Route: route,
}
return r
}
func TryGetRoute(r *http.Request) types.HTTPRoute {
@@ -74,3 +98,44 @@ func TryGetUpstreamURL(r *http.Request) string {
}
return ""
}
type requestInternal struct {
Method string
URL *url.URL
Proto string
ProtoMajor int
ProtoMinor int
Header http.Header
Body io.ReadCloser
GetBody func() (io.ReadCloser, error)
ContentLength int64
TransferEncoding []string
Close bool
Host string
Form url.Values
PostForm url.Values
MultipartForm *multipart.Form
Trailer http.Header
RemoteAddr string
RequestURI string
TLS *tls.ConnectionState
Cancel <-chan struct{}
Response *http.Response
Pattern string
ctx context.Context
}
func init() {
// make sure ctx has the same offset as http.Request
f, ok := reflect.TypeFor[requestInternal]().FieldByName("ctx")
if !ok {
panic("ctx field not found")
}
f2, ok := reflect.TypeFor[http.Request]().FieldByName("ctx")
if !ok {
panic("ctx field not found")
}
if f.Offset != f2.Offset {
panic(fmt.Sprintf("ctx has different offset than http.Request: %d != %d", f.Offset, f2.Offset))
}
}