Merge pull request #4360 from router-for-me/perf/skip-inactive-request-interceptors

This commit is contained in:
Luis Pater
2026-08-15 05:12:18 +08:00
2 changed files with 83 additions and 1 deletions

View File

@@ -425,7 +425,7 @@ func interceptStreamChunk(ctx context.Context, host PluginInterceptorHost, req p
func (h *BaseAPIHandler) applyRequestInterceptorsBeforeAuth(ctx context.Context, handlerType, requestedModel, requestID string, req coreexecutor.Request, opts coreexecutor.Options, skipPluginID string) (coreexecutor.Request, coreexecutor.Options, *interfaces.ErrorMessage) {
host := h.interceptorHost()
if host == nil {
if !requestInterceptorsEnabled(host) {
return req, opts, nil
}
resp := interceptRequestBeforeAuth(ctx, host, pluginapi.RequestInterceptRequest{

View File

@@ -34,10 +34,18 @@ type handlerInterceptorNoStreamTestHost struct {
*handlerInterceptorTestHost
}
type handlerInterceptorDisabledRequestTestHost struct {
*handlerInterceptorTestHost
}
func (h *handlerInterceptorNoStreamTestHost) HasStreamInterceptors() bool {
return false
}
func (h *handlerInterceptorDisabledRequestTestHost) HasRequestInterceptors() bool {
return false
}
func (h *handlerInterceptorTestHost) InterceptRequestBeforeAuth(ctx context.Context, req pluginapi.RequestInterceptRequest) pluginapi.RequestInterceptResponse {
if h != nil && h.interceptRequestBeforeAuth != nil {
return h.interceptRequestBeforeAuth(ctx, req)
@@ -566,6 +574,80 @@ func TestHandlerRequestInterceptorRewritesExecutorRequest(t *testing.T) {
}
}
func TestHandlerSkipsDisabledRequestInterceptorsWithoutCopyingPayload(t *testing.T) {
payload := []byte(`{"model":"disabled-interceptor-model"}`)
called := false
handler := NewBaseAPIHandlers(&sdkconfig.SDKConfig{}, nil)
handler.SetPluginHost(&handlerInterceptorDisabledRequestTestHost{
handlerInterceptorTestHost: &handlerInterceptorTestHost{
interceptRequestBeforeAuth: func(context.Context, pluginapi.RequestInterceptRequest) pluginapi.RequestInterceptResponse {
called = true
return pluginapi.RequestInterceptResponse{Body: []byte(`{"unexpected":true}`)}
},
},
})
req := coreexecutor.Request{Model: "disabled-interceptor-model", Payload: payload}
opts := coreexecutor.Options{OriginalRequest: payload}
gotReq, gotOpts, err := handler.applyRequestInterceptorsBeforeAuth(context.Background(), "openai", req.Model, "test-req", req, opts, "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if called {
t.Fatal("disabled request interceptor was called")
}
if len(gotReq.Payload) != len(payload) || &gotReq.Payload[0] != &payload[0] {
t.Fatal("request payload was copied")
}
if len(gotOpts.OriginalRequest) != len(payload) || &gotOpts.OriginalRequest[0] != &payload[0] {
t.Fatal("original request was copied")
}
}
func BenchmarkHandlerRequestInterceptors(b *testing.B) {
sizes := []struct {
name string
bytes int
}{
{name: "1KiB", bytes: 1 << 10},
{name: "1MiB", bytes: 1 << 20},
{name: "8MiB", bytes: 8 << 20},
}
hosts := []struct {
name string
host PluginInterceptorHost
}{
{
name: "disabled",
host: &handlerInterceptorDisabledRequestTestHost{
handlerInterceptorTestHost: &handlerInterceptorTestHost{},
},
},
{name: "active", host: &handlerInterceptorTestHost{}},
}
for _, size := range sizes {
payload := make([]byte, size.bytes)
req := coreexecutor.Request{Model: "benchmark-model", Payload: payload}
opts := coreexecutor.Options{OriginalRequest: payload}
for _, host := range hosts {
b.Run(host.name+"/"+size.name, func(b *testing.B) {
handler := NewBaseAPIHandlers(&sdkconfig.SDKConfig{}, nil)
handler.SetPluginHost(host.host)
b.ReportAllocs()
b.ResetTimer()
for range b.N {
gotReq, gotOpts, _ := handler.applyRequestInterceptorsBeforeAuth(context.Background(), "openai", req.Model, "benchmark-req", req, opts, "")
if len(gotReq.Payload) != size.bytes || len(gotOpts.OriginalRequest) != size.bytes {
b.Fatal("request payload length changed")
}
}
})
}
}
}
func TestHandlerRequestInterceptorEmptyBodyKeepsOriginalPayload(t *testing.T) {
model := "handler-interceptor-empty-body-model"
executor := &interceptorCaptureExecutor{}