Backport of [NET-6459] Fix issue with wanfed lan ip conflicts. into release/1.16.x (#19522)

backport of commit a47995f72dba14020cc50e69eaffdf6b131159ae

Co-authored-by: Derek Menteer <derek.menteer@hashicorp.com>
This commit is contained in:
hc-github-team-consul-core 2023-11-06 09:14:59 -06:00 committed by GitHub
parent de28b7fdce
commit d7a81cb144
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

3
.changelog/19503.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
wan-federation: Fix a bug where servers wan-federated through mesh-gateways could crash due to overlapping LAN IP addresses.
```

View File

@ -49,6 +49,7 @@ type Conn struct {
refCount int32 refCount int32
shouldClose int32 shouldClose int32
dc string
nodeName string nodeName string
addr net.Addr addr net.Addr
session muxSession session muxSession
@ -234,7 +235,7 @@ func (p *ConnPool) acquire(dc string, nodeName string, addr net.Addr) (*Conn, er
addrStr := addr.String() addrStr := addr.String()
poolKey := nodeName + ":" + addrStr poolKey := makePoolKey(dc, nodeName, addrStr)
// Check to see if there's a pooled connection available. This is up // Check to see if there's a pooled connection available. This is up
// here since it should the vastly more common case than the rest // here since it should the vastly more common case than the rest
@ -493,6 +494,7 @@ func (p *ConnPool) getNewConn(dc string, nodeName string, addr net.Addr) (*Conn,
// Wrap the connection // Wrap the connection
c := &Conn{ c := &Conn{
refCount: 1, refCount: 1,
dc: dc,
nodeName: nodeName, nodeName: nodeName,
addr: addr, addr: addr,
session: session, session: session,
@ -514,7 +516,7 @@ func (p *ConnPool) clearConn(conn *Conn) {
// Clear from the cache // Clear from the cache
addrStr := conn.addr.String() addrStr := conn.addr.String()
poolKey := conn.nodeName + ":" + addrStr poolKey := makePoolKey(conn.dc, conn.nodeName, addrStr)
p.Lock() p.Lock()
if c, ok := p.pool[poolKey]; ok && c == conn { if c, ok := p.pool[poolKey]; ok && c == conn {
delete(p.pool, poolKey) delete(p.pool, poolKey)
@ -716,3 +718,8 @@ func (p *ConnPool) reap() {
p.Unlock() p.Unlock()
} }
} }
// makePoolKey generates a unique key for grouping connections together into a pool.
func makePoolKey(dc, nodeName, addrStr string) string {
return dc + ":" + nodeName + ":" + addrStr
}