Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transport: add GatedMaListener type #3186

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sukunrt
Copy link
Member

@sukunrt sukunrt commented Feb 12, 2025

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

Copy link
Collaborator

@MarcoPolo MarcoPolo left a comment

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.

// Upgrader is a multistream upgrader that can upgrade an underlying connection
// to a full transport connection (secure and multiplexed).
type Upgrader interface {
// 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
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?

p2p/transport/tcpreuse/listener.go Outdated Show resolved Hide resolved
l.mx.Lock()
l.scopes[conn] = scope
// TODO: This should be 10 seconds?
time.AfterFunc(time.Minute, func() {
Copy link
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

}

func (l *httpNetListener) PopScope(c net.Conn) (network.ConnManagementScope, error) {
if tc, ok := c.(*tls.Conn); ok {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Interface{NetConn() ...} check instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm using tls.Conn incase we ever implement a NetConn() method for maConn and that breaks this. *tls.Conn api is guaranteed by the go 1 compatibility guarantee now.

p2p/transport/websocket/http_listener.go Outdated Show resolved Hide resolved
ma "github.com/multiformats/go-multiaddr"
)

type httpNetListener struct {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You may find Server.ConnState.

Copy link
Member Author

@sukunrt sukunrt Feb 16, 2025

Choose a reason for hiding this comment

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

Do you think we need that?
Our purpose here is similar to how we handle webtransport upgrade. I've written this(now) similar to that code using a *negotiatingConn

Copy link
Collaborator

Choose a reason for hiding this comment

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

I meant to say you may find that helpful, but let me take a look at the latest changes.

@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)
Copy link
Member Author

@sukunrt sukunrt Feb 17, 2025

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.

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
2 participants