Skip to content

Instantly share code, notes, and snippets.

@neolit123
Created January 20, 2020 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neolit123/50620cbe03da39dd52039440e274bbfd to your computer and use it in GitHub Desktop.
Save neolit123/50620cbe03da39dd52039440e274bbfd to your computer and use it in GitHub Desktop.
exponential-backoff
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