【问题标题】:Getting "stuck" using STM使用 STM 被“卡住”
【发布时间】: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?

标签: haskell stm


【解决方案1】:

在这里快速猜测。 16,000 大约是可用 TCP 端口的数量。有没有可能你没有关闭任何连接,因此ab 的开放端口用完了?

【讨论】:

  • 是的,就是这样!谢谢你!所有这些连接都处于TIME_WAIT 状态。这是否表明 scotty 没有正确关闭连接?
  • 那么解决办法是什么?
  • 似乎wrk(而不是ab)不会导致此问题。
  • @firefrorefiddle,我不太确定。我点击是因为我花了很多时间学习 STM,而不是 Scotty。我知道在打开更多端口之前需要关闭 TCP 端口,但如何做到这一点可能应该在标题中突出显示 Scotty 的新问题中提出。
猜你喜欢
  • 2016-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-17
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
  • 2018-05-16
相关资源
最近更新 更多