Skip to content

Commit

Permalink
drop incoming sessions when the accept queue fills up (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann authored Sep 7, 2022
1 parent f49dd4f commit 7714b04
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
9 changes: 7 additions & 2 deletions p2p/transport/webtransport/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,13 @@ func (l *listener) httpHandler(w http.ResponseWriter, r *http.Request) {
return
}

// TODO: think about what happens when this channel fills up
l.queue <- newConn(l.transport, sess, sconn, connScope)
select {
case l.queue <- newConn(l.transport, sess, sconn, connScope):
default:
log.Debugw("accept queue full, dropping incoming connection", "peer", sconn.RemotePeer(), "addr", r.RemoteAddr, "error", err)
sess.Close()
connScope.Done()
}
}

func (l *listener) Accept() (tpt.CapableConn, error) {
Expand Down
32 changes: 32 additions & 0 deletions p2p/transport/webtransport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/libp2p/go-libp2p/core/network"
mocknetwork "github.com/libp2p/go-libp2p/core/network/mocks"
"github.com/libp2p/go-libp2p/core/peer"
tpt "github.com/libp2p/go-libp2p/core/transport"

"github.com/golang/mock/gomock"
ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -510,3 +511,34 @@ func TestStaticTLSConf(t *testing.T) {
defer conn.Close()
})
}

func TestAcceptQueueFilledUp(t *testing.T) {
serverID, serverKey := newIdentity(t)
tr, err := libp2pwebtransport.New(serverKey, nil, network.NullResourceManager)
require.NoError(t, err)
defer tr.(io.Closer).Close()
ln, err := tr.Listen(ma.StringCast("/ip4/127.0.0.1/udp/0/quic/webtransport"))
require.NoError(t, err)
defer ln.Close()

newConn := func() (tpt.CapableConn, error) {
t.Helper()
_, key := newIdentity(t)
cl, err := libp2pwebtransport.New(key, nil, network.NullResourceManager)
require.NoError(t, err)
defer cl.(io.Closer).Close()
return cl.Dial(context.Background(), ln.Multiaddr(), serverID)
}

for i := 0; i < 16; i++ {
conn, err := newConn()
require.NoError(t, err)
defer conn.Close()
}

conn, err := newConn()
if err == nil {
_, err = conn.AcceptStream()
}
require.Error(t, err)
}

0 comments on commit 7714b04

Please sign in to comment.