sdroege/async-tungstenite

Inspecting the steam buffer

Closed this issue · 3 comments

I'm making a client-side connection that will almost certainly receive messages faster than it can process them and so I was looking to inspect the buffered set of messages, or perhaps quickly push the messages into a buffer of my own.

I managed to find size_hint but I suspect async-tungstenite (or perhaps one of the layers below) is using the default implementation, as the method keeps returning (0, None) which is what the default implementation would do.

Any suggestions on how to get some control over the buffering? In my situation, it's acceptable to close the connection or send a "please stop" message to the server, but that still requires a trigger.

There's not really any buffering except for the currently read, incomplete message (and any OS-side buffering inside the socket). If you need buffering then this has to be implemented on top of the Stream.

Note also that size_hint() is meant for the size of the whole stream until it is finished, not just what is buffered right now.

Mm, so you're saying if I open a socket and the other end starts pushing lots of messages, tungstenite does not grab them from the OSs buffer unless you read them?

How would I rapidly read all messages waiting on the OS side to count how many I've received?

Thanks for the quick reply Sebastian!

Have a separate task that reads them all in a loop and stores them inside some kind of buffer, like a VecDeque or whatever.