Search code examples
haskellstm

Getting "stuck" using STM


I have the following Scotty app which tries to use STM to keep a count of API calls served:

{-# 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>"]

I "load test" the API like this:

ab -c 100 -n 100000 http://127.0.0.1:3000/z

However, the API serves roughly about 16 thousand requests and then gets "stuck" - ab stops with error apr_socket_recv: Operation timed out (60).

I think I'm misusing STM, but not sure what I'm doing wrong. Any suggestions?


Solution

  • Quick guess here. 16,000 is about the number of available TCP ports. Is is possible you have not closed any connections and therefore run out of open ports for ab?