このリポジトリは2023-09-09にアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュ、イシューの作成、プルリクエストはできません。
Nitter-mod/src/http_pool.nim

53 行
1.2 KiB
Nim
Raw パーマリンク 通常表示 履歴

2021-12-27 10:37:38 +09:00
# SPDX-License-Identifier: AGPL-3.0-only
import httpclient
type
HttpPool* = ref object
conns*: seq[AsyncHttpClient]
2022-01-23 10:29:03 +09:00
var
maxConns: int
proxy: Proxy
proc setMaxHttpConns*(n: int) =
maxConns = n
2021-10-01 01:03:07 +09:00
proc setHttpProxy*(url: string; auth: string) =
if url.len > 0:
proxy = newProxy(url, auth)
else:
proxy = nil
proc release*(pool: HttpPool; client: AsyncHttpClient; badClient=false) =
if pool.conns.len >= maxConns or badClient:
try: client.close()
except: discard
elif client != nil:
pool.conns.insert(client)
proc acquire*(pool: HttpPool; heads: HttpHeaders): AsyncHttpClient =
if pool.conns.len == 0:
result = newAsyncHttpClient(headers=heads, proxy=proxy)
else:
result = pool.conns.pop()
result.headers = heads
template use*(pool: HttpPool; heads: HttpHeaders; body: untyped): untyped =
2022-01-23 10:29:03 +09:00
var
c {.inject.} = pool.acquire(heads)
badClient {.inject.} = false
try:
body
except ProtocolError:
# Twitter closed the connection, retry
body
except BadClientError:
# Twitter returned 503, we need a new client
pool.release(c, true)
badClient = false
c = pool.acquire(heads)
body
finally:
2022-01-23 10:29:03 +09:00
pool.release(c, badClient)