Skip to content

Commit

Permalink
fix infinite loop, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismwendt committed Mar 10, 2023
1 parent 26602c0 commit 6412088
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/Network/WebSockets/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
-- Note that in production you want to use a real webserver such as snap or
-- warp.
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
module Network.WebSockets.Server
( ServerApp
, runServer
Expand Down Expand Up @@ -111,22 +110,30 @@ runServerWithOptions opts app = S.withSocketsDo $
bracket
(makeListenSocket (serverHost opts) (serverPort opts))
S.close $ \sock -> do
heartbeat <- newEmptyMVar

let -- Update the connection options to perform a heartbeat whenever a
-- pong is received.
connOpts = (serverConnectionOptions opts)
{ connectionOnPong = tryPutMVar heartbeat () >> connectionOnPong connOpts
}

-- Kills the thread if pong was not received within the grace period.
reaper grace appAsync = timeout (grace * 1000000) (takeMVar heartbeat) >>= \case
Nothing -> appAsync `Async.cancelWith` PongTimeout
Just _ -> reaper grace appAsync
let connOpts = serverConnectionOptions opts

connThread conn = case serverRequirePong opts of
Nothing -> runApp conn connOpts app
Just grace -> runApp conn connOpts app `Async.withAsync` reaper grace
Just grace -> do
heartbeat <- newEmptyMVar

let -- Update the connection options to perform a heartbeat
-- whenever a pong is received.
connOpts' = connOpts
{ connectionOnPong = do
tryPutMVar heartbeat ()
connectionOnPong connOpts
}

-- Kills the thread if a pong was not received within
-- the grace period.
reaper grace appAsync = do
result <- timeout (grace * 1000000) (takeMVar heartbeat)
case result of
Nothing -> appAsync `Async.cancelWith` PongTimeout
Just _ -> reaper grace appAsync

runApp conn connOpts' app `Async.withAsync` reaper grace

mainThread = do
(conn, _) <- S.accept sock
Expand Down

0 comments on commit 6412088

Please sign in to comment.