@@ -15,6 +15,11 @@ const port = 9001;
1515let openedClientConnections = 0 ;
1616let closedClientConnections = 0 ;
1717
18+ function printStatistics ( ) {
19+ console . log ( 'Opened clients ' + openedClientConnections ) ;
20+ console . log ( 'Closed clients ' + closedClientConnections + '\n' ) ;
21+ }
22+
1823let listenSocket ;
1924
2025/* 0 to 1-less than max */
@@ -27,14 +32,14 @@ function establishNewConnection() {
2732
2833 ws . on ( 'open' , ( ) => {
2934 /* Open more connections */
30- if ( ++ openedClientConnections < 1000 ) {
35+ if ( ++ openedClientConnections < 100 ) {
3136 establishNewConnection ( ) ;
3237 } else {
3338 /* Stop listening */
3439 uWS . us_listen_socket_close ( listenSocket ) ;
3540 }
3641
37- console . log ( 'Opened ' + openedClientConnections + ' client connections so far.' ) ;
42+ printStatistics ( ) ;
3843 performRandomClientAction ( ws ) ;
3944 } ) ;
4045
@@ -44,15 +49,27 @@ function establishNewConnection() {
4449 } ) ;
4550
4651 ws . on ( 'close' , ( ) => {
47- console . log ( "client was closed" ) ;
52+ closedClientConnections ++ ;
53+ printStatistics ( ) ;
4854 //performRandomClientAction(ws);
4955 } ) ;
5056}
5157
58+ const maxBackpressure = 50 * 1024 * 1024 ;
59+ const maxPayloadSize = 5 * 1024 * 1024 ;
60+ const largeBuffer = new ArrayBuffer ( maxPayloadSize ) ;
61+
5262/* Perform random websockets/ws action */
5363function performRandomClientAction ( ws ) {
5464 /* 0, 1 but never 2 */
55- switch ( getRandomInt ( 2 ) ) {
65+ let action = getRandomInt ( 2 ) ;
66+
67+ /* Sending a message should have higher probability */
68+ if ( getRandomInt ( 100 ) < 80 ) {
69+ action = 1 ;
70+ }
71+
72+ switch ( action ) {
5673 case 0 : {
5774 ws . close ( ) ;
5875 break ;
@@ -76,16 +93,32 @@ function performRandomClientAction(ws) {
7693}
7794
7895/* Perform random uWebSockets.js action */
79- function performRandomServerAction ( ws ) {
96+ function performRandomServerAction ( ws , uniform ) {
97+ let action = getRandomInt ( 2 ) ;
98+
99+ /* Sending a message should have higher probability,
100+ * except for when we are draining. */
101+ if ( ! uniform ) {
102+ if ( getRandomInt ( 100 ) < 80 ) {
103+ action = 1 ;
104+ }
105+ }
106+
80107 /* 0, 1 but never 2 */
81- switch ( getRandomInt ( 2 ) ) {
108+ switch ( action ) {
82109 case 0 : {
83110 ws . close ( ) ;
84111 break ;
85112 }
86113 case 1 : {
87- /* Length should be random from small to huge */
88- ws . send ( 'a test message' , false ) ;
114+ /* If we have very high backpressure we skip sending more */
115+ if ( ws . getBufferedAmount ( ) > maxBackpressure ) {
116+ /* Do something else instead */
117+ performRandomServerAction ( ws ) ;
118+ } else {
119+ /* Length should be random from small to huge, ArrayBuffer.slice is a copy (bad but we don't care I guess) */
120+ ws . send ( largeBuffer . slice ( 0 , getRandomInt ( maxPayloadSize + 1 ) ) ) ;
121+ }
89122 break ;
90123 }
91124 case 2 : {
@@ -102,23 +135,32 @@ const app = uWS./*SSL*/App({
102135 passphrase : '1234'
103136} ) . ws ( '/*' , {
104137 compression : 0 ,
105- maxPayloadLength : 16 * 1024 * 1024 ,
106- idleTimeout : 10 ,
138+ /* We intentionally accept less than what might be sent so to test force closing */
139+ maxPayloadLength : maxPayloadSize / 2 ,
140+ idleTimeout : 100 ,
107141
108142 open : ( ws , req ) => {
109143 /* Doing a terminate here will be interesting */
110- performRandomServerAction ( ws ) ;
144+ performRandomServerAction ( ws , false ) ;
111145 } ,
112146 message : ( ws , message , isBinary ) => {
113- performRandomServerAction ( ws ) ;
147+ performRandomServerAction ( ws , false ) ;
114148 } ,
115149 drain : ( ws ) => {
116- // todo: dont send over a certain backpressure
117- //performRandomServerAction(ws);
150+ /* We only perform actions while draining rarely (5%), and we do it uniformly.
151+ * There's no real NEED to do anything here, as it will eventually result in
152+ * message event for client. */
153+ if ( getRandomInt ( 100 ) < 5 ) {
154+ performRandomServerAction ( ws , true ) ;
155+ }
118156 } ,
119157 close : ( ws , code , message ) => {
158+ /* TODO: We SHOULD TECHNICALLY allow sending here.
159+ * It should not throw, but just silently ignore it
160+ * as it makes no sense to send on a closed socket
161+ * while still being technically valid */
120162 try {
121- performRandomServerAction ( ws ) ;
163+ performRandomServerAction ( ws , false ) ;
122164 } catch ( e ) {
123165 /* We expect to land here always, since socket is invalid */
124166 return ;
@@ -143,5 +185,6 @@ const app = uWS./*SSL*/App({
143185
144186/* Yes we do this crazy thing here */
145187process . on ( 'beforeExit' , ( ) => {
188+ uWS . print ( 'Exiting now' ) ;
146189 app . forcefully_free ( ) ;
147190} ) ;
0 commit comments