Skip to content

Commit 73b438c

Browse files
committed
Add backpressure example
1 parent 1be6185 commit 73b438c

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

‎examples/Backpressure.js‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* A few users have reported having issues understanding backpressure and how to deal with it.
2+
*
3+
* Backpressure is the buildup of unacknowledged data; you can't just call ws.send without checking for backpressure.
4+
* Data doesn't just, poof, immediately jump from your server to the receiver - the receiver has to actually... receive it.
5+
* That happens with ACKs, controlling the transmission window.
6+
*
7+
* This applies to _EVERYTHING_ computer science (not just uWS); files, sockets, queues, and so on. If you're building
8+
* web services without taking backpressure into account you're not developing proper solutions - you're fucking around.
9+
*
10+
* Any slow receiver can wreck your whole server if you're not taking backpressure into account.
11+
*
12+
* The following is a (preposterous) example of how data can be pushed according to backpressure.
13+
* Do not take this as a way to actually write code, this is horrible, but it shows the concept clearly.
14+
*
15+
*/
16+
17+
/* Number between thumb and index finger */
18+
const backpressure = 1024;
19+
20+
/* Used for statistics */
21+
let messages = 0;
22+
let messageNumber = 0;
23+
24+
const uWS = require('../dist/uws.js');
25+
const port = 9001;
26+
27+
const app = uWS./*SSL*/App({
28+
key_file_name: 'misc/key.pem',
29+
cert_file_name: 'misc/cert.pem',
30+
passphrase: '1234'
31+
}).ws('/*', {
32+
/* Options */
33+
compression: 0,
34+
maxPayloadLength: 16 * 1024 * 1024,
35+
/* We need a slightly higher timeout for this crazy example */
36+
idleTimeout: 60,
37+
/* Handlers */
38+
open: (ws, req) => {
39+
console.log('A WebSocket connected via URL: ' + req.getUrl() + '! Pushing data now!');
40+
/* We begin our example by sending until we have backpressure */
41+
while (ws.getBufferedAmount() < backpressure) {
42+
ws.send("This is a message, let's call it " + messageNumber);
43+
messageNumber++;
44+
messages++;
45+
}
46+
},
47+
drain: (ws) => {
48+
/* Continue sending when we have drained (some) */
49+
while (ws.getBufferedAmount() < backpressure) {
50+
ws.send("This is a message, let's call it " + messageNumber);
51+
messageNumber++;
52+
messages++;
53+
}
54+
},
55+
close: (ws, code, message) => {
56+
console.log('WebSocket closed');
57+
}
58+
}).any('/*', (res, req) => {
59+
res.end('Nothing to see here!');
60+
}).listen(port, (token) => {
61+
if (token) {
62+
console.log('Listening to port ' + port);
63+
} else {
64+
console.log('Failed to listen to port ' + port);
65+
}
66+
});
67+
68+
/* Start a timer to check how fast we end up sending.
69+
* Not a benchmark, just statistics. */
70+
setInterval(() => {
71+
console.log("Sent " + messages + " messages last second");
72+
messages = 0;
73+
}, 1000);

0 commit comments

Comments
 (0)