-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Consistent Hashing Load Balancing Strategy #592
Add Consistent Hashing Load Balancing Strategy #592
Conversation
@mostafa |
I suggested Murmur as an example and the best hashing algorithm should be chosen in terms of 1) performance and 2) least amount of or no collisions. If you think that Murmur3 outperforms the others and is well-maintained, we can use it. (Optionally) In the future we can give the users the choice of the algorithm. |
return &ConsistentHash{ | ||
originalStrategy: originalStrategy, | ||
useSourceIP: server.LoadbalancerConsistentHash.UseSourceIP, | ||
hashMap: make(map[uint64]IProxy), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could use sync.Map, which handles locking behind the scenes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first, I messed up by just locking the load
and store
process. Now, I’ve added a sync.Mutex
to lock the whole NextProxy
connection. also added a test case to check for concurrency. (so I guess we can not use sync.Map here anymore)
Ticket(s)
#398
Description
This pull request introduces a new consistent hashing strategy to our load balancing system. The consistent hashing algorithm routes client connections to specific proxies based on a hash of the client’s IP address or full connection address. This addition enhances the load balancing mechanism by providing a more deterministic way to distribute traffic, especially useful in scenarios where persistence or even distribution is crucial.
Changes:
• Added a new ConsistentHash struct to implement the consistent hashing strategy.
• Added methods for creating a ConsistentHash instance and selecting proxies based on the hash of the client’s IP or connection address.
• Utilizes the MurmurHash3 algorithm for hashing.
• Extended the LoadBalancer struct to include optional consistent hashing configuration.
• Updated the configuration file (gatewayd.yaml) to support the new consistentHash field.
• Updated the NewLoadBalancerStrategy function to wrap existing strategies with consistent hashing if configured.
• Adjusted the NextProxy method in existing strategies to accept connection parameters, aligning with the new interface.
• Added comprehensive tests for the ConsistentHash strategy, including cases where hashing is based on source IP and full connection address.
• Updated existing tests to work with the new IConnWrapper interface.
• Refactored related components to support the new consistent hashing strategy.
• Added a mock implementation for IConnWrapper to facilitate testing.
Related PRs
Development Checklist
make gen-docs
command.Legal Checklist