From 0f7a51f4619ce2e7b9fb57bfa79200419d8fcab7 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 31 Aug 2021 14:08:23 -0700 Subject: [PATCH] fix: speedTest between peers keep the connection alive (#13120) for longer durations keep the speedTest alive instead of timing them out based on ResponseHeaderTimeout. --- cmd/peer-rest-client.go | 15 ++++++++++++--- cmd/peer-rest-server.go | 15 +++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/cmd/peer-rest-client.go b/cmd/peer-rest-client.go index 1dfac0a8d..ad4edadf6 100644 --- a/cmd/peer-rest-client.go +++ b/cmd/peer-rest-client.go @@ -1014,9 +1014,18 @@ func (client *peerRESTClient) Speedtest(ctx context.Context, size, concurrent in return SpeedtestResult{}, err } defer http.DrainBody(respBody) + waitReader, err := waitForHTTPResponse(respBody) + if err != nil { + return SpeedtestResult{}, err + } - dec := gob.NewDecoder(respBody) var result SpeedtestResult - err = dec.Decode(&result) - return result, err + err = gob.NewDecoder(waitReader).Decode(&result) + if err != nil { + return result, err + } + if result.Error != "" { + return result, errors.New(result.Error) + } + return result, nil } diff --git a/cmd/peer-rest-server.go b/cmd/peer-rest-server.go index 8f62fc885..ba6a7654f 100644 --- a/cmd/peer-rest-server.go +++ b/cmd/peer-rest-server.go @@ -1158,6 +1158,7 @@ func (s *peerRESTServer) GetPeerMetrics(w http.ResponseWriter, r *http.Request) type SpeedtestResult struct { Uploads uint64 Downloads uint64 + Error string } // SpeedtestObject implements "random-read" object reader @@ -1280,7 +1281,7 @@ func selfSpeedtest(ctx context.Context, size, concurrent int, duration time.Dura }(i) } wg.Wait() - return SpeedtestResult{objUploadCount, objDownloadCount}, nil + return SpeedtestResult{Uploads: objUploadCount, Downloads: objDownloadCount}, nil } func (s *peerRESTServer) SpeedtestHandler(w http.ResponseWriter, r *http.Request) { @@ -1313,17 +1314,15 @@ func (s *peerRESTServer) SpeedtestHandler(w http.ResponseWriter, r *http.Request duration = time.Second * 10 } + done := keepHTTPResponseAlive(w) + result, err := selfSpeedtest(r.Context(), size, concurrent, duration) if err != nil { - s.writeErrorResponse(w, err) - return + result.Error = err.Error() } - enc := gob.NewEncoder(w) - if err := enc.Encode(result); err != nil { - s.writeErrorResponse(w, errors.New("Encoding report failed: "+err.Error())) - return - } + done(nil) + logger.LogIf(r.Context(), gob.NewEncoder(w).Encode(result)) w.(http.Flusher).Flush() }