-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jobs): Add stale timeout check to subscription jobs (#96)
* stale timeout check * fix timer reset * format * tag * bet * num retries * unsubscribe on stale
- Loading branch information
Showing
3 changed files
with
272 additions
and
125 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package baseapp | ||
|
||
import ( | ||
"crypto/rand" | ||
"math/big" | ||
"time" | ||
) | ||
|
||
// Retry parameters. | ||
const ( | ||
maxBackoff = 2 * time.Minute | ||
backoffStart = 1 * time.Second | ||
backoffBase = 2 | ||
jitterRange = 1000 | ||
subscriptionStaleTimeout = 1 * time.Hour | ||
) | ||
|
||
// withRetry is a wrapper that retries a task with exponential backoff. | ||
func (jm *JobManager) withRetry(task func() bool) func() { | ||
return func() { | ||
backoff := backoffStart | ||
|
||
for { | ||
shouldRetry := task() | ||
if !shouldRetry { | ||
return | ||
} | ||
|
||
// Exponential backoff with jitter. | ||
jitter, _ := rand.Int(rand.Reader, big.NewInt(jitterRange)) | ||
if jitter == nil { | ||
jitter = new(big.Int) | ||
} | ||
sleep := backoff + time.Duration(jitter.Int64())*time.Millisecond | ||
time.Sleep(sleep) | ||
|
||
backoff *= backoffBase | ||
if backoff > maxBackoff { | ||
backoff = maxBackoff | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.