mirror of
https://github.com/zedeus/nitter
synced 2026-09-05 22:59:31 +00:00
Add http pool to reduce connection overhead
This commit is contained in:
parent
a180e5649c
commit
3bd0488c66
5 changed files with 62 additions and 20 deletions
37
src/http_pool.nim
Normal file
37
src/http_pool.nim
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import asyncdispatch, httpclient
|
||||
|
||||
type
|
||||
HttpPool* = ref object
|
||||
conns*: seq[AsyncHttpClient]
|
||||
|
||||
var maxConns {.threadvar.}: int
|
||||
|
||||
let keepAlive* = newHttpHeaders({
|
||||
"Connection": "Keep-Alive"
|
||||
})
|
||||
|
||||
proc setMaxHttpConns*(n: int) =
|
||||
maxConns = n
|
||||
|
||||
proc release*(pool: HttpPool; client: AsyncHttpClient) =
|
||||
if pool.conns.len >= maxConns:
|
||||
client.close()
|
||||
elif client != nil:
|
||||
pool.conns.insert(client)
|
||||
|
||||
template use*(pool: HttpPool; heads: HttpHeaders; body: untyped): untyped =
|
||||
var c {.inject.}: AsyncHttpClient
|
||||
|
||||
if pool.conns.len == 0:
|
||||
c = newAsyncHttpClient(headers=heads)
|
||||
else:
|
||||
c = pool.conns.pop()
|
||||
c.headers = heads
|
||||
|
||||
try:
|
||||
body
|
||||
except ProtocolError:
|
||||
# Twitter closed the connection, retry
|
||||
body
|
||||
finally:
|
||||
pool.release(c)
|
||||
Loading…
Reference in a new issue