Skip to content

Commit

Permalink
Force conn.Close() after MaxRuntime in handler.go (#362)
Browse files Browse the repository at this point in the history
* Force conn.Close() after MaxRuntime in handler.go

* Pass the context with timeout to upload.Do and download.Do

* Remove the reference to the timeout in the warning message

* Cancel context in a defer rather than waiting for timeout
  • Loading branch information
robertodauria authored Jul 27, 2022
1 parent 6aee5cc commit 4f67067
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions ndt7/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package handler

import (
"context"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -68,7 +69,17 @@ func (h Handler) runMeasurement(kind spec.SubtestKind, rw http.ResponseWriter, r
ndt7metrics.ClientConnections.WithLabelValues(string(kind), "websocket-error").Inc()
return
}
defer warnonerror.Close(conn, "runMeasurement: ignoring conn.Close result")
// Make sure that the connection is closed after (at most) MaxRuntime.
// Download and upload tests have their own timeouts, but we have observed
// that under particular network conditions the connection can remain open
// while the receiver goroutine is blocked on a read syscall, long after
// the client is gone. This is a workaround for that.
ctx, cancel := context.WithTimeout(req.Context(), spec.MaxRuntime)
defer cancel()
go func() {
<-ctx.Done()
warnonerror.Close(conn, "runMeasurement: ignoring conn.Close result")
}()
// Create measurement archival data.
data, err := getData(conn)
if err != nil {
Expand Down Expand Up @@ -96,11 +107,11 @@ func (h Handler) runMeasurement(kind spec.SubtestKind, rw http.ResponseWriter, r
var rate float64
if kind == spec.SubtestDownload {
result.Download = data
err = download.Do(req.Context(), conn, data)
err = download.Do(ctx, conn, data)
rate = downRate(data.ServerMeasurements)
} else if kind == spec.SubtestUpload {
result.Upload = data
err = upload.Do(req.Context(), conn, data)
err = upload.Do(ctx, conn, data)
rate = upRate(data.ServerMeasurements)
}

Expand Down

0 comments on commit 4f67067

Please sign in to comment.