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

Add callbacks to log when Ping/Pong is received #246

Open
adrianlungu opened this issue May 28, 2020 · 8 comments
Open

Add callbacks to log when Ping/Pong is received #246

adrianlungu opened this issue May 28, 2020 · 8 comments
Milestone

Comments

@adrianlungu
Copy link

Hello,

From what I've seen, the pong is automatically handled internally in the library, however, in some specific cases it could be useful to run a function when the pong comes back (i.e. measuring latency, logging, etc.)

Think it would be ok to add a handler function that is called when the pong is handled, which can be set through an exposed function ?

@nhooyr
Copy link
Contributor

nhooyr commented May 28, 2020

Hi!

So this was intentional to avoid a callback based API. In order to measure latency, you just record the time when you call Ping and then when it returns and the difference is the ping pong. For logging you just log after it returns as well.

I’ll be adding an example soon to make using Ping less confusing. See #227

@nhooyr nhooyr added the docs label May 28, 2020
@adrianlungu
Copy link
Author

Hello!

Thanks for the quick answer!

So basically the ping-pong is done synchronously ?

But then, how can I log the pong that is done towards the server when the library receives a ping and handles it through handleControl internally ?

@nhooyr
Copy link
Contributor

nhooyr commented May 28, 2020

So basically the ping-pong is done synchronously ?

Yes, see 1695216#diff-7101a3ea121adf8d5b0acb5a2c520ca3R138-R162

But then, how can I log the pong that is done towards the server when the library receives a ping and handles it through handleControl internally ?

You can't. Is there a reason you need to do that?

@adrianlungu
Copy link
Author

adrianlungu commented May 28, 2020

Yes, see 1695216#diff-7101a3ea121adf8d5b0acb5a2c520ca3R138-R162

Thanks!

You can't. Is there a reason you need to do that?

Apart from logging,

I've been trying to debug various scenarios in which I'm using web sockets, as well as stress testing, and it would be helpful to catch the information.

I also have a scenario in which there's more information present in the ping-pong process and, while I know it's not idiomatic, being able to log that could be useful.

@nhooyr
Copy link
Contributor

nhooyr commented Jun 1, 2020

I also have a scenario in which there's more information present in the ping-pong process and, while I know it's not idiomatic, being able to log that could be useful.

That's a fair use case I didn't consider. Unfortunately I can't think of any good API to expose it. Using callbacks would be unfortunate and the stdlib http2 library also doesn't expose this sort of API either, instead favouring for a blocking Ping like us.

For now, you can use gohack and add a log into the library itself in handleControl.

@adrianlungu
Copy link
Author

Gohack sounds interesting, I'll look into that, thanks!

@nhooyr nhooyr added this to the v1.8.7 milestone Jul 5, 2020
@nhooyr
Copy link
Contributor

nhooyr commented Jul 5, 2020

Ok after considering it I think I'd be ok with adding two functions into the option structs that allow you to log when a ping/pong is received. I think for instrumentation and debugging purposes it's justified but the blocking Ping method will still be the only way to send a ping.

@nhooyr nhooyr modified the milestones: v1.8.7, v1.9.0 Jul 5, 2020
@nhooyr nhooyr added enhancement and removed docs labels Jul 5, 2020
@nhooyr nhooyr changed the title Pong Handler Func Add callbacks to log when Ping/Pong is received Jul 5, 2020
@markdingo
Copy link

markdingo commented Jun 27, 2021

So basically the ping-pong is done synchronously ?

Yes, see 1695216#diff-7101a3ea121adf8d5b0acb5a2c520ca3R138-R162

But then, how can I log the pong that is done towards the server when the library receives a ping and handles it through handleControl internally ?

You can't. Is there a reason you need to do that?

I have a slightly different use case. I want the server to close dead client connections. If I know my clients are pinging every 'n' seconds, then if I don't see a ping in, say, 'n' * 2 then I can safely close the conn.

Why do I want to do this? Because I have 1,000s or 100s of 1,000s of clients that are on flakey networks so I need to manage server resources by releasing dead sockets as quickly as possible. I know ping is designed to check for server responsiveness, but it can also be used to determine client liveness. That's my goal - to ask the question, is the client still alive.

So any mechanism that lets the server know it is getting a ping works for me. I don't care much what it is, but some way of knowing that the client has pinged (and thus whether a client hasn't pinged) is valuable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants