Skip to content

Commit

Permalink
garden
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Dec 30, 2023
1 parent c8d625b commit 77e7428
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ handler _ = return Stamina.Retry
go2 :: IO ()
go2 = do
defaults <- Stamina.defaults
Stamina.retryOnExceptions defaults handler $ \retryStatus -> do
Stamina.retryFor defaults handler $ \retryStatus -> do
throwM $ userError "nope"
```

Expand Down
16 changes: 7 additions & 9 deletions src/Stamina.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Stamina
( -- functions
retry,
retryOnExceptions,
retryFor,
-- types
RetrySettings (..),
defaults,
Expand Down Expand Up @@ -40,7 +40,7 @@ data RetryStatus = RetryStatus
{ attempts :: Int, -- Number of retry attempts so far.
delay :: NominalDiffTime, -- Delay before the next retry.
totalDelay :: NominalDiffTime, -- Total delay so far.
reset :: IO (), -- Reset the retry status to the initial state.
resetInitial :: IO (), -- Reset the retry status to the initial state.
lastException :: Maybe SomeException -- The last exception that was thrown.
}

Expand All @@ -54,7 +54,7 @@ defaults = do
{ attempts = 0,
delay = 0,
totalDelay = 0,
reset = void $ putMVar resetMVar (),
resetInitial = void $ putMVar resetMVar (),
lastException = Nothing
},
maxAttempts = Just 10,
Expand All @@ -81,7 +81,7 @@ data RetryAction

-- If all retries fail, the last exception is let through.
retry :: (MonadCatch m, MonadIO m) => RetrySettings -> (RetryStatus -> m a) -> m a
retry settings = retryOnExceptions settings skipAsyncExceptions
retry settings = retryFor settings skipAsyncExceptions
where
-- skipAsyncExceptions :: SomeException -> m RetryAction
skipAsyncExceptions exc = case fromException exc of
Expand All @@ -90,13 +90,13 @@ retry settings = retryOnExceptions settings skipAsyncExceptions

-- TODO: implement reset
-- Same as retry, but only retry the given exceptions.
retryOnExceptions ::
retryFor ::
(Exception exc, MonadIO m, MonadCatch m) =>
RetrySettings ->
(exc -> m RetryAction) ->
(RetryStatus -> m a) ->
m a
retryOnExceptions settings handler action =
retryFor settings handler action =
go $ initialRetryStatus settings
where
-- go :: (MonadCatch m, MonadIO m) => RetryStatus -> m a
Expand All @@ -114,9 +114,7 @@ retryOnExceptions settings handler action =
RetryDelay delay_ -> do
maybeAttempt exception retryStatus delay_
RetryTime time -> do
delay_ <- liftIO $ do
now <- getCurrentTime
return $ diffUTCTime time now
delay_ <- liftIO $ diffUTCTime time <$> getCurrentTime
maybeAttempt exception retryStatus delay_

updateRetryStatus :: RetryStatus -> NominalDiffTime -> SomeException -> RetryStatus
Expand Down
2 changes: 1 addition & 1 deletion src/Stamina/HTTP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Text.Read qualified as ReadPrec
--
-- Retries a subset of HTTP exceptions and overrides the delay with the Retry-After header if present.
retry :: (MonadIO m, MonadCatch m) => Stamina.RetrySettings -> (Stamina.RetryStatus -> m a) -> m a
retry settings = Stamina.retryOnExceptions settings handler
retry settings = Stamina.retryFor settings handler

handler :: (MonadIO m) => SomeException -> m Stamina.RetryAction
handler =
Expand Down

0 comments on commit 77e7428

Please sign in to comment.