Skip to content

Commit

Permalink
fix(network): use RemoteAddr for consistent hashing key (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinadarbouy authored Sep 6, 2024
1 parent 54d0c69 commit 4738ebe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
8 changes: 5 additions & 3 deletions network/consistenthash.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ func (ch *ConsistentHash) NextProxy(conn IConnWrapper) (IProxy, *gerr.GatewayDEr
}
key = sourceIP
} else {
key = conn.LocalAddr().String() // Fallback to use full address as the key if `useSourceIp` is false
// Fallback to using the full remote address (IP:port) as the key if `useSourceIP` is false.
// This effectively disables consistent hashing, as the remote address has a random port each time.
key = conn.RemoteAddr().String()
}

hash := hashKey(key)
Expand Down Expand Up @@ -74,10 +76,10 @@ func hashKey(key string) uint64 {
return murmur3.Sum64([]byte(key))
}

// extractIPFromConn extracts the IP address from the connection's local address. It splits the address
// extractIPFromConn extracts the IP address from the connection's remote address. It splits the address
// into IP and port components and returns the IP part. This is useful for hashing based on the source IP.
func extractIPFromConn(con IConnWrapper) (string, error) {
addr := con.LocalAddr().String()
addr := con.RemoteAddr().String() // RemoteAddr is the address of the request, LocalAddress is the gateway address.
// addr will be in the format "IP:port"
ip, _, err := net.SplitHostPort(addr)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions network/consistenthash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func TestConsistentHashNextProxyUseSourceIpExists(t *testing.T) {
consistentHash := NewConsistentHash(server, originalStrategy)
mockConn := new(MockConnWrapper)

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

key := "192.168.1.1"
hash := hashKey(key)
Expand Down Expand Up @@ -77,9 +77,9 @@ func TestConsistentHashNextProxyUseFullAddress(t *testing.T) {
}
mockStrategy := NewRoundRobin(server)

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

consistentHash := NewConsistentHash(server, mockStrategy)

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

// Initialize the ConsistentHash
consistentHash := NewConsistentHash(server, originalStrategy)
Expand Down

0 comments on commit 4738ebe

Please sign in to comment.