forked from zedeus/nitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_pool.nim
52 lines (44 loc) · 1.19 KB
/
http_pool.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# SPDX-License-Identifier: AGPL-3.0-only
import httpclient
type
HttpPool* = ref object
conns*: seq[AsyncHttpClient]
var
maxConns: int
proxy: Proxy
proc setMaxHttpConns*(n: int) =
maxConns = n
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 =
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:
pool.release(c, badClient)