Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hydra-cluster: Retry connection on HandshakeException #1286

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions hydra-cluster/src/HydraNode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Cardano.BM.Tracing (ToObject)
import CardanoNode (cliQueryProtocolParameters)
import Control.Concurrent.Async (forConcurrently_)
import Control.Concurrent.Class.MonadSTM (modifyTVar', newTVarIO, readTVarIO)
import Control.Exception (IOException)
import Control.Exception (Handler (..), IOException, catches)
import Control.Lens ((?~))
import Control.Monad.Class.MonadAsync (forConcurrently)
import Data.Aeson (Value (..), object, (.=))
Expand All @@ -31,7 +31,7 @@ import Hydra.Network qualified as Network
import Hydra.Options (ChainConfig (..), DirectChainConfig (..), LedgerConfig (..), RunOptions (..), defaultDirectChainConfig, toArgs)
import Network.HTTP.Req (GET (..), HttpException, JsonResponse, NoReqBody (..), POST (..), ReqBodyJson (..), defaultHttpConfig, responseBody, runReq, (/:))
import Network.HTTP.Req qualified as Req
import Network.WebSockets (Connection, receiveData, runClient, sendClose, sendTextData)
import Network.WebSockets (Connection, HandshakeException, receiveData, runClient, sendClose, sendTextData)
import System.FilePath ((<.>), (</>))
import System.IO.Temp (withSystemTempDirectory)
import System.Info (os)
Expand Down Expand Up @@ -369,18 +369,24 @@ withHydraNode' tracer chainConfig workDir hydraNodeId hydraSKey hydraVKeys allNo
, i /= hydraNodeId
]

withConnectionToNode :: Tracer IO HydraNodeLog -> Int -> (HydraClient -> IO a) -> IO a
withConnectionToNode :: forall a. Tracer IO HydraNodeLog -> Int -> (HydraClient -> IO a) -> IO a
withConnectionToNode tracer hydraNodeId action = do
connectedOnce <- newIORef False
tryConnect connectedOnce (200 :: Int)
where
tryConnect connectedOnce n
| n == 0 = failure $ "Timed out waiting for connection to hydra-node " <> show hydraNodeId
| otherwise =
doConnect connectedOnce `catch` \(e :: IOException) -> do
readIORef connectedOnce >>= \case
False -> threadDelay 0.1 >> tryConnect connectedOnce (n - 1)
True -> throwIO e
| otherwise = do
let
retryOrThrow :: forall proxy e. Exception e => proxy e -> e -> IO a
retryOrThrow _ e =
readIORef connectedOnce >>= \case
False -> threadDelay 0.1 >> tryConnect connectedOnce (n - 1)
True -> throwIO e
doConnect connectedOnce
`catches` [ Handler $ retryOrThrow (Proxy @IOException)
, Handler $ retryOrThrow (Proxy @HandshakeException)
]

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