@@ -91,6 +91,18 @@ type ClientConfig struct {
91
91
ForceHttp2 bool
92
92
}
93
93
94
+ type RelayServerError struct {
95
+ msg string
96
+ }
97
+
98
+ func NewRelayServerError (msg string ) error {
99
+ return & RelayServerError {msg }
100
+ }
101
+
102
+ func (e * RelayServerError ) Error () string {
103
+ return e .msg
104
+ }
105
+
94
106
func DefaultClientConfig () ClientConfig {
95
107
return ClientConfig {
96
108
RemoteRequestTimeout : 60 * time .Second ,
@@ -384,9 +396,9 @@ func (c *Client) postResponse(remote *http.Client, br *pb.HttpResponse) error {
384
396
return fmt .Errorf ("couldn't read relay server's response body: %v" , err )
385
397
}
386
398
if resp .StatusCode != http .StatusOK {
387
- err := fmt .Errorf ("relay server responded %s: %s" , http .StatusText (resp .StatusCode ), body )
399
+ err := NewRelayServerError ( fmt .Sprintf ("relay server responded %s: %s" , http .StatusText (resp .StatusCode ), body ) )
388
400
if resp .StatusCode == http .StatusBadRequest {
389
- // http-relay-server may have restarted during the request.
401
+ // http-relay-server may have restarted or the client cancelled the request.
390
402
return backoff .Permanent (err )
391
403
}
392
404
return err
@@ -564,6 +576,7 @@ func (c *Client) handleRequest(remote *http.Client, local *http.Client, pbreq *p
564
576
defer span .End ()
565
577
566
578
resp , hresp , err := makeBackendRequest (ctx , local , req , id )
579
+ defer hresp .Body .Close ()
567
580
if err != nil {
568
581
// Even if we couldn't handle the backend request, send an
569
582
// answer to the relay that signals the error.
@@ -643,8 +656,11 @@ func (c *Client) handleRequest(remote *http.Client, local *http.Client, pbreq *p
643
656
log .Printf ("[%s] Failed to post response to relay: %v" , * resp .Id , err )
644
657
},
645
658
)
646
- if _ , ok := err .(* backoff.PermanentError ); ok {
647
- // A permanent error suggests the request should be aborted.
659
+ // Any error suggests the request should be aborted.
660
+ // A missing chunk will cause clients to receive corrupted data, in most cases it is better
661
+ // to close the connection to avoid that.
662
+ if err != nil {
663
+ log .Printf ("[%s] Closing backend connection: %v" , * resp .Id , err )
648
664
break
649
665
}
650
666
}
0 commit comments