Skip to content
Snippets Groups Projects
Commit 2951823e authored by Alex Tjaarda's avatar Alex Tjaarda
Browse files

Fix: Correct time-based delay calculation in metric test loop

- Replaced incorrect float-based timing logic with proper use of Go's time package.
- Previously, delay was calculated using math.Pow() on raw float values, causing unintended near-zero delays.
- Now uses time.Duration arithmetic with jitter and random factor to generate appropriate sleep intervals.
- Also fixed logging statement with incorrect parameter count.
parent ace4b5a9
No related branches found
No related tags found
No related merge requests found
Pipeline #73633 failed
package collector
import (
"math"
"math/rand"
"net/http"
"time"
......@@ -44,11 +43,12 @@ func (metric *Metric) RunPeriodicTests() {
for {
for i := 1; i < 4; i++ {
maxDelay := float64(metric.TestTimeInterval) * metric.SpreadFactor
minDelay := 0.1 // minimum base delay in seconds
randomFactor := minDelay + rand.Float64()*(maxDelay-minDelay)
backoff := math.Pow(randomFactor, float64(i))
randomDelay := time.Duration(backoff) * time.Second
maxDelay := (time.Duration(metric.TestTimeInterval) * time.Minute) * time.Duration(metric.SpreadFactor*10) / 10
minDelay := time.Second
randomFactor := minDelay + time.Duration(rand.Float64()*10)*(maxDelay-minDelay)/10
jitter := time.Duration(1<<i) * time.Duration(10*(rand.Float64()*1.0+0.5)) / 10 * time.Second
randomDelay := jitter + randomFactor
time.Sleep(randomDelay)
metric.Value = metric.method(metric.TargetNodeIp)
......@@ -56,7 +56,7 @@ func (metric *Metric) RunPeriodicTests() {
if metric.Value != 0 {
break
}
log.Infof("Couldn't measure %s between node %s and node %s. Trying again.", metric.Name, metric.SourceNodeName, metric.TargetNodeName, metric.Value)
log.Infof("Couldn't measure %s between node %s and node %s. Trying again.", metric.Name, metric.SourceNodeName, metric.TargetNodeName)
}
log.Infof(" %s between node %s and node %s is %f.", metric.Name, metric.SourceNodeName, metric.TargetNodeName, metric.Value)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment