From 970529b6eefabdacc778559be5ce9a4e1ba45ca7 Mon Sep 17 00:00:00 2001 From: Shawn G <130071715+G0d2i11a@users.noreply.github.com> Date: Thu, 16 Jul 2026 15:04:41 +0800 Subject: [PATCH] perf(api): skip inactive request interceptors Why: - The plugin host can exist without active request interceptors. - The before-auth path still cloned large request bodies four times in that case. What: - Reuse the existing request interceptor capability detector before dispatch. - Add pointer-reuse regression coverage and allocation benchmarks. Validation: - go test ./... -count=1 - go build -o /tmp/test-output ./cmd/server - focused race test passed; the full handler race run still hits an existing stream-header race reproduced on origin/dev --- sdk/api/handlers/handlers.go | 2 +- .../handlers/handlers_interceptors_test.go | 79 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/sdk/api/handlers/handlers.go b/sdk/api/handlers/handlers.go index f3ab71788..9209a8f3f 100644 --- a/sdk/api/handlers/handlers.go +++ b/sdk/api/handlers/handlers.go @@ -2064,7 +2064,7 @@ func interceptStreamChunk(ctx context.Context, host PluginInterceptorHost, req p func (h *BaseAPIHandler) applyRequestInterceptorsBeforeAuth(ctx context.Context, handlerType, requestedModel string, req coreexecutor.Request, opts coreexecutor.Options, skipPluginID string) (coreexecutor.Request, coreexecutor.Options) { host := h.interceptorHost() - if host == nil { + if !requestInterceptorsEnabled(host) { return req, opts } resp := interceptRequestBeforeAuth(ctx, host, pluginapi.RequestInterceptRequest{ diff --git a/sdk/api/handlers/handlers_interceptors_test.go b/sdk/api/handlers/handlers_interceptors_test.go index 7cc309b71..e5f6979cc 100644 --- a/sdk/api/handlers/handlers_interceptors_test.go +++ b/sdk/api/handlers/handlers_interceptors_test.go @@ -29,10 +29,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) @@ -255,6 +263,77 @@ 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 := handler.applyRequestInterceptorsBeforeAuth(context.Background(), "openai", req.Model, req, opts, "") + + if called { + t.Fatal("disabled request interceptor was called") + } + if &gotReq.Payload[0] != &payload[0] { + t.Fatal("request payload was copied") + } + if &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, 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{}