Skip to content

feat(router): add load balancing support and unit tests - #3

Closed
gianpierofasulo wants to merge 2 commits into
neuron-core:1.xfrom
gianpierofasulo:feature/router-improvements
Closed

gianpierofasulo wants to merge 2 commits into
neuron-core:1.xfrom
gianpierofasulo:feature/router-improvements

Conversation

@gianpierofasulo

Copy link
Copy Markdown

Summary

This PR introduces load balancing capabilities to the Router module, allowing requests and tasks to be distributed across available handlers, along with a dedicated test suite.

Changes Made

  • Load Balancing Logic: Implemented routing distribution logic in src/Router/.
  • Test Coverage: Added unit tests in tests/Router/ to validate distribution scenarios and fallback behavior.

Motivation

Adding built-in load balancing prevents bottlenecking on single handlers, improves system resilience, and ensures balanced throughput.

How Has This Been Tested?

  • Unit tests added under tests/Router/.
  • Test suite executed locally with all checks passing.

@ilvalerione

ilvalerione commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

This PR introduces a new sub directory src/Router that marks all the files as new. RouterProvider already exists directly under src. You should revert the repo to the original package structure.

@gianpierofasulo

Copy link
Copy Markdown
Author

Thanks for the review! I have restored the original directory structure and aligned all namespaces with the project's PSR-4 configuration (NeuronAI\ -> src/, NeuronAI\Tests\ -> tests/):

  • Kept RouterProvider.php directly under src/ with namespace NeuronAI.
  • Placed routing rules under src/Rules/ with namespace NeuronAI\Rules.
  • Placed test files directly under tests/ with namespace NeuronAI\Tests.
  • Preserved existing fallback mechanisms while integrating the load balancing enhancements.

@ilvalerione

ilvalerione commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Inspired by this PR I decided to take a different approach.

The RoundRobinRule is now deprecated, and I implemented a new LoadBalancingRule that equally distribute the requests against the configured providers by default, basically working as a round-robin. Then you can specify different distribuition weights: https://github.com/neuron-core/router#loadbalancingrule

I also took the chance for this release to introduce a new ability in the fallback strategy to retry with a backoff time.

It's available in the 1.x branch, compatible with Neuron 3.x, and it's already ported in the 2.x branch compatible with the new Neuron 4.x: https://github.com/neuron-core/router#retry-with-backoff

Thank you for your contribution.

@ilvalerione ilvalerione closed this Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants