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

Implement Connection::get_ref to retrieve a reference to the socket #196

Closed

Conversation

stormshield-fabs
Copy link

This lets users of Connection immutably access the socket they provided.

Copy link
Member

@jxs jxs left a comment

Choose a reason for hiding this comment

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

Hi! And thanks for this, can you give more context on why you need to AsyncRead and AsyncWrite on the internal connection?

@stormshield-fabs
Copy link
Author

Hello,
We don't need to use these traits on the internal connection, but we need to directly access attributes that cannot be put behind a synchronization primitive for performance reasons.

Now that I'm re-reading this, maybe the implementations of get_ref should be in different impl blocs without the trait bounds?

@jxs
Copy link
Member

jxs commented Nov 6, 2024

We don't need to use these traits on the internal connection, but we need to directly access attributes that cannot be put behind a synchronization primitive for performance reasons.

But how do you access those attributes if you are returning a T?
Can you disclose which attributes are?

@stormshield-fabs
Copy link
Author

The precise use case is for metrics: the AsyncRead and AsyncWrite methods on our socket type increment u64s stored in the structure.

let socket = Socket { metric: 0, ... };
let connection = yamux::Connection::new(socket, ...)
// calls to `poll_new_outbound`, `poll_next_inbound`...
let metric = connection.get_ref().unwrap().metric;

@jxs
Copy link
Member

jxs commented Nov 7, 2024

The precise use case is for metrics: the AsyncRead and AsyncWrite methods on our socket type increment u64s stored in the structure.

let socket = Socket { metric: 0, ... };
let connection = yamux::Connection::new(socket, ...)
// calls to `poll_new_outbound`, `poll_next_inbound`...
let metric = connection.get_ref().unwrap().metric;

thanks for elaborating, but if you are calling AsyncRead and AsyncWrite on the inner connection you are messing the internal behaviour right? Can you explain what metrics would you like to record? I think it's better to just do as we do in rust-libp2p and offer explicit API's for that

@stormshield-fabs
Copy link
Author

stormshield-fabs commented Nov 7, 2024

We never call AsyncRead nor AsyncWrite on the inner connection, but I see how the proposed patch allows this undesirable behavior.

The metrics are two simple counters for the total written and read bytes on the connection. Can you link me to the explicit APIs you mentioned in rust-libp2p?

@jxs
Copy link
Member

jxs commented Nov 7, 2024

The metrics are two simple counters for the total written and read bytes on the connection. Can you link me to the explicit APIs you mentioned in rust-libp2p?

rust-libp2p has its whole metrics crate, which we don't need in this case, we can just expose methods that return the total bytes read and written on Connection which you can then record metrics, wdyt?

@stormshield-fabs
Copy link
Author

Yes that would work.

Let's close this PR and I'll submit another with the changes you suggest (except if you'd prefer to take care of it!).

@jxs
Copy link
Member

jxs commented Nov 8, 2024

no no, go ahead!
Thanks 🚀

@jxs jxs closed this Nov 8, 2024
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.

2 participants