Giter Club home page Giter Club logo

Comments (1)

ozataman avatar ozataman commented on July 18, 2024

Since the ticket is still open, I'll mention here:

Our retry package helps with this quite trivially: http://hackage.haskell.org/package/retry

Here's an example from how we handle retries in application logic:

-------------------------------------------------------------------------------
runAws
    :: (Transaction r b, MonadIO n,
        MonadReader AppEnv n, MonadCatch n)
    => ServiceConfiguration r NormalQuery
    -> Int
    -- ^ Number of retries
    -> r
    -- ^ Request
    -> ResourceT n b
runAws servConf n r = do
    mgr <- view appManager
    conf <- view appAwsConfig
    recovering (awsPolicy n) [kinesisH echo, httpRetryH, networkRetryH] $
      hoist liftIO $
      pureAws conf servConf mgr r

with

-------------------------------------------------------------------------------
kinesisH
    :: MonadIO m
    => (String -> m ())
    -- ^ How to (and whether to) report errors
    -> Int
    -- ^ Retry number
    -> Handler m Bool
kinesisH report n = handleLog chk report n
  where
    chk e = return $ case e of
        KinesisErrorResponse cd _msg -> case cd of
            "ProvisionedThroughputExceededException" -> True
            _ -> False
        KinesisOtherError stat _ -> case stat of
            Status _ "Internal Server Error" -> True
            Status _ "Service Unavailable" -> True
            _ -> False
        _ -> False


-------------------------------------------------------------------------------
awsPolicy :: Int -> RetryPolicy
awsPolicy n = capDelay 60000000 $
              mempty <> limitRetries n <> exponentialBackoff 25000



-------------------------------------------------------------------------------
-- | Which exceptions should we retry?
httpRetryH :: MonadIO m => Int -> Handler m Bool
httpRetryH n = handleLog (return . httpRetry) echo n


-------------------------------------------------------------------------------
-- | Should given exception be retried?
httpRetry :: HttpException -> Bool
httpRetry e =
    case e of
      TooManyRetries{} -> True
      ResponseTimeout{} -> True
      FailedConnectionException{} -> True
      FailedConnectionException2{} -> True
      InternalIOException{} -> True
      ProxyConnectException{} -> True
      StatusCodeException{} -> True
      NoResponseDataReceived{} -> True
      ResponseBodyTooShort{} -> True
      InvalidChunkHeaders{} -> True
      IncompleteHeaders{} -> True
      TlsException{} -> True
      _ -> False


-------------------------------------------------------------------------------
-- | 'IOException's should be retried
networkRetryH :: MonadIO m => Int -> Handler m Bool
networkRetryH n = handleLog chk echo n
    where
      chk (_ :: IOException) = return True


-------------------------------------------------------------------------------
-- | Handle exception while logging it.
handleLog
    :: (Monad m, Show e, Exception e)
    => (e -> m Bool)
    -- ^ Test for whether action is to be retried
    -> (String -> m ())
    -- ^ How ot report the generated warning message.
    -> Int
    -- ^ Retry number
    -> Handler m Bool
handleLog f report n = Handler $ \ e -> do
    res <- f e
    let msg = "[retry:" <> show n <> "] Encountered " <> show e <> ". " <>
              if res then "Retrying." else "Crashing."
    report msg
    return res

from aws.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.