Skip to content

Commit

Permalink
fix: TestStatelessReset/reuseport_off test
Browse files Browse the repository at this point in the history
close listener underlying connection when reuseport is disabled and the close
method called

Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Aug 16, 2022
1 parent 21d5011 commit eacccb5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions p2p/transport/quic/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type pConn interface {
// count conn reference
DecreaseCount()
IncreaseCount()

// is reuseport enable
Reuseport() bool
}

type conn struct {
Expand Down
9 changes: 8 additions & 1 deletion p2p/transport/quic/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,14 @@ func (l *listener) setupConn(qconn quic.Connection) (*conn, error) {
// Close closes the listener.
func (l *listener) Close() error {
defer l.conn.DecreaseCount()
return l.quicListener.Close()

err := l.quicListener.Close()

if !l.conn.Reuseport() {
// close the underlying connection
l.conn.Close()
}
return err
}

// Addr returns the address of this listener.
Expand Down
4 changes: 4 additions & 0 deletions p2p/transport/quic/reuse.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (c *reuseConn) DecreaseCount() {
c.mutex.Unlock()
}

func (c *reuseConn) Reuseport() bool {
return true
}

func (c *reuseConn) ShouldGarbageCollect(now time.Time) bool {
c.mutex.Lock()
defer c.mutex.Unlock()
Expand Down
6 changes: 4 additions & 2 deletions p2p/transport/quic/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ type noreuseConn struct {
*net.UDPConn
}

func (c *noreuseConn) IncreaseCount() {}
func (c *noreuseConn) DecreaseCount() {}
func (c *noreuseConn) IncreaseCount() {}
func (c *noreuseConn) DecreaseCount() {}
func (c *noreuseConn) Reuseport() bool { return false }

type connManager struct {
reuseUDP4 *reuse
Expand Down Expand Up @@ -406,6 +407,7 @@ func (t *transport) Listen(addr ma.Multiaddr) (tpt.Listener, error) {
}
ln, err := newListener(conn, t, t.localPeer, t.privKey, t.identity, t.rcmgr)
if err != nil {
conn.Close()
conn.DecreaseCount()
return nil, err
}
Expand Down

0 comments on commit eacccb5

Please sign in to comment.