Last active
January 10, 2018 13:07
-
-
Save jbrestan/59e27c5a3048bf4ba6eca48d2ae03049 to your computer and use it in GitHub Desktop.
Thoroughly shaven yak
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
module ExponentialBackoff = | |
open System | |
[<Measure>] type ms | |
type Backoff = | |
| Immediate | |
| Initial | |
| Double of previousDelay: int<ms> | |
let backoff (initialDelay: TimeSpan) (maximumDelay: TimeSpan) = | |
let inline intms x = int x |> LanguagePrimitives.Int32WithMeasure<ms> | |
let initialDelay = intms initialDelay.TotalMilliseconds | |
let maximumDelay = intms maximumDelay.TotalMilliseconds | |
let inline calcDelay (x:int<ms>) = List.max [ 0; List.min [ x * 2; maximumDelay ] ] | |
let inline sleep x = int x |> Async.Sleep | |
fun nextBackoff -> async { | |
match nextBackoff with | |
| Immediate -> | |
return Initial | |
| Initial -> | |
do! sleep initialDelay | |
return Double initialDelay | |
| Double previousDelay -> | |
let currentDelay = calcDelay previousDelay | |
do! sleep currentDelay | |
return Double currentDelay | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is far less cool