Skip to content

Commit

Permalink
Add a 60s timeout to disconnect.
Browse files Browse the repository at this point in the history
Fixes #17
  • Loading branch information
barrucadu committed Jul 9, 2016
1 parent c7c2e46 commit b0582d6
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions Network/IRC/Client/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import Control.Applicative ((<$>))
import Control.Concurrent (forkIO)
import Control.Concurrent.STM (atomically, readTVar, retry, writeTVar)
import Control.Exception (SomeException, catch, throwIO)
import Control.Monad (unless)
import Control.Monad (unless, when)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.Trans.Reader (runReaderT)
import Data.ByteString (ByteString)
import Data.Conduit (Producer, Conduit, Consumer, (=$=), ($=), (=$), await, awaitForever, toProducer, yield)
import Data.Conduit.TMChan (closeTBMChan, isEmptyTBMChan, newTBMChanIO, sourceTBMChan, writeTBMChan)
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import Data.Time.Clock (NominalDiffTime, getCurrentTime)
import Data.Time.Clock (NominalDiffTime, addUTCTime, getCurrentTime)
import Data.Time.Format (formatTime)
import Network.IRC.Conduit (IrcEvent, IrcMessage, floodProtector, rawMessage, toByteString)
import Network.IRC.Client.Types
Expand Down Expand Up @@ -208,11 +208,9 @@ disconnect = do
-- Set the state to @Disconnecting@
liftIO . atomically $ writeTVar (_connState s) Disconnecting

-- Wait for all messages to be sent
-- Wait for all messages to be sent, or a minute has passed.
queueS <- _sendqueue <$> connectionConfig
liftIO . atomically $ do
empty <- isEmptyTBMChan queueS
unless empty retry
timeout 60 . atomically $ isEmptyTBMChan queueS

-- Then close the connection
disconnectNow
Expand All @@ -228,3 +226,13 @@ disconnectNow = do

s <- ircState
liftIO . atomically $ writeTVar (_connState s) Disconnected

-- | Block until an action is successful or a timeout is reached.
timeout :: MonadIO m => NominalDiffTime -> IO Bool -> m ()
timeout dt check = liftIO $ do
finish <- addUTCTime dt <$> getCurrentTime
let wait = do
now <- getCurrentTime
cond <- check
when (now < finish && not cond) wait
wait

0 comments on commit b0582d6

Please sign in to comment.