Skip to content

Commit

Permalink
rename globalDial to fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Apr 27, 2023
1 parent 8efdc6a commit 1ecd04a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
29 changes: 16 additions & 13 deletions p2p/transport/quicreuse/reuse.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,18 @@ type reuse struct {
routes routing.Router
unicast map[string] /* IP.String() */ map[int] /* port */ *reuseConn
// global contains connections that are listening on 0.0.0.0 / ::
global map[int]*reuseConn
globalDial map[int]*reuseConn
global map[int]*reuseConn
// fallback contains connections that we've dialed out of. connections
// are reused from this map if no connection is available in the global
// map
fallback map[int]*reuseConn
}

func newReuse() *reuse {
r := &reuse{
unicast: make(map[string]map[int]*reuseConn),
global: make(map[int]*reuseConn),
globalDial: make(map[int]*reuseConn),
fallback: make(map[int]*reuseConn),
closeChan: make(chan struct{}),
gcStopChan: make(chan struct{}),
}
Expand All @@ -97,7 +100,7 @@ func (r *reuse) gc() {
for _, conn := range r.global {
conn.Close()
}
for _, conn := range r.globalDial {
for _, conn := range r.fallback {
conn.Close()
}
for _, conns := range r.unicast {
Expand All @@ -124,10 +127,10 @@ func (r *reuse) gc() {
delete(r.global, key)
}
}
for key, conn := range r.globalDial {
for key, conn := range r.fallback {
if conn.ShouldGarbageCollect(now) {
conn.Close()
delete(r.globalDial, key)
delete(r.fallback, key)
}
}
for ukey, conns := range r.unicast {
Expand Down Expand Up @@ -201,7 +204,7 @@ func (r *reuse) dialLocked(network string, source *net.IP) (*reuseConn, error) {
}

// Use a connection we've previously dialed from
for _, conn := range r.globalDial {
for _, conn := range r.fallback {
return conn, nil
}

Expand All @@ -219,7 +222,7 @@ func (r *reuse) dialLocked(network string, source *net.IP) (*reuseConn, error) {
return nil, err
}
rconn := newReuseConn(conn)
r.globalDial[conn.LocalAddr().(*net.UDPAddr).Port] = rconn
r.fallback[conn.LocalAddr().(*net.UDPAddr).Port] = rconn
return rconn, nil
}

Expand All @@ -230,10 +233,10 @@ func (r *reuse) Listen(network string, laddr *net.UDPAddr) (*reuseConn, error) {
var rconn *reuseConn
var localAddr *net.UDPAddr

// reuse the connection if we've dialed out of this port already
// reuse the fallback connection if we've dialed out from this port already
if laddr.IP.IsUnspecified() {
if _, ok := r.globalDial[laddr.Port]; ok {
rconn = r.globalDial[laddr.Port]
if _, ok := r.fallback[laddr.Port]; ok {
rconn = r.fallback[laddr.Port]
localAddr = rconn.UDPConn.LocalAddr().(*net.UDPAddr)
}
}
Expand All @@ -253,8 +256,8 @@ func (r *reuse) Listen(network string, laddr *net.UDPAddr) (*reuseConn, error) {
// The kernel already checked that the laddr is not already listen
// so we need not check here (when we create ListenUDP).
r.global[localAddr.Port] = rconn
// delete the entry from dial map in case we are reusing this connection
delete(r.globalDial, localAddr.Port)
// delete the entry from fallback map in case we are reusing this connection
delete(r.fallback, localAddr.Port)
return rconn, nil
}

Expand Down
2 changes: 1 addition & 1 deletion p2p/transport/quicreuse/reuse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func closeAllConns(reuse *reuse) {
conn.DecreaseCount()
}
}
for _, conn := range reuse.globalDial {
for _, conn := range reuse.fallback {
for conn.GetCount() > 0 {
conn.DecreaseCount()
}
Expand Down

0 comments on commit 1ecd04a

Please sign in to comment.