From ca37153f3a6685c6907e38c91e9f30280b1e5c72 Mon Sep 17 00:00:00 2001 From: Elad Nachmias Date: Fri, 25 Jan 2019 11:31:08 +0700 Subject: [PATCH] cmd/swarm/swarm-smoke: fix error handling --- cmd/swarm/swarm-smoke/sliding_window.go | 46 +++++++++++++++++-------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/cmd/swarm/swarm-smoke/sliding_window.go b/cmd/swarm/swarm-smoke/sliding_window.go index 41eedad003..7cf17685a2 100644 --- a/cmd/swarm/swarm-smoke/sliding_window.go +++ b/cmd/swarm/swarm-smoke/sliding_window.go @@ -20,6 +20,7 @@ import ( "bytes" "fmt" "math/rand" + "sync" "time" "github.com/ethereum/go-ethereum/log" @@ -58,7 +59,6 @@ func slidingWindow(c *cli.Context) error { uploadedBytes := 0 networkDepth := 0 errored := false - outer: for { seed := int(time.Now().UnixNano() / 1e6) @@ -84,23 +84,39 @@ outer: hashes = append(hashes, uploadResult{hash: hash, digest: fhash}) time.Sleep(time.Duration(syncDelay) * time.Second) uploadedBytes += filesize * 1000 - + var wg sync.WaitGroup for i, v := range hashes { - rand.Seed(time.Now().UTC().UnixNano()) - randIndex := 1 + rand.Intn(len(endpoints)-1) - ruid := uuid.New()[:8] - start := time.Now() - err := fetch(v.hash, endpoints[randIndex], v.digest, ruid) - fetchTime := time.Since(start) - if err != nil { - errored = true - log.Error("error retrieving hash", "hash idx", i, "err", err) - metrics.GetOrRegisterCounter("sliding-window.single.error", nil).Inc(1) - networkDepth = i + timeout := time.After(30 * time.Second) + errored = false + wg.Add(1) + go func(i int, v uploadResult) { + defer wg.Done() + for { + select { + case <-timeout: + errored = true + log.Error("error retrieving hash. timeout", "hash idx", i, "err", err) + metrics.GetOrRegisterCounter("sliding-window.single.error", nil).Inc(1) + return + default: + } + rand.Seed(time.Now().UTC().UnixNano()) + randIndex := 1 + rand.Intn(len(endpoints)-1) + ruid := uuid.New()[:8] + start := time.Now() + err := fetch(v.hash, endpoints[randIndex], v.digest, ruid) + fetchTime := time.Since(start) + if err != nil { + continue + } + metrics.GetOrRegisterMeter("sliding-window.single.fetch-time", nil).Mark(int64(fetchTime)) + } + }(i, v) + wg.Wait() + if errored { break outer } - - metrics.GetOrRegisterMeter("sliding-window.single.fetch-time", nil).Mark(int64(fetchTime)) + networkDepth = i } }