<div dir="ltr">I'd like a configurable network connect timeout. Has anyone solved this reasonably?<div><br></div><div><b>System.Timeout.timeout</b> times out connections, but works on the granularity of the life of the connection. Is there a reasonable way to configure a timeout around just establishing the connection? Maybe something like a conditional timeout that enables an action to disable the timeout once it expires?<br></div><div><br></div><div>As a workaround, I'm spinning trying to successfully connect first before trying to connect for real:</div><div><br></div><div><div><font face="monospace, monospace">-- | Try the TCP connection and see if you can connect...</font></div><div><font face="monospace, monospace">--</font></div><div><font face="monospace, monospace">tryTCPClient :: Int -> ClientSettings -> IO ()</font></div><div><font face="monospace, monospace">tryTCPClient microseconds settings = do</font></div><div><font face="monospace, monospace">  ok <- newIORef False</font></div><div><font face="monospace, monospace">  void $ timeout microseconds $ runTCPClient settings $ const $</font></div><div><font face="monospace, monospace">    writeIORef ok True</font></div><div><font face="monospace, monospace">  ok' <- readIORef ok</font></div><div><font face="monospace, monospace">  unless ok' $</font></div><div><font face="monospace, monospace">    tryTCPClient microseconds settings</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">-- | Wrap runTCPClient with a connect timeout.</font></div><div><font face="monospace, monospace">--</font></div><div><font face="monospace, monospace">-- Tries the TCP connection first, and the runs the regular runTCPClient.</font></div><div><font face="monospace, monospace">-- Of course, this only enforces a TCP connect timeout on the first connect.</font></div><div><font face="monospace, monospace">-- The second TCP connect has no timeout :(</font></div><div><font face="monospace, monospace">--</font></div><div><font face="monospace, monospace">runTCPClient' :: Int -> ClientSettings -> (AppData -> IO a) -> IO a</font></div><div><font face="monospace, monospace">runTCPClient' microseconds settings action = do</font></div><div><font face="monospace, monospace">  tryTCPClient microseconds settings</font></div><div><font face="monospace, monospace">  runTCPClient settings action</font></div></div><div><br></div><div>I've also tried running the <b>tryTCPClient</b> in its own thread concurrently with <b>runTCPClient</b> and throwing an exception if it can't connect in <b>microseconds</b>. None of these offer an actual true connection establishment timeout.</div><div><br></div><div>Has anyone found a way to solve this? Thanks!</div><div><br></div><div>Mark</div><div><br></div><div><br></div></div>