Skip to content

Conversation

@sinadarbouy
Copy link
Collaborator

Description

This PR updates the consistent hashing logic to use the remote address (RemoteAddr) of a connection instead of the local address (LocalAddr). The change addresses the following issues:

  1. When a new connection is opened, LocalAddr points to the gateway address, while RemoteAddr correctly points to the client’s source IP. The previous use of LocalAddr caused issues, such as always directing traffic to the same database.
  2. The fallback behavior now utilizes the full remote address (IP:port) as the key when useSourceIP is set to false. This change fixes the issue where the useSourceIP is disabled, as the remote address has a random port each time, making the key different.

Additionally, this PR:

  • Updates comments to clarify the changes in hashing logic.
  • Modifies test cases in consistenthash_test.go to mock RemoteAddr instead of LocalAddr and ensure the correct behavior is tested.

Development Checklist

  • I have added a descriptive title to this PR.
  • I have squashed related commits together.
  • I have rebased my branch on top of the latest main branch.
  • I have performed a self-review of my own code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added docstring(s) to my code.
  • I have made corresponding changes to the documentation (docs).
  • I have updated docs using make gen-docs command.
  • I have added tests for my changes.
  • I have signed all the commits.

Legal Checklist

- Updated `consistenthash.go` to use `RemoteAddr` instead of `LocalAddr` when `useSourceIP` is false. This change ensures that the key for consistent hashing reflects the client's remote address, including the IP and port.
- Modified `extractIPFromConn` to extract the IP address from `RemoteAddr` instead of `LocalAddr`.
- Updated comments to clarify the impact of using `RemoteAddr` on hashing consistency.
- Adjusted unit tests in `consistenthash_test.go` to mock `RemoteAddr` and verify the behavior changes.

These changes enhance the consistency of the hashing mechanism by focusing on the client's source IP rather than the gateway address.
@sinadarbouy sinadarbouy requested a review from mostafa September 3, 2024 18:12
Copy link
Member

@mostafa mostafa left a comment

Choose a reason for hiding this comment

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

LGTM!

@mostafa mostafa merged commit 4738ebe into gatewayd-io:main Sep 6, 2024
@mostafa mostafa mentioned this pull request Oct 6, 2024
4 tasks
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