Skip to content

Commit 69635b7

Browse files
authoredFeb 5, 2024
Merge pull request #1286 from input-output-hk/retry-hydra-node-connection
hydra-cluster: Retry connection on HandshakeException
2 parents e2fe181 + 3475627 commit 69635b7

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed
 

‎hydra-cluster/src/HydraNode.hs

+14-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Cardano.BM.Tracing (ToObject)
99
import CardanoNode (cliQueryProtocolParameters)
1010
import Control.Concurrent.Async (forConcurrently_)
1111
import Control.Concurrent.Class.MonadSTM (modifyTVar', newTVarIO, readTVarIO)
12-
import Control.Exception (IOException)
12+
import Control.Exception (Handler (..), IOException, catches)
1313
import Control.Lens ((?~))
1414
import Control.Monad.Class.MonadAsync (forConcurrently)
1515
import Data.Aeson (Value (..), object, (.=))
@@ -31,7 +31,7 @@ import Hydra.Network qualified as Network
3131
import Hydra.Options (ChainConfig (..), DirectChainConfig (..), LedgerConfig (..), RunOptions (..), defaultDirectChainConfig, toArgs)
3232
import Network.HTTP.Req (GET (..), HttpException, JsonResponse, NoReqBody (..), POST (..), ReqBodyJson (..), defaultHttpConfig, responseBody, runReq, (/:))
3333
import Network.HTTP.Req qualified as Req
34-
import Network.WebSockets (Connection, receiveData, runClient, sendClose, sendTextData)
34+
import Network.WebSockets (Connection, HandshakeException, receiveData, runClient, sendClose, sendTextData)
3535
import System.FilePath ((<.>), (</>))
3636
import System.IO.Temp (withSystemTempDirectory)
3737
import System.Info (os)
@@ -369,18 +369,24 @@ withHydraNode' tracer chainConfig workDir hydraNodeId hydraSKey hydraVKeys allNo
369369
, i /= hydraNodeId
370370
]
371371

372-
withConnectionToNode :: Tracer IO HydraNodeLog -> Int -> (HydraClient -> IO a) -> IO a
372+
withConnectionToNode :: forall a. Tracer IO HydraNodeLog -> Int -> (HydraClient -> IO a) -> IO a
373373
withConnectionToNode tracer hydraNodeId action = do
374374
connectedOnce <- newIORef False
375375
tryConnect connectedOnce (200 :: Int)
376376
where
377377
tryConnect connectedOnce n
378378
| n == 0 = failure $ "Timed out waiting for connection to hydra-node " <> show hydraNodeId
379-
| otherwise =
380-
doConnect connectedOnce `catch` \(e :: IOException) -> do
381-
readIORef connectedOnce >>= \case
382-
False -> threadDelay 0.1 >> tryConnect connectedOnce (n - 1)
383-
True -> throwIO e
379+
| otherwise = do
380+
let
381+
retryOrThrow :: forall proxy e. Exception e => proxy e -> e -> IO a
382+
retryOrThrow _ e =
383+
readIORef connectedOnce >>= \case
384+
False -> threadDelay 0.1 >> tryConnect connectedOnce (n - 1)
385+
True -> throwIO e
386+
doConnect connectedOnce
387+
`catches` [ Handler $ retryOrThrow (Proxy @IOException)
388+
, Handler $ retryOrThrow (Proxy @HandshakeException)
389+
]
384390

385391
doConnect connectedOnce = runClient "127.0.0.1" (4_000 + hydraNodeId) "/" $ \connection -> do
386392
atomicWriteIORef connectedOnce True

0 commit comments

Comments
 (0)
Please sign in to comment.