[Haskell-cafe] Network Connect Timeouts

Mark Fine mark.fine at gmail.com
Sat Oct 14 02:14:33 UTC 2017

I'd like a configurable network connect timeout. Has anyone solved this

*System.Timeout.timeout* 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?

As a workaround, I'm spinning trying to successfully connect first before
trying to connect for real:

-- | Try the TCP connection and see if you can connect...
tryTCPClient :: Int -> ClientSettings -> IO ()
tryTCPClient microseconds settings = do
  ok <- newIORef False
  void $ timeout microseconds $ runTCPClient settings $ const $
    writeIORef ok True
  ok' <- readIORef ok
  unless ok' $
    tryTCPClient microseconds settings

-- | Wrap runTCPClient with a connect timeout.
-- Tries the TCP connection first, and the runs the regular runTCPClient.
-- Of course, this only enforces a TCP connect timeout on the first connect.
-- The second TCP connect has no timeout :(
runTCPClient' :: Int -> ClientSettings -> (AppData -> IO a) -> IO a
runTCPClient' microseconds settings action = do
  tryTCPClient microseconds settings
  runTCPClient settings action

I've also tried running the *tryTCPClient* in its own thread concurrently
with *runTCPClient* and throwing an exception if it can't connect in
*microseconds*. None of these offer an actual true connection establishment

Has anyone found a way to solve this? Thanks!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20171013/9fc978bd/attachment.html>

More information about the Haskell-Cafe mailing list