Add http pool to reduce connection overhead
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user