Skip to content

Commit 4738ebe

Browse files
authored
fix(network): use RemoteAddr for consistent hashing key (#599)
1 parent 54d0c69 commit 4738ebe

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

network/consistenthash.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ func (ch *ConsistentHash) NextProxy(conn IConnWrapper) (IProxy, *gerr.GatewayDEr
4545
}
4646
key = sourceIP
4747
} else {
48-
key = conn.LocalAddr().String() // Fallback to use full address as the key if `useSourceIp` is false
48+
// Fallback to using the full remote address (IP:port) as the key if `useSourceIP` is false.
49+
// This effectively disables consistent hashing, as the remote address has a random port each time.
50+
key = conn.RemoteAddr().String()
4951
}
5052

5153
hash := hashKey(key)
@@ -74,10 +76,10 @@ func hashKey(key string) uint64 {
7476
return murmur3.Sum64([]byte(key))
7577
}
7678

77-
// extractIPFromConn extracts the IP address from the connection's local address. It splits the address
79+
// extractIPFromConn extracts the IP address from the connection's remote address. It splits the address
7880
// into IP and port components and returns the IP part. This is useful for hashing based on the source IP.
7981
func extractIPFromConn(con IConnWrapper) (string, error) {
80-
addr := con.LocalAddr().String()
82+
addr := con.RemoteAddr().String() // RemoteAddr is the address of the request, LocalAddress is the gateway address.
8183
// addr will be in the format "IP:port"
8284
ip, _, err := net.SplitHostPort(addr)
8385
if err != nil {

network/consistenthash_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ func TestConsistentHashNextProxyUseSourceIpExists(t *testing.T) {
4242
consistentHash := NewConsistentHash(server, originalStrategy)
4343
mockConn := new(MockConnWrapper)
4444

45-
// Mock LocalAddr to return a specific IP:port format
45+
// Mock RemoteAddr to return a specific IP:port format
4646
mockAddr := &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 1234}
47-
mockConn.On("LocalAddr").Return(mockAddr)
47+
mockConn.On("RemoteAddr").Return(mockAddr)
4848

4949
key := "192.168.1.1"
5050
hash := hashKey(key)
@@ -77,9 +77,9 @@ func TestConsistentHashNextProxyUseFullAddress(t *testing.T) {
7777
}
7878
mockStrategy := NewRoundRobin(server)
7979

80-
// Mock LocalAddr to return full address
80+
// Mock RemoteAddr to return full address
8181
mockAddr := &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 1234}
82-
mockConn.On("LocalAddr").Return(mockAddr)
82+
mockConn.On("RemoteAddr").Return(mockAddr)
8383

8484
consistentHash := NewConsistentHash(server, mockStrategy)
8585

@@ -120,8 +120,8 @@ func TestConsistentHashNextProxyConcurrency(t *testing.T) {
120120
// Mock IP addresses
121121
mockAddr1 := &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 1234}
122122
mockAddr2 := &net.TCPAddr{IP: net.ParseIP("192.168.1.2"), Port: 1234}
123-
conn1.On("LocalAddr").Return(mockAddr1)
124-
conn2.On("LocalAddr").Return(mockAddr2)
123+
conn1.On("RemoteAddr").Return(mockAddr1)
124+
conn2.On("RemoteAddr").Return(mockAddr2)
125125

126126
// Initialize the ConsistentHash
127127
consistentHash := NewConsistentHash(server, originalStrategy)

0 commit comments

Comments
 (0)