Skip to content

Commit

Permalink
Add new exception HttpClientTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
morucci committed Feb 8, 2025
1 parent 4941123 commit 65721eb
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Monocle/Effects.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import Monocle.Config qualified
import Network.HTTP.Client (HttpException (..))
import Network.HTTP.Client qualified as HTTP

import Control.Exception qualified
import Effectful as E
import Effectful.Dispatch.Static (SideEffects (..), StaticRep, evalStaticRep, getStaticRep, localStaticRep)
import Effectful.Dispatch.Static.Primitive qualified as EffStatic
Expand Down Expand Up @@ -545,17 +546,21 @@ httpRequestWithTimeout request = do
resp <- Effectful.Timeout.timeout 75_000_000 $ httpRequest request
case resp of
Just x -> pure x
Nothing -> error "The HTTP request hung for more than 75 seconds"
Nothing -> unsafeEff_ $ Control.Exception.throwIO HttpClientTimeout

-------------------------------------------------------------------------------
-- A network retry system

retryLimit :: Int
retryLimit = 7

data HttpClientTimeout = HttpClientTimeout HTTP.Request
deriving (Show)
instance Exception HttpClientTimeout

-- | Retry HTTP network action, doubling backoff each time
httpRetry :: (HasCallStack, PrometheusEffect :> es, Retry :> es, LoggerEffect :> es) => Text -> Eff es a -> Eff es a
httpRetry urlLabel baseAction = Retry.recovering policy [httpHandler] (const action)
httpRetry urlLabel baseAction = Retry.recovering policy [httpHandler, httpHandler2] (const action)
where
modName = case getCallStack callStack of
((_, srcLoc) : _) -> from (srcLocModule srcLoc)
Expand All @@ -577,6 +582,8 @@ httpRetry urlLabel baseAction = Retry.recovering policy [httpHandler] (const act
promIncrCounter httpFailureCounter label
pure True
InvalidUrlException _ _ -> pure False
httpHandler2 (RetryStatus _ _ _) = Handler $ \case
HttpClientTimeout _ -> pure True

------------------------------------------------------------------
--
Expand Down

0 comments on commit 65721eb

Please sign in to comment.