@@ -15,29 +15,41 @@ const port = 9001;
1515let openedClientConnections = 0 ;
1616let closedClientConnections = 0 ;
1717
18+ let listenSocket ;
19+
20+ const maxBackpressure = 50 * 1024 * 1024 ;
21+ const maxPayloadSize = 5 * 1024 * 1024 ;
22+ const largeBuffer = new ArrayBuffer ( maxPayloadSize ) ;
23+
1824function printStatistics ( ) {
1925 console . log ( 'Opened clients ' + openedClientConnections ) ;
2026 console . log ( 'Closed clients ' + closedClientConnections + '\n' ) ;
2127}
2228
23- let listenSocket ;
24-
2529/* 0 to 1-less than max */
2630function getRandomInt ( max ) {
2731 return Math . floor ( Math . random ( ) * Math . floor ( max ) ) ;
2832}
2933
34+ /* This gets called every time we either open a new connection,
35+ * or it immediately land in websockets/ws error handler */
36+ function accountForConnection ( ) {
37+ /* Open more connections */
38+ if ( ++ openedClientConnections < /*100*/ 1000 ) {
39+ establishNewConnection ( ) ;
40+ } else {
41+ /* Stop listening */
42+ uWS . us_listen_socket_close ( listenSocket ) ;
43+ }
44+ }
45+
3046function establishNewConnection ( ) {
3147 const ws = new WebSocket ( 'ws://localhost:' + port ) ;
3248
3349 ws . on ( 'open' , ( ) => {
34- /* Open more connections */
35- if ( ++ openedClientConnections < 100 ) {
36- establishNewConnection ( ) ;
37- } else {
38- /* Stop listening */
39- uWS . us_listen_socket_close ( listenSocket ) ;
40- }
50+ /* Mark this socket as opened */
51+ ws . _opened = true ;
52+ accountForConnection ( ) ;
4153
4254 printStatistics ( ) ;
4355 performRandomClientAction ( ws ) ;
@@ -48,21 +60,30 @@ function establishNewConnection() {
4860 performRandomClientAction ( ws ) ;
4961 } ) ;
5062
63+ /* Close can be called more times than open, websockets/ws is really messy! */
5164 ws . on ( 'close' , ( ) => {
65+ /* Check if this websocket really was opened first */
66+ if ( ! ws . _opened ) {
67+ accountForConnection ( ) ;
68+ }
69+
5270 closedClientConnections ++ ;
5371 printStatistics ( ) ;
72+ /* I guess there is no need for us to call anything
73+ * here, websockets/ws will not send anything to us anyways */
5474 //performRandomClientAction(ws);
5575 } ) ;
56- }
5776
58- const maxBackpressure = 50 * 1024 * 1024 ;
59- const maxPayloadSize = 5 * 1024 * 1024 ;
60- const largeBuffer = new ArrayBuffer ( maxPayloadSize ) ;
77+ ws . on ( 'error' , ( ) => {
78+ /* We simply ignore errors. websockets/ws will call our close handler
79+ * on errors, potentially before even calling the open handler */
80+ } ) ;
81+ }
6182
6283/* Perform random websockets/ws action */
6384function performRandomClientAction ( ws ) {
64- /* 0, 1 but never 2 */
65- let action = getRandomInt ( 2 ) ;
85+ /* 0, 1, 2 but never 3 */
86+ let action = getRandomInt ( 3 ) ;
6687
6788 /* Sending a message should have higher probability */
6889 if ( getRandomInt ( 100 ) < 80 ) {
@@ -85,7 +106,6 @@ function performRandomClientAction(ws) {
85106 break ;
86107 }
87108 case 2 : {
88- /* This should correspond to hard us_socket_close */
89109 ws . terminate ( ) ;
90110 break ;
91111 }
@@ -94,7 +114,8 @@ function performRandomClientAction(ws) {
94114
95115/* Perform random uWebSockets.js action */
96116function performRandomServerAction ( ws , uniform ) {
97- let action = getRandomInt ( 2 ) ;
117+ /* 0, 1, 2 but never 3 */
118+ let action = getRandomInt ( 3 ) ;
98119
99120 /* Sending a message should have higher probability,
100121 * except for when we are draining. */
@@ -104,7 +125,6 @@ function performRandomServerAction(ws, uniform) {
104125 }
105126 }
106127
107- /* 0, 1 but never 2 */
108128 switch ( action ) {
109129 case 0 : {
110130 ws . close ( ) ;
0 commit comments