Skip to content

⚡ perf: Optimize proxy balancer round-robin - #4549

Open
james-yusuke wants to merge 10 commits into
gofiber:mainfrom
james-yusuke:perf/proxy-balancer-forward-round-robin
Open

⚡ perf: Optimize proxy balancer round-robin#4549
james-yusuke wants to merge 10 commits into
gofiber:mainfrom
james-yusuke:perf/proxy-balancer-forward-round-robin

Conversation

@james-yusuke

Copy link
Copy Markdown
Contributor

Description

Removes the lock from BalancerForward server selection to reduce contention on the round-robin hot path.

The selector now uses an atomic bounded counter while preserving the existing cyclic selection behavior, including counter rollover. Public APIs, proxy validation, and SSRF protections are unchanged.

Changes introduced

  • Benchmarks: Added parallel benchmarks comparing the atomic selector with the previous mutex-based approach. Both paths perform zero allocations.
  • API Longevity: No public API or configuration changes.
  • Added rollover coverage to preserve round-robin ordering when the counter reaches the uint64 boundary.
  • Added concurrent selection coverage to validate race-safe distribution.

Type of change

  • Performance improvement (non-breaking change which improves efficiency)

Checklist

  • Conducted a self-review of the code and provided comments for complex or critical parts.
  • Added or updated unit tests to validate the effectiveness of the changes or new features.
  • Ensured that new and existing unit tests pass locally with the changes.
  • Aimed for optimal performance with minimal allocations in the new code.
  • Provided benchmarks for the new code to analyze and improve upon.

@james-yusuke
james-yusuke requested a review from a team as a code owner July 21, 2026 06:46
@ReneWerner87 ReneWerner87 added this to v3 Jul 21, 2026
@ReneWerner87 ReneWerner87 added this to the v3 milestone Jul 21, 2026
@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

The proxy round-robin selector now uses an atomic CAS-based counter instead of a mutex. Tests cover counter wrap-around and concurrent distribution, while benchmarks compare atomic and mutex-protected implementations.

Changes

Proxy round-robin selection

Layer / File(s) Summary
Atomic round-robin implementation
middleware/proxy/proxy.go
urlRoundrobin uses an atomic.Uint64 counter and CAS-based advancement with pool-size wrap-around.
Selection validation and benchmarking
middleware/proxy/security_test.go, middleware/proxy/bench_test.go
Tests cover modulo and uint64 wrap-around plus concurrent selection distribution; benchmarks compare atomic and mutex-based selectors.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • gofiber/fiber#4405: Reworks proxy round-robin usage and introduces the related selector implementation.

Suggested labels: ⚡️ Performance

Suggested reviewers: sixcolors, renewerner87, efectn

Poem

A rabbit hops through upstream lanes,
While atoms spin instead of chains.
Round and round the URLs flow,
Past the max and back they go.
“Hop!” says Bun, “the tests all glow!”

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately describes the proxy round-robin performance change.
Description check ✅ Passed The description covers the purpose, changes, benchmarks, type of change, and checklist; only optional template items like issue links are omitted.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jul 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.29%. Comparing base (48b5ea3) to head (58c6313).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4549   +/-   ##
=======================================
  Coverage   93.29%   93.29%           
=======================================
  Files         140      140           
  Lines       14854    14856    +2     
=======================================
+ Hits        13858    13860    +2     
  Misses        620      620           
  Partials      376      376           
Flag Coverage Δ
unittests 93.29% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.

@gaby gaby changed the title 🧹 chore: optimize proxy balancer round-robin ⚡ perf: optimize proxy balancer round-robin Jul 23, 2026
@gaby gaby changed the title ⚡ perf: optimize proxy balancer round-robin ⚡ perf: Optimize proxy balancer round-robin Jul 23, 2026

// Test_Security_RoundRobin_WrapsAround covers the modulo wrap-around in
// urlRoundrobin.get when current >= len(pool).
func Test_Security_RoundRobin_WrapsAround(t *testing.T) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this removed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right—there was no reason to remove this coverage. I've restored it and expanded it to cover both the normal round-robin sequence and the counter-limit case.

Comment thread middleware/proxy/roundrobin_bench_test.go Outdated
Comment thread app.go Outdated
@github-project-automation github-project-automation Bot moved this to Done in v3 Jul 24, 2026
@gaby

gaby commented Jul 24, 2026

Copy link
Copy Markdown
Member

@james-yusuke Why you closed it?

@james-yusuke

Copy link
Copy Markdown
Contributor Author

Sorry, I closed it by mistake while handling the benchmark failure. The checks are now green, so I’ve reopened it. Could you please take another look?

@james-yusuke james-yusuke reopened this Jul 24, 2026
Comment thread .github/workflows/benchmark.yml Outdated
@james-yusuke
james-yusuke force-pushed the perf/proxy-balancer-forward-round-robin branch from 5f2ef10 to 4a178bc Compare July 25, 2026 22:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants