Last active
August 30, 2019 15:17
-
-
Save mashingan/7532bd89c4e3bde6526a896a3ae611b7 to your computer and use it in GitHub Desktop.
Revisions
-
mashingan revised this gist
Aug 30, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: poll(100) except: discard method returnConn(p: Pool, id: int) {.base.} = -
mashingan revised this gist
May 17, 2019 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 try: pool(100) except: discard method returnConn(p: Pool, id: int) {.base.} = p.available.addFirst id -
mashingan revised this gist
May 17, 2019 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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]) else: discard sleepAsync 100 -
mashingan created this gist
May 17, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)