dom96 / httpbeast

A highly performant, multi-threaded HTTP 1.1 server written in Nim.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

There was an error when I used `httpbeast` and `asyncmysql` to get MYSQL data.

lihf8515 opened this issue · comments

l use httpbeast and asyncmysql asynchronous connection database to get data. There are no errors in compiling, no errors in running, and the data can be obtained correctly when I access it with a browser. But when I use wrk -c10-d20s -T30s --latency http://192.168.1.2:8080/ for stress tests, the run goes wrong. Error :Error:unhandled exception:/home/lhf/. nimble/pkgs/httpbeast-0.2.2/httpbeast.nim(321,10)getData.headersFinishedSelector not ready to send.[AssertionError]. I don't know if there's a problem with my code or the framework. please help to find the reason. Thank you.
My source code is as follows:

import net,asyncdispatch,asyncmysql
import httpbeast,options

const 
  MysqlHost = "localhost"
  MysqlPort= Port(3306)
  MysqlUser = "root"
  MysqlPassword = "123456"

type
  Replies = seq[tuple[packet: ResultPacket, rows: seq[string]]]

proc newPool*(connectLimit = 10):Future[AsyncMysqlPool] {.async.} =
  result = await openMysqlPool(AF_INET, MysqlPort, MysqlHost, MysqlUser,
    MysqlPassword, "db", connectionLimit = connectLimit)

proc closePool*(pool: AsyncMysqlPool) =
  close(pool)
  
proc dbQuery*(pool: AsyncMysqlPool, q: SqlQuery): Future[Replies] =
  var retFuture = newFuture[Replies]("dbQuery")
  result = retFuture

  proc finishCb(err: ref Exception, replies: Replies) {.async.} =
    if err == nil:
      complete(retFuture, replies)
    else:
      fail(retFuture, err)

  execQuery(pool, q, finishCb)

proc cx():Future[seq[string]] {.async.} =
  var pool = await newPool(2)
  let query = sql("select * from sys_user;")
  let replies = await pool.dbQuery(query)
  closePool(pool)
  result = replies[0].rows

proc onRequest(req: Request): Future[void] {.async.} =
  if req.httpMethod == some(HttpGet):
    case req.path.get()
    of "/":
      let data = await cx()
      const headers = "Content-Type: text/plain; charset=UTF-8"
      req.send(Http200, $data, headers)
    else:
      req.send(Http404)

run(onRequest)