forked from uNetworking/uWebSockets.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RateLimit.js
69 lines (62 loc) · 2.29 KB
/
RateLimit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* RateLimit example */
const uWS = require('../dist/uws.js');
const port = 9001;
const RateLimit = (limit, interval) => { let now = 0; const last = Symbol(), count = Symbol(); setInterval(() => ++now, interval); return ws => { if (ws[last] != now) { ws[last] = now; ws[count] = 1 } else { return ++ws[count] > limit } } }
const rateLimit = RateLimit(1, 10000) // limit is: 1 message per 10 seconds.
const rateLimit2 = RateLimit(10, 2000) // limit per interval (milliseconds)
const app = uWS./*SSL*/App({
key_file_name: 'misc/key.pem',
cert_file_name: 'misc/cert.pem',
passphrase: '1234'
}).ws('/*', {
/* Options */
compression: uWS.SHARED_COMPRESSOR,
maxPayloadLength: 16 * 1024 * 1024,
idleTimeout: 10,
/* Handlers */
open: (ws) => {
console.log('A WebSocket connected!');
},
message: async (ws, message, isBinary) => {
// rateLimit(ws) returns true if over limit
if (rateLimit(ws)) {
/*
Do as you wish, personally, I prefer to inform the client, do Log,
and close the connection if over limit happened.
or you can just ignore its message and just return.
*/
const dataToSendToClient = {
action: 'info',
data: {
message: {
english: 'over limit! please slow down.',
farsi: 'یواشتر'
}
}
}
await ws.send(JSON.stringify(dataToSendToClient));
console.warn('rate limit hit!');
return ws.end();
}
//or maybe different type of message or /endpoint needs different limit
if (ws.type2 && rateLimit2(ws)) {
console.warn('type2 is over limit');
return ws.end();
}
// rest of message code goes here ...
},
drain: (ws) => {
console.log('WebSocket backpressure: ' + ws.getBufferedAmount());
},
close: (ws, code, message) => {
console.log('WebSocket closed');
}
}).any('/*', (res, req) => {
res.end('Nothing to see here!');
}).listen(port, (token) => {
if (token) {
console.log('Listening to port ' + port);
} else {
console.log('Failed to listen to port ' + port);
}
});