diff --git a/src/apiutils.nim b/src/apiutils.nim index 21c25df..087044c 100644 --- a/src/apiutils.nim +++ b/src/apiutils.nim @@ -58,17 +58,15 @@ template fetchImpl(result, fetchBody) {.dirty.} = if token.tok.len == 0: raise rateLimitError() - var - client = pool.acquire(genHeaders(token)) - badClient = false - try: - let resp = await client.get($url) - result = await resp.body + var resp: AsyncResponse + pool.use(genHeaders(token)): + resp = await c.get(url) + result = await resp.body - if resp.status == $Http503: - badClient = true - raise newException(InternalError, result) + if resp.status == $Http503: + badClient = true + raise newException(InternalError, result) if result.len > 0: if resp.headers.getOrDefault("content-encoding") == "gzip": @@ -89,8 +87,6 @@ template fetchImpl(result, fetchBody) {.dirty.} = if "length" notin e.msg and "descriptor" notin e.msg: release(token, invalid=true) raise rateLimitError() - finally: - pool.release(client, badClient=badClient) proc fetch*(url: Uri; api: Api): Future[JsonNode] {.async.} = var body: string diff --git a/src/http_pool.nim b/src/http_pool.nim index a39918b..2037520 100644 --- a/src/http_pool.nim +++ b/src/http_pool.nim @@ -5,8 +5,9 @@ type HttpPool* = ref object conns*: seq[AsyncHttpClient] -var maxConns: int -var proxy: Proxy +var + maxConns: int + proxy: Proxy proc setMaxHttpConns*(n: int) = maxConns = n @@ -32,7 +33,9 @@ proc acquire*(pool: HttpPool; heads: HttpHeaders): AsyncHttpClient = result.headers = heads 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: body @@ -40,4 +43,4 @@ template use*(pool: HttpPool; heads: HttpHeaders; body: untyped): untyped = # Twitter closed the connection, retry body finally: - pool.release(c) + pool.release(c, badClient)