Created
August 18, 2022 00:39
-
-
Save Gabriella439/193de4cd489c4a81c991b07d85a79d3b to your computer and use it in GitHub Desktop.
Example async-exception-safe retry function
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
{-# LANGUAGE BlockArguments #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
module Main where | |
import Control.Concurrent (threadDelay) | |
import Control.Exception | |
retry :: Int -> IO a -> IO a | |
retry n io | |
| n <= 0 = io | |
| otherwise = io `catch` \(_ :: SomeException) -> do | |
mask \restore -> do | |
restore (threadDelay 1000000) | |
`onException` restore (retry (n - 1) io) | |
restore (retry (n - 1) io) | |
main :: IO () | |
main = do | |
retry 4 (print 1) | |
retry 4 (fail "Urk!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment