Skip to content

transport: add GatedMaListener type - #3186

Merged
sukunrt merged 2 commits into
masterfrom
sukun/tcpreuse-refactor
Mar 25, 2025
Merged

sukunrt merged 2 commits into
masterfrom
sukun/tcpreuse-refactor

Conversation

@sukunrt

@sukunrt sukunrt commented Feb 12, 2025

Copy link
Copy Markdown
Member

This introduces a new GatedMaListener type which gates conns
accepted from a manet.Listener with a gater and creates the rcmgr
scope for it. Explicitly passing the scope allows for many guardrails
that the previous interface assertion didn't.

This breaks the previous responsibility of the upgradeListener method
into two, one gating the connection initially, and the other upgrading
the connection with a security and muxer selection.

This split makes it easy to gate the connection with the resource
manager as early as possible. This is especially true for websocket
because we want to gate the connection just after the TCP connection is
established, and not after the tls handshake + websocket upgrade is
completed.

fixes: #3182

@MarcoPolo MarcoPolo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brief review. Overall I like being explicit here.

// UpgradeListener upgrades the passed multiaddr-net listener into a full libp2p-transport listener.
//
// Deprecated: Use UpgradeGatedManetListener(upgrader.GateManetListener(manet.Listener)) instead.
UpgradeListener(Transport, manet.Listener) Listener

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is anyone besides us using this? Should we just remove it?

Comment thread p2p/transport/tcpreuse/listener.go Outdated
l.mx.Lock()
l.scopes[conn] = scope
// TODO: This should be 10 seconds?
time.AfterFunc(time.Minute, func() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this in case the connection is closed or dropped? This is kind of hacky

Comment thread p2p/transport/websocket/http_listener.go Outdated
Comment thread p2p/transport/websocket/http_listener.go Outdated
Comment thread p2p/transport/websocket/http_listener.go Outdated
@sukunrt
sukunrt force-pushed the sukun/tcpreuse-refactor branch 2 times, most recently from a3eadf2 to 78cd22b Compare February 15, 2025 17:25
@sukunrt sukunrt changed the title transport: add UnsecuredListener type transport: add GatedMaListener type Feb 15, 2025
@sukunrt
sukunrt force-pushed the sukun/tcpreuse-refactor branch 3 times, most recently from ae94a2d to 6b3b600 Compare February 16, 2025 15:03
@sukunrt
sukunrt marked this pull request as ready for review February 16, 2025 15:03
@sukunrt
sukunrt requested a review from MarcoPolo February 16, 2025 15:03
@sukunrt
sukunrt force-pushed the sukun/tcpreuse-refactor branch from 6b3b600 to 512a7d5 Compare February 16, 2025 15:21
This introduces a new GatedMaListener type which gates conns
accepted from a manet.Listener with a gater and creates the rcmgr
scope for it. Explicitly passing the scope allows for many guardrails
that the previous interface assertion didn't.

This breaks the previous responsibility of the upgradeListener method
into two, one gating the connection initially, and the other upgrading
the connection with a security and muxer selection.

This split makes it easy to gate the connection with the resource
manager as early as possible. This is especially true for websocket
because we want to gate the connection just after the TCP connection is
established, and not after the tls handshake + websocket upgrade is
completed.
@sukunrt
sukunrt force-pushed the sukun/tcpreuse-refactor branch from 512a7d5 to f1b1ee9 Compare February 16, 2025 22:38
if err != nil {
return nil, err
}
gmal = upgrader.GateMaListener(mal)

@sukunrt sukunrt Feb 17, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This changes the address reported to the resource manager and connection gater from:
/ip4/a.b.c.d/tcp/xx/ws to /ip4/a.b.c.d/tcp/xx
We are not providing the /ws component anymore. This is the same behavior as what the shared tcp listener does as it doesn't know whether the connection will be a websocket or tcp one.

This is easy enough to fix, but that would make the behavior between the shared tcp listener and unshared one inconsistent.

I don't think the resource manager cares, as we don't have a limit of connections per transport.
The conn gater might care about this. If that's the case, we can move the conngater.InterceptAccept into GateMaListener and have the gatedMaListener only manage resource manager scope.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I understand. Are we not including the ws component in the
connection's Multiaddr methods? We probably should since it's often useful for
debugging and understanding where a connection came from.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need a method on the Scope / Rcmgr for this. We don't know whether it'll be a tcp or a ws connection just when we've accepted the tcp connection.

@MarcoPolo MarcoPolo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Just one question around the multiaddr not including "ws". I think it probably should, if only to help debugging.

Comment thread p2p/transport/tcp/metrics.go Outdated
Comment thread p2p/transport/websocket/listener.go Outdated
Comment thread p2p/net/upgrader/upgrader.go
Comment thread p2p/transport/tcp/tcp.go

if t.enableMetrics {
list = newTracingListener(&tcpListener{list, 0}, t.metricsCollector)
// TODO: Fix this: The tcpListener wrapping should happen on both enableMetrics and disabledMetrics path

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would it happen on the disable metrics path?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are two wrappings. One is for metrics. That shouldn't happen on metrics path. The other is for setting socket options like SoLinger(0). That should happen on both parts.

Comment thread p2p/transport/tcpreuse/listener.go Outdated
Comment thread p2p/transport/websocket/conn.go Outdated
if err != nil {
return nil, err
}
gmal = upgrader.GateMaListener(mal)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I understand. Are we not including the ws component in the
connection's Multiaddr methods? We probably should since it's often useful for
debugging and understanding where a connection came from.

@sukunrt
sukunrt merged commit 6249e68 into master Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor shared TCP Listener's resource manager scope usage

3 participants