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?

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

@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.

Copy link
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
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.

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

return nil, nil, err
}

// TODO(sukunrt): Should we log and ignore this error? We can proceed. We just won't have metrics for this connection.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Log and ignore seems reasonable to me

if nc, ok := c.(*negotiatingConn); ok {
return context.WithValue(ctx, connKey{}, nc)
}
log.Errorf("BUG: expected *websocket.negotiatingConn in context: got %T", c)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this say: "expected *websocket.negotiatingConn type of NetConn" or similar?

@@ -107,19 +107,44 @@ func New(security []sec.SecureTransport, muxers []StreamMuxer, psk ipnet.PSK, rc
func (u *upgrader) UpgradeListener(t transport.Transport, list manet.Listener) transport.Listener {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this just call return u.UpgradeGatedMaListener(t, u.GateMaListener(list))

}

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
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?

if !connType.IsKnown() {
fmt.Println("connType", connType)
Copy link
Collaborator

Choose a reason for hiding this comment

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

🔥

@@ -167,6 +148,7 @@ func (c *Conn) closeOnceFn() error {
time.Now().Add(GracefulCloseTimeout),
)
err2 := c.Conn.Close()
c.Scope.Done()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't the upgrader close the scope?

if err != nil {
return nil, err
}
gmal = upgrader.GateMaListener(mal)
Copy link
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.

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