Simplify bad http client pruning
This commit is contained in:
parent
dcfdd225a2
commit
79b98a8081
2 changed files with 14 additions and 15 deletions
|
@ -58,12 +58,10 @@ template fetchImpl(result, fetchBody) {.dirty.} =
|
||||||
if token.tok.len == 0:
|
if token.tok.len == 0:
|
||||||
raise rateLimitError()
|
raise rateLimitError()
|
||||||
|
|
||||||
var
|
|
||||||
client = pool.acquire(genHeaders(token))
|
|
||||||
badClient = false
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
let resp = await client.get($url)
|
var resp: AsyncResponse
|
||||||
|
pool.use(genHeaders(token)):
|
||||||
|
resp = await c.get(url)
|
||||||
result = await resp.body
|
result = await resp.body
|
||||||
|
|
||||||
if resp.status == $Http503:
|
if resp.status == $Http503:
|
||||||
|
@ -89,8 +87,6 @@ template fetchImpl(result, fetchBody) {.dirty.} =
|
||||||
if "length" notin e.msg and "descriptor" notin e.msg:
|
if "length" notin e.msg and "descriptor" notin e.msg:
|
||||||
release(token, invalid=true)
|
release(token, invalid=true)
|
||||||
raise rateLimitError()
|
raise rateLimitError()
|
||||||
finally:
|
|
||||||
pool.release(client, badClient=badClient)
|
|
||||||
|
|
||||||
proc fetch*(url: Uri; api: Api): Future[JsonNode] {.async.} =
|
proc fetch*(url: Uri; api: Api): Future[JsonNode] {.async.} =
|
||||||
var body: string
|
var body: string
|
||||||
|
|
|
@ -5,8 +5,9 @@ type
|
||||||
HttpPool* = ref object
|
HttpPool* = ref object
|
||||||
conns*: seq[AsyncHttpClient]
|
conns*: seq[AsyncHttpClient]
|
||||||
|
|
||||||
var maxConns: int
|
var
|
||||||
var proxy: Proxy
|
maxConns: int
|
||||||
|
proxy: Proxy
|
||||||
|
|
||||||
proc setMaxHttpConns*(n: int) =
|
proc setMaxHttpConns*(n: int) =
|
||||||
maxConns = n
|
maxConns = n
|
||||||
|
@ -32,7 +33,9 @@ proc acquire*(pool: HttpPool; heads: HttpHeaders): AsyncHttpClient =
|
||||||
result.headers = heads
|
result.headers = heads
|
||||||
|
|
||||||
template use*(pool: HttpPool; heads: HttpHeaders; body: untyped): untyped =
|
template use*(pool: HttpPool; heads: HttpHeaders; body: untyped): untyped =
|
||||||
let c {.inject.} = pool.acquire(heads)
|
var
|
||||||
|
c {.inject.} = pool.acquire(heads)
|
||||||
|
badClient {.inject.} = false
|
||||||
|
|
||||||
try:
|
try:
|
||||||
body
|
body
|
||||||
|
@ -40,4 +43,4 @@ template use*(pool: HttpPool; heads: HttpHeaders; body: untyped): untyped =
|
||||||
# Twitter closed the connection, retry
|
# Twitter closed the connection, retry
|
||||||
body
|
body
|
||||||
finally:
|
finally:
|
||||||
pool.release(c)
|
pool.release(c, badClient)
|
||||||
|
|
Loading…
Reference in a new issue