Skip to content

Commit

Permalink
Merge pull request #25 from makotom/20240526-safety
Browse files Browse the repository at this point in the history
Safety
  • Loading branch information
makotom authored May 26, 2024
2 parents 66ebf51 + 9c98f8e commit f7ec890
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 9 deletions.
11 changes: 6 additions & 5 deletions cfspeed/measurement.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ const (
downURLTemplate = "https://speed.cloudflare.com/__down?bytes=%d"
upURLTemplate = "https://speed.cloudflare.com/__up"

rttMeasurementDuration = 2 * time.Second // Measurement resumes by sending another ping until exceeding this time duration
speedMeasurementDuration = 10 * time.Second // Download / Upload continues until exceeding this time duration
downloadSizeMax = 512 * 1024 * 1024 // Maximum size of data to be downloaded; 512 MiB
uploadSizeMax = 512 * 1024 * 1024 // Maximum size of data to be uploaded; 512 MiB
rttMeasurementDurationMax = 2 * time.Second // Maximum duration of RTT measurement
rttMeasurementMax = 20 // Maximum number of pings to be made for RTT measurement
speedMeasurementDuration = 10 * time.Second // Download / Upload continues until exceeding this time duration
downloadSizeMax = 512 * 1024 * 1024 // Maximum size of data to be downloaded; 512 MiB
uploadSizeMax = 512 * 1024 * 1024 // Maximum size of data to be uploaded; 512 MiB
)

type MeasurementMetadata struct {
Expand Down Expand Up @@ -234,7 +235,7 @@ func MeasureRTT() (*Stats, *Stats, error) {
durations := []time.Duration{}
cfReqDurs := []time.Duration{}

for measureUntil := time.Now().Add(rttMeasurementDuration); time.Since(measureUntil) < 0; {
for measureUntil := time.Now().Add(rttMeasurementDurationMax); time.Since(measureUntil) < 0 && len(durations) < rttMeasurementMax; {
measurement, err := doUplinkMeasurement(0, time.Now())
if err != nil {
return nil, nil, err
Expand Down
89 changes: 85 additions & 4 deletions cfspeed/run-print.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

const (
defaultDialTimeout = 10 * time.Second
defaultRunTimeout = 30 * time.Second
)

func printMetadata(printer *log.Logger, metadata *MeasurementMetadata) {
Expand Down Expand Up @@ -70,6 +71,26 @@ func runAndPrintMeasurementMetadata(printer *log.Logger) error {
return nil
}

func runAndPrintMeasurementMetadataWithTimeout(printer *log.Logger, timeout time.Duration) error {
var err error = nil
completed := make(chan bool)

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

go func() {
err = runAndPrintMeasurementMetadata(printer)
completed <- true
}()

select {
case <-completed:
return err
case <-ctx.Done():
return ctx.Err()
}
}

func runAndPrintUnloadedRTTMeasurement(printer *log.Logger) error {
rttStats, _, err := MeasureRTT()

Expand All @@ -82,6 +103,26 @@ func runAndPrintUnloadedRTTMeasurement(printer *log.Logger) error {
return nil
}

func runAndPrintUnloadedRTTMeasurementWithTimeout(printer *log.Logger, timeout time.Duration) error {
var err error = nil
completed := make(chan bool)

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

go func() {
err = runAndPrintUnloadedRTTMeasurement(printer)
completed <- true
}()

select {
case <-completed:
return err
case <-ctx.Done():
return ctx.Err()
}
}

func runAndPrintDownlinkMeasurement(printer *log.Logger, multiplicity int, measureLoadedRTT bool) error {
var dlStats *SpeedMeasurementStats
var dlLoadedRTTStats *Stats
Expand Down Expand Up @@ -117,6 +158,26 @@ func runAndPrintDownlinkMeasurement(printer *log.Logger, multiplicity int, measu
return nil
}

func runAndPrintDownlinkMeasurementWithTimeout(printer *log.Logger, multiplicity int, measureLoadedRTT bool, timeout time.Duration) error {
var err error = nil
completed := make(chan bool)

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

go func() {
err = runAndPrintDownlinkMeasurement(printer, multiplicity, measureLoadedRTT)
completed <- true
}()

select {
case <-completed:
return err
case <-ctx.Done():
return ctx.Err()
}
}

func runAndPrintUplinkMeasurement(printer *log.Logger, multiplicity int, measureLoadedRTT bool) error {
var ulStats *SpeedMeasurementStats
var ulLoadedRTTStats *Stats
Expand Down Expand Up @@ -152,6 +213,26 @@ func runAndPrintUplinkMeasurement(printer *log.Logger, multiplicity int, measure
return nil
}

func runAndPrintUplinkMeasurementWithTimeout(printer *log.Logger, multiplicity int, measureLoadedRTT bool, timeout time.Duration) error {
var err error = nil
completed := make(chan bool)

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

go func() {
err = runAndPrintUplinkMeasurement(printer, multiplicity, measureLoadedRTT)
completed <- true
}()

select {
case <-completed:
return err
case <-ctx.Done():
return ctx.Err()
}
}

func SetTransportProtocol(protocol string, dialTimeout time.Duration) {
// cf. https://go.googlesource.com/go/+/refs/tags/go1.22.1/src/net/http/transport.go#43
// cf. https://go.googlesource.com/go/+/refs/tags/go1.22.1/src/net/http/transport.go#140
Expand All @@ -174,24 +255,24 @@ func SetTransportProtocol(protocol string, dialTimeout time.Duration) {
func RunAndPrint(printer *log.Logger, transportProtocol string, multiplicity int, measureRTT bool) error {
SetTransportProtocol(transportProtocol, defaultDialTimeout)

if err := runAndPrintMeasurementMetadata(printer); err != nil {
if err := runAndPrintMeasurementMetadataWithTimeout(printer, defaultRunTimeout); err != nil {
return err
}
printer.Println()

if measureRTT {
if err := runAndPrintUnloadedRTTMeasurement(printer); err != nil {
if err := runAndPrintUnloadedRTTMeasurementWithTimeout(printer, defaultRunTimeout); err != nil {
return err
}
printer.Println()
}

if err := runAndPrintDownlinkMeasurement(printer, multiplicity, measureRTT); err != nil {
if err := runAndPrintDownlinkMeasurementWithTimeout(printer, multiplicity, measureRTT, defaultRunTimeout); err != nil {
return err
}
printer.Println()

if err := runAndPrintUplinkMeasurement(printer, multiplicity, measureRTT); err != nil {
if err := runAndPrintUplinkMeasurementWithTimeout(printer, multiplicity, measureRTT, defaultRunTimeout); err != nil {
return err
}

Expand Down

0 comments on commit f7ec890

Please sign in to comment.