Created
January 20, 2020 00:19
-
-
Save neolit123/50620cbe03da39dd52039440e274bbfd to your computer and use it in GitHub Desktop.
exponential-backoff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"math" | |
"math/rand" | |
"os" | |
"strconv" | |
"time" | |
) | |
func exitWithError(err error) { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
func main() { | |
fmt.Println("* exponential backoff calculator") | |
scanner := bufio.NewScanner(os.Stdin) | |
sz := 64 | |
fmt.Print("steps: ") | |
scanner.Scan() | |
steps, err := strconv.ParseFloat(scanner.Text(), sz) | |
if err != nil { | |
exitWithError(err) | |
} | |
fmt.Print("duration: ") | |
scanner.Scan() | |
duration, err := strconv.ParseFloat(scanner.Text(), sz) | |
if err != nil { | |
exitWithError(err) | |
} | |
fmt.Print("factor: ") | |
scanner.Scan() | |
factor, err := strconv.ParseFloat(scanner.Text(), sz) | |
if err != nil { | |
exitWithError(err) | |
} | |
fmt.Print("jitter: ") | |
scanner.Scan() | |
jitter, err := strconv.ParseFloat(scanner.Text(), sz) | |
if err != nil { | |
exitWithError(err) | |
} | |
value := 0.0 | |
rand.Seed(time.Now().UnixNano()) | |
for i := uint(0); i < uint(steps); i++ { | |
fmt.Printf("step: %v, value: %v\n", i, math.Round(value*100)/100) | |
value = value*factor + duration + rand.Float64()*jitter*duration | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment