Skip to content

A peer-to-peer stack for code collaboration

feat: Explicit timeouts for IPFS API. (#557)

James Haydon051c9e94fdea
diff --git a/src/Radicle/Daemon/Client.hs b/src/Radicle/Daemon/Client.hs
index df39878..155851f 100644
--- a/src/Radicle/Daemon/Client.hs
+++ b/src/Radicle/Daemon/Client.hs
@@ -24,7 +24,7 @@ import Radicle.Internal.Core
import Radicle.Internal.Identifier
import qualified Radicle.Internal.PrimFns as PrimFns
import Radicle.Internal.Type
-
+import qualified Radicle.Ipfs as Ipfs
-- | Constraints for all client functions
type ClientM m = (MonadIO m, MonadError Servant.ServantError m)
@@ -41,7 +41,7 @@ newClient :: IO Client
newClient = do
baseUrlString <- fromMaybe "http://localhost:8909" <$> lookupEnv "RAD_DAEMON_API_URL"
clientBaseUrl <- Servant.parseBaseUrl baseUrlString
- clientHttpManager <- HttpClient.newManager HttpClient.defaultManagerSettings
+ clientHttpManager <- Ipfs.newIpfsHttpClientManager
pure Client {..}
-- | Send an input to a machine to be evaluated and applied. Returns the results
diff --git a/src/Radicle/Ipfs.hs b/src/Radicle/Ipfs.hs
index 358ce59..3c39ddf 100644
--- a/src/Radicle/Ipfs.hs
+++ b/src/Radicle/Ipfs.hs
@@ -2,6 +2,7 @@
module Radicle.Ipfs
( MonadIpfs(..)
, Client
+ , newIpfsHttpClientManager
, newClient
, IpfsException(..)
@@ -65,11 +66,17 @@ data Client = Client
, clientHttpManager :: HttpClient.Manager
}
+newIpfsHttpClientManager :: IO HttpClient.Manager
+newIpfsHttpClientManager = HttpClient.newManager
+ HttpClient.defaultManagerSettings{
+ HttpClient.managerResponseTimeout = HttpClient.responseTimeoutMicro (ipfsTimeoutSeconds * 1000000)
+ }
+
newClient :: MonadIO m => m Client
newClient = liftIO $ do
baseUrl <- fromMaybe "http://localhost:9301" <$> lookupEnv "RAD_IPFS_API_URL"
clientBaseRequest <- HttpClient.parseUrlThrow baseUrl
- clientHttpManager <- HttpClient.newManager HttpClient.defaultManagerSettings
+ clientHttpManager <- newIpfsHttpClientManager
pure Client{clientBaseRequest, clientHttpManager}
data IpfsException
@@ -291,7 +298,7 @@ newtype NameResolveResponse
= NameResolveResponse CID
nameResolve :: (MonadIpfs m) => IpnsId -> m NameResolveResponse
-nameResolve ipnsId = ipfsHttpGet "name/resolve" [("arg", ipnsId), ("recursive", "true")]
+nameResolve ipnsId = ipfsHttpGet "name/resolve" [("arg", ipnsId), ("recursive", "true"), ("dht-timeout", show ipfsTimeoutSeconds <> "s")]
instance FromJSON NameResolveResponse where
parseJSON = Aeson.withObject "v0/name/resolve response" $ \o -> do
@@ -361,6 +368,9 @@ makeIpfsRequest baseRequest method path params =
where
params' = [ (encodeUtf8 key, Just (encodeUtf8 value)) | (key, value) <- params]
+-- | Timeout for IPFS API.
+ipfsTimeoutSeconds :: Int
+ipfsTimeoutSeconds = 60
-- | Parses response body as JSON and returns the parsed value. @path@
-- is the IPFS API the response was obtained from. Throws