【发布时间】:2019-04-09 06:37:32
【问题描述】:
我有以下Scotty 应用程序,它尝试使用 STM 来保持服务的 API 调用计数:
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Web.Scotty
import Data.Monoid (mconcat)
import Control.Concurrent.STM
import Control.Monad.IO.Class
main :: IO ()
main = do
counter <- newTVarIO 0
scotty 3000 $
get "/:word" $ do
liftIO $ atomically $ do
counter' <- readTVar counter
writeTVar counter (counter' + 1)
liftIO $ do
counter' <- atomically (readTVar counter)
print counter'
beam <- param "word"
html $ mconcat ["<h1>Scotty, ", beam, " me up!</h1>"]
我像这样“加载测试”API:
ab -c 100 -n 100000 http://127.0.0.1:3000/z
但是,API 服务大约 16,000 个请求,然后“卡住” - ab 停止并出现错误 apr_socket_recv: Operation timed out (60)。
我认为我在滥用 STM,但不确定我做错了什么。有什么建议吗?
【问题讨论】:
-
服务器“卡住”时使用了多少 CPU?