@@ -55,11 +55,11 @@ func (c *DefaultDialerClient) OpenStream(ctx context.Context, url string, body i
5555 },
5656 })
5757
58- method := "GET"
58+ method := "GET" // stream-down
5959 if body != nil {
60- method = "POST"
60+ method = "POST" // stream-up/one
6161 }
62- req , _ := http .NewRequestWithContext (ctx , method , url , body )
62+ req , _ := http .NewRequestWithContext (context . WithoutCancel ( ctx ) , method , url , body )
6363 req .Header = c .transportConfig .GetRequestHeader ()
6464 if method == "POST" && ! c .transportConfig .NoGRPCHeader {
6565 req .Header .Set ("Content-Type" , "application/grpc" )
@@ -69,17 +69,20 @@ func (c *DefaultDialerClient) OpenStream(ctx context.Context, url string, body i
6969 go func () {
7070 resp , err := c .client .Do (req )
7171 if err != nil {
72+ if ! uploadOnly {
73+ c .closed = true
74+ }
7275 errors .LogInfoInner (ctx , err , "failed to " + method + " " + url )
7376 gotConn .Close ()
7477 wrc .Close ()
7578 return
7679 }
7780 if resp .StatusCode != 200 && ! uploadOnly {
78- // c.closed = true
7981 errors .LogInfo (ctx , "unexpected status " , resp .StatusCode )
8082 }
81- if resp .StatusCode != 200 || uploadOnly {
82- resp .Body .Close ()
83+ if resp .StatusCode != 200 || uploadOnly { // stream-up
84+ io .Copy (io .Discard , resp .Body )
85+ resp .Body .Close () // if it is called immediately, the upload will be interrupted also
8386 wrc .Close ()
8487 return
8588 }
@@ -91,7 +94,7 @@ func (c *DefaultDialerClient) OpenStream(ctx context.Context, url string, body i
9194}
9295
9396func (c * DefaultDialerClient ) PostPacket (ctx context.Context , url string , body io.Reader , contentLength int64 ) error {
94- req , err := http .NewRequestWithContext (ctx , "POST" , url , body )
97+ req , err := http .NewRequestWithContext (context . WithoutCancel ( ctx ) , "POST" , url , body )
9598 if err != nil {
9699 return err
97100 }
@@ -101,13 +104,14 @@ func (c *DefaultDialerClient) PostPacket(ctx context.Context, url string, body i
101104 if c .httpVersion != "1.1" {
102105 resp , err := c .client .Do (req )
103106 if err != nil {
107+ c .closed = true
104108 return err
105109 }
106110
111+ io .Copy (io .Discard , resp .Body )
107112 defer resp .Body .Close ()
108113
109114 if resp .StatusCode != 200 {
110- // c.closed = true
111115 return errors .New ("bad status code:" , resp .Status )
112116 }
113117 } else {
@@ -139,11 +143,12 @@ func (c *DefaultDialerClient) PostPacket(ctx context.Context, url string, body i
139143 if h1UploadConn .UnreadedResponsesCount > 0 {
140144 resp , err := http .ReadResponse (h1UploadConn .RespBufReader , req )
141145 if err != nil {
146+ c .closed = true
142147 return fmt .Errorf ("error while reading response: %s" , err .Error ())
143148 }
149+ io .Copy (io .Discard , resp .Body )
150+ defer resp .Body .Close ()
144151 if resp .StatusCode != 200 {
145- // c.closed = true
146- // resp.Body.Close() // I'm not sure
147152 return fmt .Errorf ("got non-200 error response code: %d" , resp .StatusCode )
148153 }
149154 }
0 commit comments