@@ -494,8 +494,17 @@ func (pr *Proxy) PassThroughToClient(conn *ConnWrapper, stack *Stack) *gerr.Gate
494494 received , response , err := pr .receiveTrafficFromServer (client )
495495 span .AddEvent ("Received traffic from server" )
496496
497- // If the response is empty, don't send anything, instead just close the ingress connection.
498- if received == 0 || err != nil {
497+ // If there is no data to send to the client,
498+ // we don't need to run the hooks and
499+ // we obviously have no data to send to the client.
500+ if received == 0 {
501+ span .AddEvent ("No data to send to client" )
502+ stack .PopLastRequest ()
503+ return nil
504+ }
505+
506+ // If there is an error, close the ingress connection.
507+ if err != nil {
499508 fields := map [string ]interface {}{"function" : "proxy.passthrough" }
500509 if client .LocalAddr () != "" {
501510 fields ["localAddr" ] = client .LocalAddr ()
@@ -517,7 +526,7 @@ func (pr *Proxy) PassThroughToClient(conn *ConnWrapper, stack *Stack) *gerr.Gate
517526
518527 // Get the last request from the stack.
519528 lastRequest := stack .PopLastRequest ()
520- request := make ( []byte , 0 )
529+ request := []byte {}
521530 if lastRequest != nil {
522531 request = lastRequest .Data
523532 }
@@ -698,7 +707,7 @@ func (pr *Proxy) receiveTrafficFromClient(conn net.Conn) ([]byte, *gerr.GatewayD
698707 defer span .End ()
699708
700709 // request contains the data from the client.
701- received := 0
710+ total := 0
702711 buffer := bytes .NewBuffer (nil )
703712 for {
704713 chunk := make ([]byte , pr .ClientConfig .ReceiveChunkSize )
@@ -713,10 +722,10 @@ func (pr *Proxy) receiveTrafficFromClient(conn net.Conn) ([]byte, *gerr.GatewayD
713722 return chunk [:read ], gerr .ErrReadFailed .Wrap (err )
714723 }
715724
716- received += read
725+ total += read
717726 buffer .Write (chunk [:read ])
718727
719- if received == 0 || received < pr .ClientConfig .ReceiveChunkSize {
728+ if read < pr .ClientConfig .ReceiveChunkSize {
720729 break
721730 }
722731
@@ -725,19 +734,18 @@ func (pr *Proxy) receiveTrafficFromClient(conn net.Conn) ([]byte, *gerr.GatewayD
725734 }
726735 }
727736
728- length := len (buffer .Bytes ())
729737 pr .Logger .Debug ().Fields (
730738 map [string ]interface {}{
731- "length" : length ,
739+ "length" : total ,
732740 "local" : LocalAddr (conn ),
733741 "remote" : RemoteAddr (conn ),
734742 },
735743 ).Msg ("Received data from client" )
736744
737745 span .AddEvent ("Received data from client" )
738746
739- metrics .BytesReceivedFromClient .Observe (float64 (length ))
740- metrics .TotalTrafficBytes .Observe (float64 (length ))
747+ metrics .BytesReceivedFromClient .Observe (float64 (total ))
748+ metrics .TotalTrafficBytes .Observe (float64 (total ))
741749
742750 return buffer .Bytes (), nil
743751}
0 commit comments