mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 16:58:31 +02:00
refactor(route): simplify context handling in RouteContext
- Removed unnecessary requestInternal struct and directly accessed the context field of http.Request. - Simplified the initialization of ctxFieldOffset.
This commit is contained in:
@@ -2,10 +2,6 @@ package routes
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"mime/multipart"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -34,7 +30,8 @@ func (r *RouteContext) Value(key any) any {
|
|||||||
func WithRouteContext(r *http.Request, route types.HTTPRoute) *http.Request {
|
func WithRouteContext(r *http.Request, route types.HTTPRoute) *http.Request {
|
||||||
// we don't want to copy the request object every fucking requests
|
// we don't want to copy the request object every fucking requests
|
||||||
// return r.WithContext(context.WithValue(r.Context(), routeContextKey, route))
|
// return r.WithContext(context.WithValue(r.Context(), routeContextKey, route))
|
||||||
(*requestInternal)(unsafe.Pointer(r)).ctx = &RouteContext{
|
ctxFieldPtr := (*context.Context)(unsafe.Pointer(uintptr(unsafe.Pointer(r)) + ctxFieldOffset))
|
||||||
|
*ctxFieldPtr = &RouteContext{
|
||||||
Context: r.Context(),
|
Context: r.Context(),
|
||||||
Route: route,
|
Route: route,
|
||||||
}
|
}
|
||||||
@@ -107,43 +104,12 @@ func TryGetUpstreamURL(r *http.Request) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
type requestInternal struct {
|
var ctxFieldOffset uintptr
|
||||||
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() {
|
func init() {
|
||||||
// make sure ctx has the same offset as http.Request
|
f, ok := reflect.TypeFor[http.Request]().FieldByName("ctx")
|
||||||
f, ok := reflect.TypeFor[requestInternal]().FieldByName("ctx")
|
|
||||||
if !ok {
|
if !ok {
|
||||||
panic("ctx field not found")
|
panic("ctx field not found")
|
||||||
}
|
}
|
||||||
f2, ok := reflect.TypeFor[http.Request]().FieldByName("ctx")
|
ctxFieldOffset = f.Offset
|
||||||
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))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user