@@ -224,12 +224,22 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
224
224
response . on ( 'data' , function ( chunk ) {
225
225
if ( req . method !== 'HEAD' && res . writable ) {
226
226
try {
227
- res . write ( chunk ) ;
227
+ var flushed = res . write ( chunk ) ;
228
228
}
229
229
catch ( ex ) {
230
230
console . error ( "res.write error: %s" , ex . message ) ;
231
+
231
232
try { res . end ( ) }
232
- catch ( ex ) { console . error ( "res.write error: %s" , ex . message ) }
233
+ catch ( ex ) { console . error ( "res.end error: %s" , ex . message ) }
234
+
235
+ return ;
236
+ }
237
+
238
+ if ( ! flushed ) {
239
+ response . pause ( ) ;
240
+ res . once ( 'drain' , function ( ) {
241
+ response . resume ( ) ;
242
+ } ) ;
233
243
}
234
244
}
235
245
} ) ;
@@ -265,7 +275,13 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
265
275
//
266
276
req . on ( 'data' , function ( chunk ) {
267
277
if ( ! errState ) {
268
- reverseProxy . write ( chunk ) ;
278
+ var flushed = reverseProxy . write ( chunk ) ;
279
+ if ( ! flushed ) {
280
+ req . pause ( ) ;
281
+ reverseProxy . once ( 'drain' , function ( ) {
282
+ req . resume ( ) ;
283
+ } ) ;
284
+ }
269
285
}
270
286
} ) ;
271
287
@@ -334,8 +350,14 @@ HttpProxy.prototype._forwardRequest = function (req) {
334
350
// the proxied request come in
335
351
//
336
352
req . on ( 'data' , function ( chunk ) {
337
- forwardProxy . write ( chunk ) ;
338
- } )
353
+ var flushed = forwardProxy . write ( chunk ) ;
354
+ if ( ! flushed ) {
355
+ req . pause ( ) ;
356
+ forwardProxy . once ( 'drain' , function ( ) {
357
+ req . resume ( ) ;
358
+ } ) ;
359
+ }
360
+ } ) ;
339
361
340
362
//
341
363
// At the end of the client request, we are going to
@@ -357,7 +379,7 @@ HttpProxy.prototype._forwardRequest = function (req) {
357
379
//
358
380
HttpProxy . prototype . proxyWebSocketRequest = function ( req , socket , head , buffer ) {
359
381
var self = this ,
360
- outgoing = new ( this . target . base ) ;
382
+ outgoing = new ( this . target . base ) ,
361
383
listeners = { } ,
362
384
errState = false ,
363
385
CRLF = '\r\n' ;
@@ -420,7 +442,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
420
442
if ( reverseProxy . incoming . socket . writable ) {
421
443
try {
422
444
self . emit ( 'websocket:outgoing' , req , socket , head , data ) ;
423
- reverseProxy . incoming . socket . write ( data ) ;
445
+ var flushed = reverseProxy . incoming . socket . write ( data ) ;
446
+ if ( ! flushed ) {
447
+ proxySocket . pause ( ) ;
448
+ reverseProxy . incoming . socket . once ( 'drain' , function ( ) {
449
+ proxySocket . resume ( ) ;
450
+ } ) ;
451
+ }
424
452
}
425
453
catch ( ex ) {
426
454
detach ( ) ;
@@ -437,7 +465,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
437
465
reverseProxy . incoming . socket . on ( 'data' , listeners . onOutgoing = function ( data ) {
438
466
try {
439
467
self . emit ( 'websocket:incoming' , reverseProxy , reverseProxy . incoming , head , data ) ;
440
- proxySocket . write ( data ) ;
468
+ var flushed = proxySocket . write ( data ) ;
469
+ if ( ! flushed ) {
470
+ reverseProxy . incoming . socket . pause ( ) ;
471
+ proxySocket . once ( 'drain' , function ( ) {
472
+ reverseProxy . incoming . socket . resume ( ) ;
473
+ } ) ;
474
+ }
441
475
}
442
476
catch ( ex ) {
443
477
detach ( ) ;
@@ -595,7 +629,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
595
629
//
596
630
self . emit ( 'websocket:handshake' , req , socket , head , sdata , data ) ;
597
631
socket . write ( sdata ) ;
598
- socket . write ( data ) ;
632
+ var flushed = socket . write ( data ) ;
633
+ if ( ! flushed ) {
634
+ reverseProxy . socket . pause ( ) ;
635
+ socket . once ( 'drain' , function ( ) {
636
+ reverseProxy . socket . resume ( ) ;
637
+ } ) ;
638
+ }
599
639
}
600
640
catch ( ex ) {
601
641
//
@@ -620,7 +660,9 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
620
660
621
661
try {
622
662
//
623
- // Attempt to write the upgrade-head to the reverseProxy request.
663
+ // Attempt to write the upgrade-head to the reverseProxy
664
+ // request. This is small, and there's only ever one of
665
+ // it; no need for pause/resume.
624
666
//
625
667
reverseProxy . write ( head ) ;
626
668
}
0 commit comments