Browse Source

drop incoming sessions when the accept queue fills up (#24)

pull/1737/head
Marten Seemann 2 years ago
committed by GitHub
parent
commit
7714b04df5
  1. 9
      p2p/transport/webtransport/listener.go
  2. 32
      p2p/transport/webtransport/transport_test.go

9
p2p/transport/webtransport/listener.go

@ -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) {

32
p2p/transport/webtransport/transport_test.go

@ -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"
@ -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)
}

Loading…
Cancel
Save