Last active
December 10, 2022 16:10
-
-
Save dantheman213/db6aa0c4d2071aed359b15c8d98d01b4 to your computer and use it in GitHub Desktop.
Golang exponential back off simple example
This file contains hidden or 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 "fmt" | |
import "time" | |
import "math" | |
var exponentialBackoffCeilingSecs int64 = 14400 // 4 hours | |
func main() { | |
fmt.Println("Hello World") | |
lastUpdatedAt := time.Now() | |
attempts := 0 | |
for true { | |
if time.Now().Sub(lastUpdatedAt).Hours() >= 12 { | |
attempts = 0 | |
} | |
lastUpdatedAt = time.Now() | |
attempts += 1 | |
// exponential back-off implemented | |
delaySecs := int64(math.Floor((math.Pow(2, float64(attempts)) - 1) * 0.5)) | |
if delaySecs > exponentialBackoffCeilingSecs { | |
delaySecs = exponentialBackoffCeilingSecs | |
} | |
// should only get here if the ffmpeg record stream process dies | |
fmt.Printf("this is %d attempt to restart. waiting %d seconds and then restarting\n", attempts, delaySecs) | |
time.Sleep(time.Duration(delaySecs) * time.Second) | |
} | |
} |
Author
dantheman213
commented
Aug 11, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment