Skip to content
This repository was archived by the owner on May 26, 2022. It is now read-only.

Commit bbe9904

Browse files
don't start a Go routine for every connection dialed
In the swarm, we're calling Close for every connection before it is garbage collected. We therefore don't need to start a Go routine here just to see when a connection is closed. We now also increment the reuse counter every time a connection is dialed. This simplifies closing the connection.
1 parent 2ca7bc3 commit bbe9904

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

conn.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import (
88
"github.com/libp2p/go-libp2p-core/peer"
99
tpt "github.com/libp2p/go-libp2p-core/transport"
1010

11-
quic "github.com/lucas-clemente/quic-go"
11+
"github.com/lucas-clemente/quic-go"
1212
ma "github.com/multiformats/go-multiaddr"
1313
)
1414

1515
type conn struct {
1616
sess quic.Session
17+
pconn *reuseConn
1718
transport tpt.Transport
1819

1920
localPeer peer.ID
@@ -27,7 +28,11 @@ type conn struct {
2728

2829
var _ tpt.CapableConn = &conn{}
2930

31+
// Close closes the connection.
32+
// It must be called even if the peer closed the connection in order for
33+
// garbage collection to properly work in this package.
3034
func (c *conn) Close() error {
35+
c.pconn.DecreaseCount()
3136
return c.sess.CloseWithError(0, "")
3237
}
3338

conn_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,14 @@ func TestStatelessReset(t *testing.T) {
353353
require.NoError(t, err)
354354
conn, err := clientTransport.Dial(context.Background(), proxyAddr, serverID)
355355
require.NoError(t, err)
356+
connChan := make(chan tpt.CapableConn)
356357
go func() {
357358
conn, err := ln.Accept()
358359
require.NoError(t, err)
359360
str, err := conn.OpenStream(context.Background())
360361
require.NoError(t, err)
361362
str.Write([]byte("foobar"))
363+
connChan <- conn
362364
}()
363365

364366
str, err := conn.AcceptStream()
@@ -370,6 +372,7 @@ func TestStatelessReset(t *testing.T) {
370372
// This prevents the CONNECTION_CLOSE from reaching the client.
371373
atomic.StoreUint32(&drop, 1)
372374
ln.Close()
375+
(<-connChan).Close()
373376
// require.NoError(t, ln.Close())
374377
time.Sleep(2000 * time.Millisecond) // give the kernel some time to free the UDP port
375378
ln = runServer(t, serverTransport, fmt.Sprintf("/ip4/127.0.0.1/udp/%d/quic", serverPort))

listener.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,18 @@ func (l *listener) setupConn(sess quic.Session) (*conn, error) {
102102
if err != nil {
103103
return nil, err
104104
}
105-
106105
remotePeerID, err := peer.IDFromPublicKey(remotePubKey)
107106
if err != nil {
108107
return nil, err
109108
}
110-
111109
remoteMultiaddr, err := toQuicMultiaddr(sess.RemoteAddr())
112110
if err != nil {
113111
return nil, err
114112
}
115-
113+
l.conn.IncreaseCount()
116114
return &conn{
117115
sess: sess,
116+
pconn: l.conn,
118117
transport: l.transport,
119118
localPeer: l.localPeer,
120119
localMultiaddr: l.localMultiaddr,

transport.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,6 @@ func (t *transport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (tp
207207
pconn.DecreaseCount()
208208
return nil, errors.New("go-libp2p-quic-transport BUG: expected remote pub key to be set")
209209
}
210-
go func() {
211-
<-sess.Context().Done()
212-
pconn.DecreaseCount()
213-
}()
214210

215211
localMultiaddr, err := toQuicMultiaddr(pconn.LocalAddr())
216212
if err != nil {
@@ -219,6 +215,7 @@ func (t *transport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (tp
219215
}
220216
conn := &conn{
221217
sess: sess,
218+
pconn: pconn,
222219
transport: t,
223220
privKey: t.privKey,
224221
localPeer: t.localPeer,

0 commit comments

Comments
 (0)