mirror of
https://github.com/cachix/stamina.hs.git
synced 2024-09-17 13:27:22 +03:00
Retrying for humans using Haskell.
.github/workflows | ||
src | ||
test | ||
.envrc | ||
.gitignore | ||
CHANGELOG.md | ||
devenv.lock | ||
devenv.nix | ||
devenv.yaml | ||
LICENSE | ||
README.md | ||
stack.yaml | ||
stamina.cabal |
Stamina
A retry Haskell library for humans:
- Exponential backoff with jitter between retries.
- Limit the attempts of retries and total time.
Stamina.HTTP
for retrying retriableNetwork.HTTP.Client
exceptions.- Introspectable retry state for logging using
RetryStatus
.
API
import Control.Exception (Exception, Handler)
import Control.Monad.IO.Class (MonadIO)
import Data.Time.Clock (DiffTime)
defaults :: RetrySettings
data RetryStatus = RetryStatus
{ attempts :: Int,
delay :: DiffTime,
totalDelay :: DiffTime
}
-- Retry on all sync exceptions
retry :: MonadIO m
=> RetrySettings
-> (RetryStatus -> m a)
-> m a
-- Retry on specific exceptions
retryOnExceptions :: (Exception e, MonadIO m)
=> RetrySettings
-> [Handler RetryAction]
-> (RetryStatus -> m a)
-> m a
data RetryAction = Skip | Retry | RetryAfter Int
Example
import qualified Stamina
main :: IO ()
main = do
Stamina.retry Stamina.defaults $ \retryStatus -> do
... monadic logic that raises exceptions
Development
-
Install devenv.sh.
-
devenv shell
-
stack build
Credits
- Heavily inspired by stamina for Python.
- retry as case study for what needs to be supported.