Haskell 如何在超时时重试http请求?
我的下载方法经常超时,我想在这里添加一些重试:Haskell 如何在超时时重试http请求?,haskell,httprequest,Haskell,Httprequest,我的下载方法经常超时,我想在这里添加一些重试: withSocketsDo $ do irequest ← liftIO $ parseUrl url fileExist ← doesFileExist fname when fileExist $ do putStrLn " -> Removing old version" removeFile fname withManager $ \manager → do
withSocketsDo $ do
irequest ← liftIO $ parseUrl url
fileExist ← doesFileExist fname
when fileExist $ do
putStrLn " -> Removing old version"
removeFile fname
withManager $ \manager → do
let request = irequest
{ method = methodGet
, responseTimeout = Just 10000000 }
response ← http request manager
responseBody response C.$$+- sinkFile fname
我找到了带有ScopedTypeVariables
retryOnTimeout ∷ IO a → IO a
retryOnTimeout action = catch action $ \ (_ :: HttpException) → do
putStrLn "Timed out. Trying again."
threadDelay 5000000
action
添加try count不是什么大问题,但似乎我不能只使用类似的东西
response ← retryOnTimeout ( http request manager )
因为它会导致奇怪的类型冲突
Couldn't match type `IO'
with `resourcet-1.1.3.3:Control.Monad.Trans.Resource.Internal.ResourceT
IO'
Expected type: resourcet-1.1.3.3:Control.Monad.Trans.Resource.Internal.ResourceT
IO
(Network.HTTP.Conduit.Response
(C.ResumableSource IO Data.ByteString.Internal.ByteString))
Actual type: IO
(Network.HTTP.Conduit.Response
(C.ResumableSource IO Data.ByteString.Internal.ByteString))
如何为HTTP get方法实现超时重试?错误消息告诉您,您的代码需要一个
IO a
,但事实上,您得到了一个资源IO a
。最简单的方法是:
- 将您的类型签名更改为匹配
- 使用
(在Control.Exception.Lifted
包中)中的Lifted base
,而不是catch
Control.Exception
resourcet
使用resourcet
包,并使用liftIO$putStrLn“超时”运行其他方法。请重试。“
对这两个问题都是