Skip to content

Instantly share code, notes, and snippets.

@mashingan
Last active August 30, 2019 15:17
Show Gist options
  • Save mashingan/7532bd89c4e3bde6526a896a3ae611b7 to your computer and use it in GitHub Desktop.
Save mashingan/7532bd89c4e3bde6526a896a3ae611b7 to your computer and use it in GitHub Desktop.

Revisions

  1. mashingan revised this gist Aug 30, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion poolconn.nim
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ method getConn(p: Pool): Future[(int, AsyncHttpClient)] {.base, async.} =
    return (id, p.conns[id])
    else:
    #discard sleepAsync 100
    try: pool(100)
    try: poll(100)
    except: discard

    method returnConn(p: Pool, id: int) {.base.} =
  2. mashingan revised this gist May 17, 2019. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion poolconn.nim
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,9 @@ method getConn(p: Pool): Future[(int, AsyncHttpClient)] {.base, async.} =
    let id = p.available.popLast
    return (id, p.conns[id])
    else:
    discard sleepAsync 100
    #discard sleepAsync 100
    try: pool(100)
    except: discard

    method returnConn(p: Pool, id: int) {.base.} =
    p.available.addFirst id
  3. mashingan revised this gist May 17, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion poolconn.nim
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,8 @@ type
    method getConn(p: Pool): Future[(int, AsyncHttpClient)] {.base, async.} =
    while true:
    if p.available.len != 0:
    let id = p.available.popLast return (id, p.conns[id])
    let id = p.available.popLast
    return (id, p.conns[id])
    else:
    discard sleepAsync 100

  4. mashingan created this gist May 17, 2019.
    46 changes: 46 additions & 0 deletions poolconn.nim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    import std/[deques, asyncdispatch, httpclient, tables, math, times]

    type
    Pool = ref object
    conns: TableRef[int, AsyncHttpClient]
    available: Deque[int]

    method getConn(p: Pool): Future[(int, AsyncHttpClient)] {.base, async.} =
    while true:
    if p.available.len != 0:
    let id = p.available.popLast return (id, p.conns[id])
    else:
    discard sleepAsync 100

    method returnConn(p: Pool, id: int) {.base.} =
    p.available.addFirst id

    proc newPool(size: int): Pool =
    new result
    result.conns = newTable[int, AsyncHttpClient](size)
    result.available = initDeque[int](size)
    for i in 0 ..< size:
    result.conns[i] = newAsyncHttpClient()
    result.available.addLast i

    proc ops(pool: Pool, conn: AsyncHttpClient, id, count: int): Future[string] {.async.} =
    result = await conn.getContent("http://localhost:3000/" & $count)
    pool.returnConn id

    proc main {.async.} =
    let opsSize = 8
    var pool = newPool((opsSize div 2).nextPowerOfTwo)
    var clientops = newseq[Future[string]]()
    #var clientops = newseq[string]()
    for i in 1 .. opsSize:
    echo "ops: ", i
    let (id, conn) = await pool.getConn
    #clientops.add(await pool.ops(conn, id, i))
    clientops.add(pool.ops(conn, id, i))
    #echo await pool.ops(conn, id, i)

    discard await all(clientops)

    let start = cpuTime()
    waitFor main()
    echo "ended after: ", cpuTime() - start
    12 changes: 12 additions & 0 deletions server.nim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    import std/[asynchttpserver, asyncdispatch]


    var server = newAsyncHttpServer()
    proc cb(req: Request) {.async.} =
    echo "got request of path: ", req.url.path
    discard sleepAsync(500) # to emulate slow operation
    let responsetext = "Hello 異世界" & req.url.path
    await req.respond(Http200, responsetext)

    echo "serving on port 3000"
    waitFor server.serve(Port 3000, cb)