uNetworking / uWebSockets

Simple, secure & standards compliant web server for the most demanding of applications
Apache License 2.0
17.24k stars 1.75k forks source link

getBufferedAmount always returns 0 #1167

Closed ambrop72 closed 3 years ago

ambrop72 commented 3 years ago

Hi,

According to the documentation WebSocket::getBufferedAmount() returns the amount of buffered data to be sent. However, what I am seeing in my application is that it always returns 0. In my test case I am sending data faster than the client can receive/process it. Before each WebSocket::send(), I check getBufferedAmount() with the intent of not sending at that time if too much data is buffered. However getBufferedAmount() is always zero and at some point send() just returns false, despite getBufferedAmount() having returned 0 just prior

The documentation even suggests to check getBufferedAmount to detect backpressure, clearly this is not working for me.

ghost commented 3 years ago

https://github.com/uNetworking/uWebSockets.js/blob/master/examples/Backpressure.js

getBufferedAmount returns user space backpressure. You also have kernel space backpressure which is not counted. So it fills up seemingly non-linearly.

ambrop72 commented 3 years ago

So why does WebSocket::send() fail when the kernel backpressure fills up, shouldn't it put data in the user space buffer whose size is reported by getBufferedAmount()?

ghost commented 3 years ago

I don't know what you mean by "fail". You either succeed to write the data to kernel space, or it buffers up in user space. You need maxBackpressure set to something higher than 0. Did you check the example I linked? It demonstrates exactly this.

ambrop72 commented 3 years ago

What I am seeing is that send() returns false. I do not know what this means. Was the message placed in the user-space buffer or discarded?