Simon Marlow在 Haskell eXchange 2012 上做了一个高性能并发演讲。由于时间限制,他跳过了关于简单并发聊天服务器的部分。对省略的内容感到好奇,通过网络搜索在服务器应用程序和GitHub 上的实现中发现了类似的幻灯片。
幻灯片 33读取
回话……
talk :: Server -> Handle -> IO () talk server@Server{..} handle = do hSetNewlineMode handle universalNewlineMode hSetBuffering handle LineBuffering readName where readName = do hPutStrLn handle "What is your name?" name <- hGetLine handle m <- checkAddClient server name handle case m of Nothing -> do hPrintf handle "The name %s is in use" name readName Just client -> do runClient server client `finally` removeClient server name
严格来说,我们应该堵住和之间的洞
checkAddClient
(finally
见注释……)
早些时候,幻灯片 3提到了“笔记中的第 14 章”,我认为这是指他即将出版的书。checkAddClient
和之间的同步裂缝是什么finally
,我们如何堵塞它?
上述实现使用mask
来自 Control.Exception。如果这是解决方法,那么不合时宜的异常会破坏聚会的情况是什么?
... readName = do hPutStrLn handle "What is your name?" name <- hGetLine handle if null name then readName else mask $ \restore -> do ok <- checkAddClient server name handle case ok of Nothing -> restore $ do hPrintf handle "The name %s is in use, please choose another\n" name readName Just client -> restore (runClient server client) `finally` removeClient server name