Skip to content

Commit

Permalink
Merge pull request #237 from cachix/connection-timeout
Browse files Browse the repository at this point in the history
Client: default to 30s for connection timeout
  • Loading branch information
domenkozar committed Dec 27, 2023
2 parents 680a5e0 + 499e759 commit b76337d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/Network/WebSockets/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Data.IORef (newIORef)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Network.Socket as S
import System.Timeout (timeout)


--------------------------------------------------------------------------------
Expand Down Expand Up @@ -75,10 +76,12 @@ runClientWith host port path0 opts customHeaders app = do
S.setSocketOption sock S.NoDelay 1

-- Connect WebSocket and run client
res <- finally
(S.connect sock (S.addrAddress addr) >>
runClientWithSocket sock fullHost path opts customHeaders app)
(S.close sock)
res <- bracket
(timeout (connectionTimeout opts * 1000 * 1000) $ S.connect sock (S.addrAddress addr))
(const $ S.close sock) $ \maybeConnected -> case maybeConnected of
Nothing -> throwIO $ ConnectionTimeout
Just () -> runClientWithSocket sock fullHost path opts customHeaders app


-- Clean up
return res
Expand Down
4 changes: 4 additions & 0 deletions src/Network/WebSockets/Connection/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ data ConnectionOptions = ConnectionOptions
{ connectionOnPong :: !(IO ())
-- ^ Whenever a 'pong' is received, this IO action is executed. It can be
-- used to tickle connections or fire missiles.
, connectionTimeout :: !Int
-- ^ Timeout for connection establishment in seconds. Only used in the client.
, connectionCompressionOptions :: !CompressionOptions
-- ^ Enable 'PermessageDeflate'.
, connectionStrictUnicode :: !Bool
Expand Down Expand Up @@ -59,9 +61,11 @@ data ConnectionOptions = ConnectionOptions
-- * Nothing happens when a pong is received.
-- * Compression is disabled.
-- * Lenient unicode decoding.
-- * 30 second timeout for connection establishment.
defaultConnectionOptions :: ConnectionOptions
defaultConnectionOptions = ConnectionOptions
{ connectionOnPong = return ()
, connectionTimeout = 30
, connectionCompressionOptions = NoCompression
, connectionStrictUnicode = False
, connectionFramePayloadSizeLimit = mempty
Expand Down
2 changes: 2 additions & 0 deletions src/Network/WebSockets/Http.hs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ data HandshakeException
-- | The request was well-formed, but the library user rejected it.
-- (e.g. "unknown path")
| RequestRejected RequestHead ResponseHead
-- | The connection timed out
| ConnectionTimeout
-- | for example "EOF came too early" (which is actually a parse error)
-- or for your own errors. (like "unknown path"?)
| OtherHandshakeException String
Expand Down

0 comments on commit b76337d

Please sign in to comment.