Skip to content

Optimize middleware/proxy per-request hot path without behavior changes#1014

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/review-performance-improvements
Draft

Optimize middleware/proxy per-request hot path without behavior changes#1014
Copilot wants to merge 3 commits into
mainfrom
copilot/review-performance-improvements

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 16, 2026

This proxy sits on the Kubernetes API request path, so avoidable per-request work in middleware and request handling directly impacts latency/throughput. This change reduces repeated auth parsing and hot-path allocations while preserving existing behavior.

  • Request-scoped auth resolution cache

    • Added a request-context based resolver (ResolveUserAndGroups) to compute user/groups once per request and reuse across middleware/handlers.
    • Replaced repeated NewHTTP(...).GetUserAndGroups() calls in:
      • internal/webserver/webserver.go
      • internal/webserver/middleware/user_in_group.go
  • ******** parsing hot-path improvements**

    • Moved websocket bearer regex compilation to package scope (no per-request MustCompile).
    • Added safe regex match handling before group indexing to avoid panic risk.
    • Switched from ReplaceAll("Bearer ", "") to TrimPrefix("Bearer ").
  • Proxy handler allocation/recompute reductions

    • In authorization middleware, reused ResponseRecorder.Result() once and closed body once.
    • In selector handling, computed selector.String() once and reused.
    • In token injection paths, fetched BearerToken() once and reused; removed repeated formatting overhead.

Example (new request-scoped resolver usage):

request, user, groups, err := req.ResolveUserAndGroups(
    request,
    authTypes,
    claim,
    writer,
    ignoredImpersonationGroups,
    impersonationGroupsRegexp,
    skipImpersonationReview,
)

Copilot AI and others added 3 commits April 16, 2026 10:46
Agent-Logs-Url: https://github.com/projectcapsule/capsule-proxy/sessions/2f8293ae-ba3a-459c-aa67-4cf17337115e

Co-authored-by: oliverbaehler <26610571+oliverbaehler@users.noreply.github.com>
Agent-Logs-Url: https://github.com/projectcapsule/capsule-proxy/sessions/2f8293ae-ba3a-459c-aa67-4cf17337115e

Co-authored-by: oliverbaehler <26610571+oliverbaehler@users.noreply.github.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (123ffa2) to head (e814940).
⚠️ Report is 9 commits behind head on main.

Additional details and impacted files
@@          Coverage Diff          @@
##            main   #1014   +/-   ##
=====================================
  Coverage   0.00%   0.00%           
=====================================
  Files          1       1           
  Lines        200     200           
=====================================
  Misses       200     200           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link
Copy Markdown

This pull request has been marked as stale because it has been inactive for more than 30 days. Please update this pull request or it will be automatically closed in 7 days.

@github-actions github-actions Bot added the Stale label May 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants