Yesod launched the site slowly to release a connection to the database pool

UPDATE

I simplified the demonstration of this with the actual project created on the scaffold - you can check it here: https://github.com/tetigi/yesod-bug-test

Follow the readme to set up the repo and repeat the problem! Thanks:)

ORIGINAL MAIL

Recently, I tried to create a simple website using yesod - in one specific handler, it makes a couple of runDB calls (selecting and inserting some values ​​into DB-200 elements). However, with an average load, for example, when a page loads quickly in a browser, the page starts to freeze.

Performing some debugging, I found that the daod application does not release connections to the database pool in a timely manner and ultimately waits for them to exit. To confirm this, I found the following:

  • Reducing the database pool to 2 gave me freeze after a few clicks
  • The default value (10) is delayed approximately 5 seconds after being pressed
  • Increasing the database pool to 100 gave me a much longer click, up to 10-15 seconds of quick click
  • The problem is the same whether I use postgres or sqlite
  • In postgres, you could see "COMMIT" transactions folding over time
  • These transactions will eventually disappear over time, and the website will react again.

Is there something I am missing here? The web page does not do anything complicated, as the snippet below will be shown. Any ideas? Be that as it may, the website will not be accessible to multiple users until I find a way to fix it!

I use a standard kernel application using the stack, as recommended in the documentation.

Hooray!

Luke

Sample handler code (abbreviated)

getCompareR :: Handler Html getCompareR = do -- Get all entities from the db. Throws error if < 2 elems in the DB. entities <- fmap (\xs -> assert (length xs >= 2) xs) $ runDB $ selectList [] [] -- Pick an entity at random Entity _ thisThingEntity <- liftIO $ runRVar (choice entities) DevRandom -- Pull out everything NOT the thing we just picked otherEntities <- runDB $ selectList [ComparisonHash !=. (comparisonHash thisThingEntity)] [] -- Pick one at random Entity _ thatThingEntity <- liftIO $ runRVar (choice otherEntities) DevRandom -- Some stuff including some inserts -- ... -- ... runDB $ sequence [update thisId [ComparisonElo =. thisElo], update thatId [ComparisonElo =. thatElo]] -- Start laying out the webpage defaultLayout $ do -- Fill in the rest with compare.hamlet $(widgetFile "compare") 
+6
source share
1 answer

The problem is Data.Random - replacing the choice call with something like:

 import System.Random (randomRIO) ... -- Pick an entity at random randomInt1 <- liftIO $ randomRIO (0, length entities -1) let Entity _ thisThingEntity = entities !! randomInt1 

Everything is fixed, and we stopped slowing down. Not quite sure why Data.Random does this, but at least it works now!

Another interesting thing - the problem is not present in Mac OS X, only on Linux accessories (CentOS, Arch, Ubuntu are the ones we tried)

+3
source

All Articles