From 7773888b80bdab3c1e6292941898cd3348122575 Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Thu, 13 Jul 2023 11:46:38 -0700 Subject: [PATCH] Finish renaming conn -> transport where appropriate --- p2p/transport/quicreuse/connmgr.go | 36 +++++++-------- p2p/transport/quicreuse/connmgr_test.go | 14 +++--- p2p/transport/quicreuse/listener.go | 14 +++--- p2p/transport/quicreuse/reuse.go | 58 ++++++++++++------------- 4 files changed, 61 insertions(+), 61 deletions(-) diff --git a/p2p/transport/quicreuse/connmgr.go b/p2p/transport/quicreuse/connmgr.go index 1ce809e7d6..c12b86671a 100644 --- a/p2p/transport/quicreuse/connmgr.go +++ b/p2p/transport/quicreuse/connmgr.go @@ -23,23 +23,23 @@ type ConnManager struct { serverConfig *quic.Config clientConfig *quic.Config - connsMu sync.Mutex - conns map[string]connListenerEntry + quicListenersMu sync.Mutex + quicListeners map[string]quicListenerEntry srk quic.StatelessResetKey mt *metricsTracer } -type connListenerEntry struct { +type quicListenerEntry struct { refCount int - ln *connListener + ln *quicListener } func NewConnManager(statelessResetKey quic.StatelessResetKey, opts ...Option) (*ConnManager, error) { cm := &ConnManager{ enableReuseport: true, enableDraft29: true, - conns: make(map[string]connListenerEntry), + quicListeners: make(map[string]quicListenerEntry), srk: statelessResetKey, } for _, o := range opts { @@ -104,22 +104,22 @@ func (c *ConnManager) ListenQUIC(addr ma.Multiaddr, tlsConf *tls.Config, allowWi return nil, err } - c.connsMu.Lock() - defer c.connsMu.Unlock() + c.quicListenersMu.Lock() + defer c.quicListenersMu.Unlock() key := laddr.String() - entry, ok := c.conns[key] + entry, ok := c.quicListeners[key] if !ok { - conn, err := c.transportForListen(netw, laddr) + tr, err := c.transportForListen(netw, laddr) if err != nil { return nil, err } - ln, err := newConnListener(conn, c.serverConfig, c.enableDraft29) + ln, err := newQuicListener(tr, c.serverConfig, c.enableDraft29) if err != nil { return nil, err } - key = conn.LocalAddr().String() - entry = connListenerEntry{ln: ln} + key = tr.LocalAddr().String() + entry = quicListenerEntry{ln: ln} } l, err := entry.ln.Add(tlsConf, allowWindowIncrease, func() { c.onListenerClosed(key) }) if err != nil { @@ -129,21 +129,21 @@ func (c *ConnManager) ListenQUIC(addr ma.Multiaddr, tlsConf *tls.Config, allowWi return nil, err } entry.refCount++ - c.conns[key] = entry + c.quicListeners[key] = entry return l, nil } func (c *ConnManager) onListenerClosed(key string) { - c.connsMu.Lock() - defer c.connsMu.Unlock() + c.quicListenersMu.Lock() + defer c.quicListenersMu.Unlock() - entry := c.conns[key] + entry := c.quicListeners[key] entry.refCount = entry.refCount - 1 if entry.refCount <= 0 { - delete(c.conns, key) + delete(c.quicListeners, key) entry.ln.Close() } else { - c.conns[key] = entry + c.quicListeners[key] = entry } } diff --git a/p2p/transport/quicreuse/connmgr_test.go b/p2p/transport/quicreuse/connmgr_test.go index 8b8846c3f2..92c7e31250 100644 --- a/p2p/transport/quicreuse/connmgr_test.go +++ b/p2p/transport/quicreuse/connmgr_test.go @@ -26,12 +26,12 @@ func checkClosed(t *testing.T, cm *ConnManager) { continue } r.mutex.Lock() - for _, conn := range r.globalListeners { - require.Zero(t, conn.GetCount()) + for _, tr := range r.globalListeners { + require.Zero(t, tr.GetCount()) } - for _, conns := range r.unicast { - for _, conn := range conns { - require.Zero(t, conn.GetCount()) + for _, trs := range r.unicast { + for _, tr := range trs { + require.Zero(t, tr.GetCount()) } } r.mutex.Unlock() @@ -93,7 +93,7 @@ func testListenOnSameProto(t *testing.T, enableReuseport bool) { // type-asserted to a UDPConn. That way, it can use all kinds of optimizations. func TestConnectionPassedToQUICForListening(t *testing.T) { if runtime.GOOS == "windows" { - t.Skip("skipping on windows. Not sure why this fails") + t.Skip("skipping on windows. Windows doesn't support these optimizations") } cm, err := NewConnManager([32]byte{}, DisableReuseport()) require.NoError(t, err) @@ -153,7 +153,7 @@ func TestAcceptErrorGetCleanedUp(t *testing.T) { // in order to enable features like batch processing and ECN. func TestConnectionPassedToQUICForDialing(t *testing.T) { if runtime.GOOS == "windows" { - t.Skip("skipping on windows. Not sure why this fails") + t.Skip("skipping on windows. Windows doesn't support these optimizations") } cm, err := NewConnManager([32]byte{}, DisableReuseport()) require.NoError(t, err) diff --git a/p2p/transport/quicreuse/listener.go b/p2p/transport/quicreuse/listener.go index 42ac6217a9..cd1fbcb5c2 100644 --- a/p2p/transport/quicreuse/listener.go +++ b/p2p/transport/quicreuse/listener.go @@ -28,7 +28,7 @@ type protoConf struct { allowWindowIncrease func(conn quic.Connection, delta uint64) bool } -type connListener struct { +type quicListener struct { l *quic.Listener transport refCountedQuicTransport running chan struct{} @@ -38,7 +38,7 @@ type connListener struct { protocols map[string]protoConf } -func newConnListener(c refCountedQuicTransport, quicConfig *quic.Config, enableDraft29 bool) (*connListener, error) { +func newQuicListener(c refCountedQuicTransport, quicConfig *quic.Config, enableDraft29 bool) (*quicListener, error) { localMultiaddrs := make([]ma.Multiaddr, 0, 2) a, err := ToQuicMultiaddr(c.LocalAddr(), quic.Version1) if err != nil { @@ -52,7 +52,7 @@ func newConnListener(c refCountedQuicTransport, quicConfig *quic.Config, enableD } localMultiaddrs = append(localMultiaddrs, a) } - cl := &connListener{ + cl := &quicListener{ protocols: map[string]protoConf{}, running: make(chan struct{}), transport: c, @@ -85,7 +85,7 @@ func newConnListener(c refCountedQuicTransport, quicConfig *quic.Config, enableD return cl, nil } -func (l *connListener) allowWindowIncrease(conn quic.Connection, delta uint64) bool { +func (l *quicListener) allowWindowIncrease(conn quic.Connection, delta uint64) bool { l.protocolsMu.Lock() defer l.protocolsMu.Unlock() @@ -96,7 +96,7 @@ func (l *connListener) allowWindowIncrease(conn quic.Connection, delta uint64) b return conf.allowWindowIncrease(conn, delta) } -func (l *connListener) Add(tlsConf *tls.Config, allowWindowIncrease func(conn quic.Connection, delta uint64) bool, onRemove func()) (Listener, error) { +func (l *quicListener) Add(tlsConf *tls.Config, allowWindowIncrease func(conn quic.Connection, delta uint64) bool, onRemove func()) (Listener, error) { l.protocolsMu.Lock() defer l.protocolsMu.Unlock() @@ -128,7 +128,7 @@ func (l *connListener) Add(tlsConf *tls.Config, allowWindowIncrease func(conn qu return ln, nil } -func (l *connListener) Run() error { +func (l *quicListener) Run() error { defer close(l.running) defer l.transport.DecreaseCount() for { @@ -152,7 +152,7 @@ func (l *connListener) Run() error { } } -func (l *connListener) Close() error { +func (l *quicListener) Close() error { err := l.l.Close() <-l.running // wait for Run to return return err diff --git a/p2p/transport/quicreuse/reuse.go b/p2p/transport/quicreuse/reuse.go index c7352733d1..1584b29254 100644 --- a/p2p/transport/quicreuse/reuse.go +++ b/p2p/transport/quicreuse/reuse.go @@ -20,7 +20,7 @@ type refCountedQuicTransport interface { Close() error - // count conn reference + // count transport reference DecreaseCount() IncreaseCount() @@ -169,28 +169,28 @@ func (r *reuse) gc() { case <-ticker.C: now := time.Now() r.mutex.Lock() - for key, conn := range r.globalListeners { - if conn.ShouldGarbageCollect(now) { - conn.Close() + for key, tr := range r.globalListeners { + if tr.ShouldGarbageCollect(now) { + tr.Close() delete(r.globalListeners, key) } } - for key, conn := range r.globalDialers { - if conn.ShouldGarbageCollect(now) { - conn.Close() + for key, tr := range r.globalDialers { + if tr.ShouldGarbageCollect(now) { + tr.Close() delete(r.globalDialers, key) } } - for ukey, conns := range r.unicast { - for key, conn := range conns { - if conn.ShouldGarbageCollect(now) { - conn.Close() - delete(conns, key) + for ukey, trs := range r.unicast { + for key, tr := range trs { + if tr.ShouldGarbageCollect(now) { + tr.Close() + delete(trs, key) } } - if len(conns) == 0 { + if len(trs) == 0 { delete(r.unicast, ukey) - // If we've dropped all connections with a unicast binding, + // If we've dropped all transports with a unicast binding, // assume our routes may have changed. if len(r.unicast) == 0 { r.routes = nil @@ -236,27 +236,27 @@ func (r *reuse) TransportForDial(network string, raddr *net.UDPAddr) (*refcounte func (r *reuse) transportForDialLocked(network string, source *net.IP) (*refcountedTransport, error) { if source != nil { - // We already have at least one suitable connection... - if conns, ok := r.unicast[source.String()]; ok { + // We already have at least one suitable transport... + if trs, ok := r.unicast[source.String()]; ok { // ... we don't care which port we're dialing from. Just use the first. - for _, c := range conns { - return c, nil + for _, tr := range trs { + return tr, nil } } } - // Use a connection listening on 0.0.0.0 (or ::). + // Use a transport listening on 0.0.0.0 (or ::). // Again, we don't care about the port number. - for _, conn := range r.globalListeners { - return conn, nil + for _, tr := range r.globalListeners { + return tr, nil } - // Use a connection we've previously dialed from - for _, conn := range r.globalDialers { - return conn, nil + // Use a transport we've previously dialed from + for _, tr := range r.globalDialers { + return tr, nil } - // We don't have a connection that we can use for dialing. + // We don't have a transport that we can use for dialing. // Dial a new connection from a random port. var addr *net.UDPAddr switch network { @@ -284,17 +284,17 @@ func (r *reuse) TransportForListen(network string, laddr *net.UDPAddr) (*refcoun r.mutex.Lock() defer r.mutex.Unlock() - // Check if we can reuse a connection we have already dialed out from. - // We reuse a connection from globalDialers when the requested port is 0 or the requested + // Check if we can reuse a transport we have already dialed out from. + // We reuse a transport from globalDialers when the requested port is 0 or the requested // port is already in the globalDialers. - // If we are reusing a connection from globalDialers, we move the globalDialers entry to + // If we are reusing a transport from globalDialers, we move the globalDialers entry to // globalListeners if laddr.IP.IsUnspecified() { var rTr *refcountedTransport var localAddr *net.UDPAddr if laddr.Port == 0 { - // the requested port is 0, we can reuse any connection + // the requested port is 0, we can reuse any transport for _, tr := range r.globalDialers { rTr = tr localAddr = rTr.LocalAddr().(*net.UDPAddr)