nhooyr / websocket

Minimal and idiomatic WebSocket library for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add callbacks to log when Ping/Pong is received

adrianlungu opened this issue · comments

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 ?

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

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 ?

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?

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.

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.

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

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.

commented

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.