Skip to content

Commit

Permalink
add inner type signature
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Jan 17, 2024
1 parent 6310076 commit d24f052
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/Stamina.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE ScopedTypeVariables #-}

module Stamina
( -- functions
retry,
Expand All @@ -14,7 +16,7 @@ module Stamina
)
where

import Control.Concurrent (isEmptyMVar, newEmptyMVar, threadDelay, tryPutMVar)
import Control.Concurrent (MVar, isEmptyMVar, newEmptyMVar, threadDelay, tryPutMVar)
import Control.Exception (Exception (..), SomeAsyncException (SomeAsyncException), SomeException, throwIO)
import Control.Monad (void)
import Control.Monad.Catch (MonadCatch, throwM, try)
Expand Down Expand Up @@ -100,16 +102,17 @@ data RetryAction
-- @
--
-- If all retries fail, the last exception is let through.
retry :: (MonadCatch m, MonadIO m) => RetrySettings -> (RetryStatus -> m a) -> m a
retry :: forall m a. (MonadCatch m, MonadIO m) => RetrySettings -> (RetryStatus -> m a) -> m a
retry settings = retryFor settings skipAsyncExceptions
where
-- skipAsyncExceptions :: SomeException -> m RetryAction
skipAsyncExceptions :: SomeException -> m RetryAction
skipAsyncExceptions exc = case fromException exc of
Just (SomeAsyncException _) -> return RaiseException
Nothing -> return Retry

-- Same as retry, but only retry the given exceptions.
retryFor ::
forall m exc a.
(Exception exc, MonadIO m, MonadCatch m) =>
RetrySettings ->
(exc -> m RetryAction) ->
Expand All @@ -121,7 +124,7 @@ retryFor settings handler action = initialize >>= go
resetMVar <- liftIO $ newEmptyMVar
let retryStatus = (initialRetryStatus settings) {resetInitial = void $ tryPutMVar resetMVar ()}
return (retryStatus, resetMVar)
-- go :: (MonadCatch m, MonadIO m) => RetryStatus -> m a
go :: (MonadCatch m, MonadIO m) => (RetryStatus, MVar ()) -> m a
go (retryStatus, currentResetMVar) = do
result <- try $ action retryStatus
case result of
Expand All @@ -135,7 +138,7 @@ retryFor settings handler action = initialize >>= go
exceptionAction <- handler exception
delay_ <- case exceptionAction of
RaiseException -> throwM exception
Retry -> liftIO $ increaseDelay newRetryStatus
Retry -> increaseDelay newRetryStatus
RetryDelay delay_ -> return delay_
RetryTime time -> liftIO $ diffUTCTime time <$> getCurrentTime
let RetrySettings {maxTime, maxAttempts} = settings
Expand Down

0 comments on commit d24f052

Please sign in to comment.