-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add docs for shutting down sockets in a threaded scenario #548
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,44 @@ | |
-- unexpected things would happen. There is one exception for multiple | ||
-- threads vs a single 'Socket': one thread reads data from a 'Socket' | ||
-- only and the other thread writes data to the 'Socket' only. | ||
-- | ||
-- The preferred way to terminate a thread that is blocked on a call to | ||
-- 'Network.Socket.ByteString.recv' is to use 'shutdown' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The immediate question this poses: Why this, instead of just throwing an exception to the thread that's blocked on That would be the most natural to me, as that's how e.g.
Then, if you want your app to do a graceful shutdown, it can do bracket createConnection gracefulClose $ -- ... code using recv ... |
||
-- | ||
-- > import Control.Concurrent (forkFinally, threadDelay) | ||
-- > import qualified Control.Exception as E | ||
-- > import Control.Monad (forever, guard, void) | ||
-- > import Control.Monad.IO.Class (liftIO) | ||
-- > import Control.Monad.Trans.Maybe (runMaybeT) | ||
-- > import qualified Data.ByteString as BS | ||
-- > import Network.Socket | ||
-- > import Network.Socket.ByteString | ||
-- > | ||
-- > main = | ||
-- > let maxQueuedConnections = 3 | ||
-- > gracefulCloseTimeout = 5000 | ||
-- > in do | ||
-- > sock <- socket AF_UNIX Stream defaultProtocol | ||
-- > bind sock (SockAddrUnix "./socket") | ||
-- > listen sock maxQueuedConnections | ||
-- > | ||
-- > E.bracketOnError (accept sock) (\(connectedSock, _) -> close connectedSock) $ | ||
-- > \(connectedSock, _) -> void $ do | ||
-- > forkFinally | ||
-- > (printer connectedSock) | ||
-- > (const $ gracefulClose connectedSock gracefulCloseTimeout >> print "closed") | ||
-- > | ||
-- > threadDelay (5_000_000) | ||
-- > putStrLn "Time's up" | ||
-- > shutdown connectedSock ShutdownBoth | ||
-- > | ||
-- > printer :: Socket -> IO () | ||
-- > printer connectedSock = | ||
-- > -- https://www.haskellforall.com/2012/07/breaking-from-loop.html | ||
-- > void $ runMaybeT $ forever $ do | ||
-- > bytes <- liftIO $ recv connectedSock 4096 | ||
-- > guard $ not (BS.null bytes) | ||
-- > liftIO $ print $ "Got bytes: " <> show bytes | ||
----------------------------------------------------------------------------- | ||
|
||
-- In order to process this file, you need to have CALLCONV defined. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This existing sentence is not good.
unexpected things
, what's that referring to?This needs explanation.