Add http pool to reduce connection overhead

This commit is contained in:
Zed
2020-11-07 21:31:03 +01:00
parent a180e5649c
commit 3bd0488c66
5 changed files with 62 additions and 20 deletions

View File

@@ -1,9 +1,11 @@
import httpclient, asyncdispatch, options, times, strutils, uri
import packedjson
import types, tokens, consts, parserutils
import types, tokens, consts, parserutils, http_pool
const rl = "x-rate-limit-"
var pool {.threadvar.}: HttpPool
proc genParams*(pars: openarray[(string, string)] = @[]; cursor="";
count="20"; ext=true): seq[(string, string)] =
result = timelineParams
@@ -18,6 +20,7 @@ proc genParams*(pars: openarray[(string, string)] = @[]; cursor="";
proc genHeaders*(token: Token = nil): HttpHeaders =
result = newHttpHeaders({
"connection": "keep-alive",
"authorization": auth,
"content-type": "application/json",
"x-guest-token": if token == nil: "" else: token.tok,
@@ -29,14 +32,17 @@ proc genHeaders*(token: Token = nil): HttpHeaders =
})
proc fetch*(url: Uri; oldApi=false): Future[JsonNode] {.async.} =
once:
pool = HttpPool()
var token = await getToken()
if token.tok.len == 0:
result = newJNull()
var client = newAsyncHttpClient(headers=genHeaders(token))
let headers = genHeaders(token)
try:
let
resp = await client.get($url)
resp = pool.use(headers): await c.get($url)
body = await resp.body
if body.startsWith('{') or body.startsWith('['):
@@ -54,5 +60,3 @@ proc fetch*(url: Uri; oldApi=false): Future[JsonNode] {.async.} =
except Exception:
echo "error: ", url
result = newJNull()
finally:
client.close()