Skip to content

Commit

Permalink
quicreuse: prevent gc and shutdown of externally constructed transports
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Dec 21, 2024
1 parent e2e5729 commit 8db5f55
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion p2p/transport/quicreuse/connmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (c *ConnManager) AddTransport(network string, tr QUICTransport, conn net.Pa
refCountedTr := &refcountedTransport{
QUICTransport: tr,
packetConn: conn,
refCount: 1,
isExternal: true,
}

var reuse *reuse
Expand Down
16 changes: 13 additions & 3 deletions p2p/transport/quicreuse/reuse.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type refcountedTransport struct {
mutex sync.Mutex
refCount int
unusedSince time.Time
isExternal bool // if the transport was created externally, it is neither gc-ed nor closed

assocations map[any]struct{}
}
Expand Down Expand Up @@ -151,6 +152,9 @@ func (c *refcountedTransport) DecreaseCount() {
}

func (c *refcountedTransport) ShouldGarbageCollect(now time.Time) bool {
if c.isExternal {
return false
}
c.mutex.Lock()
defer c.mutex.Unlock()
return !c.unusedSince.IsZero() && c.unusedSince.Add(maxUnusedDuration).Before(now)
Expand Down Expand Up @@ -193,14 +197,20 @@ func (r *reuse) gc() {
defer func() {
r.mutex.Lock()
for _, tr := range r.globalListeners {
tr.Close()
if !tr.isExternal {
tr.Close()
}
}
for _, tr := range r.globalDialers {
tr.Close()
if !tr.isExternal {
tr.Close()
}
}
for _, trs := range r.unicast {
for _, tr := range trs {
tr.Close()
if !tr.isExternal {
tr.Close()
}
}
}
r.mutex.Unlock()
Expand Down

0 comments on commit 8db5f55

Please sign in to comment.