module Ecluse.Core.Registry.Backoff (
reportingBackoff,
CooldownEvent (..),
publicRetryAfter,
PublicBackoff,
newPublicBackoff,
PublicAttempt,
withPublicAttempt,
PublicResponse (..),
recordPublicResponse,
) where
import Data.Map.Strict qualified as Map
import Data.Time (UTCTime, diffUTCTime)
import UnliftIO.Exception (mask)
import Ecluse.Core.Breaker (Breaker, BreakerPhase (..))
import Ecluse.Core.Breaker qualified as Breaker
import Ecluse.Core.Fault (RetryAfter (RetryAfter))
import Ecluse.Core.InFlight (guardInFlight)
import Ecluse.Core.Security.Authority (HostPort)
data BackoffPolicy = BackoffPolicy
{ BackoffPolicy -> RetryAfter
bpFloor :: RetryAfter
, BackoffPolicy -> RetryAfter
bpCap :: RetryAfter
, BackoffPolicy -> RetryAfter
bpProbeHint :: RetryAfter
}
deriving stock (BackoffPolicy -> BackoffPolicy -> Bool
(BackoffPolicy -> BackoffPolicy -> Bool)
-> (BackoffPolicy -> BackoffPolicy -> Bool) -> Eq BackoffPolicy
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BackoffPolicy -> BackoffPolicy -> Bool
== :: BackoffPolicy -> BackoffPolicy -> Bool
$c/= :: BackoffPolicy -> BackoffPolicy -> Bool
/= :: BackoffPolicy -> BackoffPolicy -> Bool
Eq, Int -> BackoffPolicy -> ShowS
[BackoffPolicy] -> ShowS
BackoffPolicy -> String
(Int -> BackoffPolicy -> ShowS)
-> (BackoffPolicy -> String)
-> ([BackoffPolicy] -> ShowS)
-> Show BackoffPolicy
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BackoffPolicy -> ShowS
showsPrec :: Int -> BackoffPolicy -> ShowS
$cshow :: BackoffPolicy -> String
show :: BackoffPolicy -> String
$cshowList :: [BackoffPolicy] -> ShowS
showList :: [BackoffPolicy] -> ShowS
Show)
fixedBackoffPolicy :: BackoffPolicy
fixedBackoffPolicy :: BackoffPolicy
fixedBackoffPolicy = RetryAfter -> RetryAfter -> RetryAfter -> BackoffPolicy
BackoffPolicy (Int -> RetryAfter
RetryAfter Int
60) (Int -> RetryAfter
RetryAfter Int
600) (Int -> RetryAfter
RetryAfter Int
1)
data CooldownEvent = CoolingDown | Recovered
deriving stock (CooldownEvent -> CooldownEvent -> Bool
(CooldownEvent -> CooldownEvent -> Bool)
-> (CooldownEvent -> CooldownEvent -> Bool) -> Eq CooldownEvent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CooldownEvent -> CooldownEvent -> Bool
== :: CooldownEvent -> CooldownEvent -> Bool
$c/= :: CooldownEvent -> CooldownEvent -> Bool
/= :: CooldownEvent -> CooldownEvent -> Bool
Eq, Int -> CooldownEvent -> ShowS
[CooldownEvent] -> ShowS
CooldownEvent -> String
(Int -> CooldownEvent -> ShowS)
-> (CooldownEvent -> String)
-> ([CooldownEvent] -> ShowS)
-> Show CooldownEvent
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CooldownEvent -> ShowS
showsPrec :: Int -> CooldownEvent -> ShowS
$cshow :: CooldownEvent -> String
show :: CooldownEvent -> String
$cshowList :: [CooldownEvent] -> ShowS
showList :: [CooldownEvent] -> ShowS
Show)
reportingBackoff :: (HostPort -> CooldownEvent -> IO ()) -> PublicBackoff -> PublicBackoff
reportingBackoff :: (HostPort -> CooldownEvent -> IO ())
-> PublicBackoff -> PublicBackoff
reportingBackoff HostPort -> CooldownEvent -> IO ()
report PublicBackoff
backoff = PublicBackoff
backoff{pbReport = report}
publicRetryAfter :: PublicBackoff -> HostPort -> IO (Maybe RetryAfter)
publicRetryAfter :: PublicBackoff -> HostPort -> IO (Maybe RetryAfter)
publicRetryAfter PublicBackoff
backoff HostPort
authority = do
now <- PublicBackoff -> IO UTCTime
pbClock PublicBackoff
backoff
hosts <- readTVarIO (pbHosts backoff)
pure $
Map.lookup authority hosts >>= \HostState
hostState -> case Breaker -> BreakerPhase
Breaker.breakerPhase (HostState -> Breaker
hsBreaker HostState
hostState) of
Closed{} -> Maybe RetryAfter
forall a. Maybe a
Nothing
BreakerPhase
RecoveryReady -> Maybe RetryAfter
forall a. Maybe a
Nothing
BreakerPhase
_ -> RetryAfter -> Maybe RetryAfter
forall a. a -> Maybe a
Just (BackoffPolicy -> UTCTime -> Breaker -> RetryAfter
remainingDelay (PublicBackoff -> BackoffPolicy
pbPolicy PublicBackoff
backoff) UTCTime
now (HostState -> Breaker
hsBreaker HostState
hostState))
data PublicBackoff = PublicBackoff
{ PublicBackoff -> BackoffPolicy
pbPolicy :: BackoffPolicy
, PublicBackoff -> IO UTCTime
pbClock :: IO UTCTime
, PublicBackoff -> RetryAfter -> RetryAfter -> IO RetryAfter
pbJitter :: RetryAfter -> RetryAfter -> IO RetryAfter
, PublicBackoff -> TVar (Map HostPort HostState)
pbHosts :: TVar (Map HostPort HostState)
, PublicBackoff -> HostPort -> CooldownEvent -> IO ()
pbReport :: HostPort -> CooldownEvent -> IO ()
}
data HostState = HostState
{ HostState -> Breaker
hsBreaker :: Breaker
, HostState -> RetryAfter
hsCeiling :: RetryAfter
}
newPublicBackoff :: IO UTCTime -> (RetryAfter -> RetryAfter -> IO RetryAfter) -> IO PublicBackoff
newPublicBackoff :: IO UTCTime
-> (RetryAfter -> RetryAfter -> IO RetryAfter) -> IO PublicBackoff
newPublicBackoff IO UTCTime
clock RetryAfter -> RetryAfter -> IO RetryAfter
jitter = do
hosts <- Map HostPort HostState -> IO (TVar (Map HostPort HostState))
forall (m :: * -> *) a. MonadIO m => a -> m (TVar a)
newTVarIO Map HostPort HostState
forall k a. Map k a
Map.empty
pure (PublicBackoff fixedBackoffPolicy clock jitter hosts (\HostPort
_ CooldownEvent
_ -> IO ()
forall (f :: * -> *). Applicative f => f ()
pass))
data PublicAttempt = PublicAttempt
{ PublicAttempt -> PublicBackoff
paBackoff :: PublicBackoff
, PublicAttempt -> HostPort
paAuthority :: HostPort
, PublicAttempt -> Attempt
paTicket :: Breaker.Attempt
, PublicAttempt -> TVar Bool
paFinished :: TVar Bool
}
withPublicAttempt :: PublicBackoff -> HostPort -> (PublicAttempt -> IO a) -> IO (Either RetryAfter a)
withPublicAttempt :: forall a.
PublicBackoff
-> HostPort -> (PublicAttempt -> IO a) -> IO (Either RetryAfter a)
withPublicAttempt PublicBackoff
backoff HostPort
authority PublicAttempt -> IO a
action = ((forall a. IO a -> IO a) -> IO (Either RetryAfter a))
-> IO (Either RetryAfter a)
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> m a) -> m b) -> m b
mask (((forall a. IO a -> IO a) -> IO (Either RetryAfter a))
-> IO (Either RetryAfter a))
-> ((forall a. IO a -> IO a) -> IO (Either RetryAfter a))
-> IO (Either RetryAfter a)
forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
restore -> do
now <- PublicBackoff -> IO UTCTime
pbClock PublicBackoff
backoff
admitted <- atomically (claim backoff authority now)
case admitted of
Left RetryAfter
delay -> Either RetryAfter a -> IO (Either RetryAfter a)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RetryAfter -> Either RetryAfter a
forall a b. a -> Either a b
Left RetryAfter
delay)
Right PublicAttempt
attempt -> a -> Either RetryAfter a
forall a b. b -> Either a b
Right (a -> Either RetryAfter a) -> IO a -> IO (Either RetryAfter a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (IO a -> IO a) -> (SomeException -> IO ()) -> IO () -> IO a -> IO a
forall a.
(IO a -> IO a) -> (SomeException -> IO ()) -> IO () -> IO a -> IO a
guardInFlight IO a -> IO a
forall a. IO a -> IO a
restore (IO () -> SomeException -> IO ()
forall a b. a -> b -> a
const IO ()
forall (f :: * -> *). Applicative f => f ()
pass) (PublicAttempt -> IO ()
releaseAttempt PublicAttempt
attempt) (PublicAttempt -> IO a
action PublicAttempt
attempt)
data PublicResponse
= NonThrottling
|
Throttled (Maybe RetryAfter)
deriving stock (PublicResponse -> PublicResponse -> Bool
(PublicResponse -> PublicResponse -> Bool)
-> (PublicResponse -> PublicResponse -> Bool) -> Eq PublicResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PublicResponse -> PublicResponse -> Bool
== :: PublicResponse -> PublicResponse -> Bool
$c/= :: PublicResponse -> PublicResponse -> Bool
/= :: PublicResponse -> PublicResponse -> Bool
Eq, Int -> PublicResponse -> ShowS
[PublicResponse] -> ShowS
PublicResponse -> String
(Int -> PublicResponse -> ShowS)
-> (PublicResponse -> String)
-> ([PublicResponse] -> ShowS)
-> Show PublicResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PublicResponse -> ShowS
showsPrec :: Int -> PublicResponse -> ShowS
$cshow :: PublicResponse -> String
show :: PublicResponse -> String
$cshowList :: [PublicResponse] -> ShowS
showList :: [PublicResponse] -> ShowS
Show)
recordPublicResponse :: PublicAttempt -> PublicResponse -> IO ()
recordPublicResponse :: PublicAttempt -> PublicResponse -> IO ()
recordPublicResponse PublicAttempt
attempt = \case
PublicResponse
NonThrottling -> (HostState -> HostState) -> IO ()
commit (BackoffPolicy -> Attempt -> HostState -> HostState
acceptResponse BackoffPolicy
policy (PublicAttempt -> Attempt
paTicket PublicAttempt
attempt))
Throttled Maybe RetryAfter
hint -> do
delay <- PublicAttempt -> Maybe RetryAfter -> IO RetryAfter
responseDelay PublicAttempt
attempt Maybe RetryAfter
hint
now <- pbClock backoff
commit (throttleResponse policy (paTicket attempt) now delay)
where
backoff :: PublicBackoff
backoff = PublicAttempt -> PublicBackoff
paBackoff PublicAttempt
attempt
policy :: BackoffPolicy
policy = PublicBackoff -> BackoffPolicy
pbPolicy PublicBackoff
backoff
commit :: (HostState -> HostState) -> IO ()
commit HostState -> HostState
step = STM (Maybe CooldownEvent) -> IO (Maybe CooldownEvent)
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (PublicAttempt
-> (HostState -> HostState) -> STM (Maybe CooldownEvent)
settle PublicAttempt
attempt HostState -> HostState
step) IO (Maybe CooldownEvent) -> (Maybe CooldownEvent -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (CooldownEvent -> IO ()) -> Maybe CooldownEvent -> IO ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ (PublicBackoff -> HostPort -> CooldownEvent -> IO ()
pbReport PublicBackoff
backoff (PublicAttempt -> HostPort
paAuthority PublicAttempt
attempt))
initialHost :: BackoffPolicy -> HostState
initialHost :: BackoffPolicy -> HostState
initialHost BackoffPolicy
policy = Breaker -> RetryAfter -> HostState
HostState Breaker
Breaker.initialBreaker (BackoffPolicy -> RetryAfter
bpFloor BackoffPolicy
policy)
claim :: PublicBackoff -> HostPort -> UTCTime -> STM (Either RetryAfter PublicAttempt)
claim :: PublicBackoff
-> HostPort -> UTCTime -> STM (Either RetryAfter PublicAttempt)
claim PublicBackoff
backoff HostPort
authority UTCTime
now = do
hosts <- TVar (Map HostPort HostState) -> STM (Map HostPort HostState)
forall a. TVar a -> STM a
readTVar (PublicBackoff -> TVar (Map HostPort HostState)
pbHosts PublicBackoff
backoff)
let hostState = HostState -> HostPort -> Map HostPort HostState -> HostState
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault (BackoffPolicy -> HostState
initialHost (PublicBackoff -> BackoffPolicy
pbPolicy PublicBackoff
backoff)) HostPort
authority Map HostPort HostState
hosts
(ticket, breaker) = Breaker.admit now (hsBreaker hostState)
case ticket of
Maybe Attempt
Nothing -> Either RetryAfter PublicAttempt
-> STM (Either RetryAfter PublicAttempt)
forall a. a -> STM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RetryAfter -> Either RetryAfter PublicAttempt
forall a b. a -> Either a b
Left (BackoffPolicy -> UTCTime -> Breaker -> RetryAfter
remainingDelay (PublicBackoff -> BackoffPolicy
pbPolicy PublicBackoff
backoff) UTCTime
now (HostState -> Breaker
hsBreaker HostState
hostState)))
Just Attempt
owned -> do
TVar (Map HostPort HostState) -> Map HostPort HostState -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar (PublicBackoff -> TVar (Map HostPort HostState)
pbHosts PublicBackoff
backoff) (HostPort
-> HostState -> Map HostPort HostState -> Map HostPort HostState
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert HostPort
authority HostState
hostState{hsBreaker = breaker} Map HostPort HostState
hosts)
finished <- Bool -> STM (TVar Bool)
forall a. a -> STM (TVar a)
newTVar Bool
False
pure (Right (PublicAttempt backoff authority owned finished))
remainingDelay :: BackoffPolicy -> UTCTime -> Breaker -> RetryAfter
remainingDelay :: BackoffPolicy -> UTCTime -> Breaker -> RetryAfter
remainingDelay BackoffPolicy
policy UTCTime
now Breaker
breaker = case Breaker -> BreakerPhase
Breaker.breakerPhase Breaker
breaker of
Open UTCTime
until' -> Int -> RetryAfter
RetryAfter (Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
max Integer
1 (Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
min (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int
forall a. Bounded a => a
maxBound :: Int)) (NominalDiffTime -> Integer
forall b. Integral b => NominalDiffTime -> b
forall a b. (RealFrac a, Integral b) => a -> b
ceiling (UTCTime -> UTCTime -> NominalDiffTime
diffUTCTime UTCTime
until' UTCTime
now)))))
BreakerPhase
_ -> BackoffPolicy -> RetryAfter
bpProbeHint BackoffPolicy
policy
releaseAttempt :: PublicAttempt -> IO ()
releaseAttempt :: PublicAttempt -> IO ()
releaseAttempt PublicAttempt
attempt = IO (Maybe CooldownEvent) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (STM (Maybe CooldownEvent) -> IO (Maybe CooldownEvent)
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (PublicAttempt
-> (HostState -> HostState) -> STM (Maybe CooldownEvent)
settle PublicAttempt
attempt (\HostState
hostState -> HostState
hostState{hsBreaker = Breaker.release (paTicket attempt) (hsBreaker hostState)})))
settle :: PublicAttempt -> (HostState -> HostState) -> STM (Maybe CooldownEvent)
settle :: PublicAttempt
-> (HostState -> HostState) -> STM (Maybe CooldownEvent)
settle PublicAttempt
attempt HostState -> HostState
step = do
finished <- TVar Bool -> STM Bool
forall a. TVar a -> STM a
readTVar (PublicAttempt -> TVar Bool
paFinished PublicAttempt
attempt)
if finished
then pure Nothing
else do
let hostsVar = PublicBackoff -> TVar (Map HostPort HostState)
pbHosts (PublicAttempt -> PublicBackoff
paBackoff PublicAttempt
attempt)
hosts <- readTVar hostsVar
let old = HostPort -> Map HostPort HostState -> Maybe HostState
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (PublicAttempt -> HostPort
paAuthority PublicAttempt
attempt) Map HostPort HostState
hosts
event = Maybe HostState
old Maybe HostState
-> (HostState -> Maybe CooldownEvent) -> Maybe CooldownEvent
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \HostState
hostState -> Breaker -> Breaker -> Maybe CooldownEvent
transition (HostState -> Breaker
hsBreaker HostState
hostState) (HostState -> Breaker
hsBreaker (HostState -> HostState
step HostState
hostState))
writeTVar hostsVar (Map.adjust step (paAuthority attempt) hosts)
writeTVar (paFinished attempt) True
pure event
acceptResponse :: BackoffPolicy -> Breaker.Attempt -> HostState -> HostState
acceptResponse :: BackoffPolicy -> Attempt -> HostState -> HostState
acceptResponse BackoffPolicy
policy Attempt
ticket HostState
hostState = case Breaker -> BreakerPhase
Breaker.breakerPhase (HostState -> Breaker
hsBreaker HostState
hostState) of
BreakerPhase
HalfOpen
| Breaker
recovered Breaker -> Breaker -> Bool
forall a. Eq a => a -> a -> Bool
/= HostState -> Breaker
hsBreaker HostState
hostState -> HostState
hostState{hsBreaker = recovered, hsCeiling = bpFloor policy}
BreakerPhase
_ -> HostState
hostState
where
recovered :: Breaker
recovered = Attempt -> Breaker -> Breaker
Breaker.recordSuccess Attempt
ticket (HostState -> Breaker
hsBreaker HostState
hostState)
responseDelay :: PublicAttempt -> Maybe RetryAfter -> IO RetryAfter
responseDelay :: PublicAttempt -> Maybe RetryAfter -> IO RetryAfter
responseDelay PublicAttempt
_ (Just RetryAfter
hint) | RetryAfter
hint RetryAfter -> RetryAfter -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> RetryAfter
RetryAfter Int
0 = RetryAfter -> IO RetryAfter
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure RetryAfter
hint
responseDelay PublicAttempt
attempt Maybe RetryAfter
_ = do
hosts <- TVar (Map HostPort HostState) -> IO (Map HostPort HostState)
forall (m :: * -> *) a. MonadIO m => TVar a -> m a
readTVarIO (PublicBackoff -> TVar (Map HostPort HostState)
pbHosts PublicBackoff
backoff)
let ceilingDelay = RetryAfter
-> (HostState -> RetryAfter) -> Maybe HostState -> RetryAfter
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (BackoffPolicy -> RetryAfter
bpFloor BackoffPolicy
policy) HostState -> RetryAfter
hsCeiling (HostPort -> Map HostPort HostState -> Maybe HostState
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (PublicAttempt -> HostPort
paAuthority PublicAttempt
attempt) Map HostPort HostState
hosts)
selected <- pbJitter backoff (bpFloor policy) ceilingDelay
pure (max (bpFloor policy) (min ceilingDelay selected))
where
backoff :: PublicBackoff
backoff = PublicAttempt -> PublicBackoff
paBackoff PublicAttempt
attempt
policy :: BackoffPolicy
policy = PublicBackoff -> BackoffPolicy
pbPolicy PublicBackoff
backoff
throttleResponse :: BackoffPolicy -> Breaker.Attempt -> UTCTime -> RetryAfter -> HostState -> HostState
throttleResponse :: BackoffPolicy
-> Attempt -> UTCTime -> RetryAfter -> HostState -> HostState
throttleResponse BackoffPolicy
policy Attempt
ticket UTCTime
now (RetryAfter Int
seconds) HostState
hostState
| Breaker
next Breaker -> Breaker -> Bool
forall a. Eq a => a -> a -> Bool
== Breaker
old = HostState
hostState
| Bool
otherwise = HostState
hostState{hsBreaker = next, hsCeiling = nextCeiling}
where
old :: Breaker
old = HostState -> Breaker
hsBreaker HostState
hostState
duration :: NominalDiffTime
duration = case Breaker -> BreakerPhase
Breaker.breakerPhase Breaker
old of
Open UTCTime
until' -> NominalDiffTime -> NominalDiffTime -> NominalDiffTime
forall a. Ord a => a -> a -> a
max (Int -> NominalDiffTime
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
seconds) (UTCTime -> UTCTime -> NominalDiffTime
diffUTCTime UTCTime
until' UTCTime
now)
BreakerPhase
_ -> Int -> NominalDiffTime
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
seconds
next :: Breaker
next = Int -> NominalDiffTime -> UTCTime -> Attempt -> Breaker -> Breaker
Breaker.recordFailure Int
1 NominalDiffTime
duration UTCTime
now Attempt
ticket Breaker
old
nextCeiling :: RetryAfter
nextCeiling = case Breaker -> BreakerPhase
Breaker.breakerPhase Breaker
old of
Open{} -> HostState -> RetryAfter
hsCeiling HostState
hostState
BreakerPhase
_ -> BackoffPolicy -> RetryAfter -> RetryAfter
growFallback BackoffPolicy
policy (HostState -> RetryAfter
hsCeiling HostState
hostState)
growFallback :: BackoffPolicy -> RetryAfter -> RetryAfter
growFallback :: BackoffPolicy -> RetryAfter -> RetryAfter
growFallback BackoffPolicy
policy (RetryAfter Int
current) = case BackoffPolicy -> RetryAfter
bpCap BackoffPolicy
policy of
RetryAfter Int
cap -> Int -> RetryAfter
RetryAfter (Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
min (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
cap) (Integer
2 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
current)))
transition :: Breaker -> Breaker -> Maybe CooldownEvent
transition :: Breaker -> Breaker -> Maybe CooldownEvent
transition Breaker
old Breaker
new = case (Breaker -> BreakerPhase
Breaker.breakerPhase Breaker
old, Breaker -> BreakerPhase
Breaker.breakerPhase Breaker
new) of
(Open{}, Open{}) -> Maybe CooldownEvent
forall a. Maybe a
Nothing
(BreakerPhase
_, Open{}) -> CooldownEvent -> Maybe CooldownEvent
forall a. a -> Maybe a
Just CooldownEvent
CoolingDown
(BreakerPhase
HalfOpen, Closed{}) -> CooldownEvent -> Maybe CooldownEvent
forall a. a -> Maybe a
Just CooldownEvent
Recovered
(BreakerPhase, BreakerPhase)
_ -> Maybe CooldownEvent
forall a. Maybe a
Nothing