@@ -539,16 +539,23 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
539
539
response . on ( 'data' , function ( chunk ) {
540
540
if ( req . method !== 'HEAD' && res . writable ) {
541
541
try {
542
- res . write ( chunk ) ;
542
+ var flushed = res . write ( chunk ) ;
543
543
} catch ( er ) {
544
544
console . error ( "res.write error: %s" , er . message ) ;
545
545
try {
546
546
res . end ( ) ;
547
547
} catch ( er ) {
548
548
console . error ( "res.end error: %s" , er . message ) ;
549
549
}
550
+ return ;
550
551
}
551
552
}
553
+ if ( ! flushed ) {
554
+ response . pause ( ) ;
555
+ res . once ( 'drain' , function ( ) {
556
+ response . resume ( ) ;
557
+ } ) ;
558
+ }
552
559
} ) ;
553
560
554
561
// When the `reverseProxy` `response` ends, end the
@@ -578,7 +585,13 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
578
585
// `req` write it to the `reverseProxy` request.
579
586
req . on ( 'data' , function ( chunk ) {
580
587
if ( ! errState ) {
581
- reverseProxy . write ( chunk ) ;
588
+ var flushed = reverseProxy . write ( chunk ) ;
589
+ if ( ! flushed ) {
590
+ req . pause ( ) ;
591
+ reverseProxy . once ( 'drain' , function ( ) {
592
+ req . resume ( ) ;
593
+ } ) ;
594
+ }
582
595
}
583
596
} ) ;
584
597
@@ -646,7 +659,13 @@ HttpProxy.prototype._forwardRequest = function (req) {
646
659
647
660
// Chunk the client request body as chunks from the proxied request come in
648
661
req . on ( 'data' , function ( chunk ) {
649
- forwardProxy . write ( chunk ) ;
662
+ var flushed = forwardProxy . write ( chunk ) ;
663
+ if ( ! flushed ) {
664
+ req . pause ( ) ;
665
+ forwardProxy . once ( 'drain' , function ( ) {
666
+ req . resume ( ) ;
667
+ } ) ;
668
+ }
650
669
} )
651
670
652
671
// At the end of the client request, we are going to stop the proxied request
@@ -741,7 +760,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
741
760
if ( reverseProxy . incoming . socket . writable ) {
742
761
try {
743
762
self . emit ( 'websocket:outgoing' , req , socket , head , data ) ;
744
- reverseProxy . incoming . socket . write ( data ) ;
763
+ var flushed = reverseProxy . incoming . socket . write ( data ) ;
764
+ if ( ! flushed ) {
765
+ proxySocket . pause ( ) ;
766
+ reverseProxy . incoming . socket . once ( 'drain' , function ( ) {
767
+ proxySocket . resume ( ) ;
768
+ } ) ;
769
+ }
745
770
}
746
771
catch ( e ) {
747
772
detach ( ) ;
@@ -758,7 +783,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
758
783
reverseProxy . incoming . socket . on ( 'data' , listeners . onOutgoing = function ( data ) {
759
784
try {
760
785
self . emit ( 'websocket:incoming' , reverseProxy , reverseProxy . incoming , head , data ) ;
761
- proxySocket . write ( data ) ;
786
+ var flushed = proxySocket . write ( data ) ;
787
+ if ( ! flushed ) {
788
+ reverseProxy . incoming . socket . pause ( ) ;
789
+ proxySocket . once ( 'drain' , function ( ) {
790
+ reverseProxy . incoming . socket . resume ( ) ;
791
+ } ) ;
792
+ }
762
793
}
763
794
catch ( e ) {
764
795
detach ( ) ;
@@ -918,7 +949,14 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
918
949
//
919
950
self . emit ( 'websocket:handshake' , req , socket , head , sdata , data ) ;
920
951
socket . write ( sdata ) ;
921
- socket . write ( data ) ;
952
+ var flushed = socket . write ( data ) ;
953
+ if ( ! flushed ) {
954
+ reverseProxy . socket . pause ( ) ;
955
+ socket . once ( 'drain' , function ( ) {
956
+ reverseProxy . socket . resume ( ) ;
957
+ } ) ;
958
+ }
959
+
922
960
}
923
961
catch ( ex ) {
924
962
proxyError ( ex )
@@ -935,9 +973,9 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
935
973
reverseProxy . on ( 'error' , proxyError ) ;
936
974
937
975
try {
938
- //
939
976
// Attempt to write the upgrade-head to the reverseProxy request.
940
- //
977
+ // This is small, and there's only ever one of it.
978
+ // No need for pause/resume.
941
979
reverseProxy . write ( head ) ;
942
980
}
943
981
catch ( ex ) {
0 commit comments