cheatfate / asyncpg

Asynchronous PostgreSQL driver for Nim language
MIT License
59 stars 6 forks source link

If making more requests then in a pool, odd errors happen. #9

Open treeform opened 6 years ago

treeform commented 6 years ago

Pool size of 10 works, pool size of 5 does not.

import asyncdispatch, asyncpg/asyncpg

const POOL_SIZE = 5
var pool = newPool(POOL_SIZE)

proc main() {.async.} =
  var start: float
  var connStr = "host=localhost port=5432 dbname=test user="
  await pool.connect(connStr)
  var futures = newSeq[Future[apgResult]]()
  for i in 0..<10:
    futures.add exec(pool, "SELECT $1, pg_sleep(1);", i)
  var results = await all(futures)
  for res in results:
    for item in res[0].rows():
      echo item, " "

waitFor main()

Starts 10 connections that sleep for 10 seconds. If pool size is less the number of connections strange error happens:

asyncpgtest.nim(18)      asyncpgtest
asyncdispatch.nim(1654)  waitFor
asyncdispatch.nim(1514)  poll
asyncdispatch.nim(1233)  runOnce
Error: unhandled exception: No handles or timers registered in dispatcher. [ValueError]

I think if connections are not available it should error out or wait some how. I think it just forgets to register no available connection case.

Nim Compiler Version 0.19.1 [MacOSX: amd64]
Compiled at 2018-10-22
Copyright (c) 2006-2018 by Andreas Rumpf

git hash: 16c3d4332fac56b275ca3c66dcf12573738bdc91
active boot switches: -d:release
treeform commented 4 years ago

I ended up writing my own async pg lib that does not have this issue here: https://github.com/treeform/pg