Temporary fix to Twitter's global timeline error

This commit is contained in:
Zed 2022-01-21 09:17:18 +01:00
parent d77d5911b0
commit cdb4efadfe
3 changed files with 31 additions and 16 deletions

View file

@ -58,11 +58,17 @@ 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:
var resp: AsyncResponse let resp = await client.get($url)
result = pool.use(genHeaders(token)): result = await resp.body
resp = await c.get($url)
await resp.body if resp.status == $Http503:
badClient = true
raise newException(InternalError, result)
if result.len > 0: if result.len > 0:
if resp.headers.getOrDefault("content-encoding") == "gzip": if resp.headers.getOrDefault("content-encoding") == "gzip":
@ -83,6 +89,8 @@ 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

View file

@ -1,5 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
import asyncdispatch, httpclient import httpclient
type type
HttpPool* = ref object HttpPool* = ref object
@ -17,20 +17,22 @@ proc setHttpProxy*(url: string; auth: string) =
else: else:
proxy = nil proxy = nil
proc release*(pool: HttpPool; client: AsyncHttpClient) = proc release*(pool: HttpPool; client: AsyncHttpClient; badClient=false) =
if pool.conns.len >= maxConns: if pool.conns.len >= maxConns or badClient:
client.close() try: client.close()
except: discard
elif client != nil: elif client != nil:
pool.conns.insert(client) pool.conns.insert(client)
template use*(pool: HttpPool; heads: HttpHeaders; body: untyped): untyped = proc acquire*(pool: HttpPool; heads: HttpHeaders): AsyncHttpClient =
var c {.inject.}: AsyncHttpClient
if pool.conns.len == 0: if pool.conns.len == 0:
c = newAsyncHttpClient(headers=heads, proxy=proxy) result = newAsyncHttpClient(headers=heads, proxy=proxy)
else: else:
c = pool.conns.pop() result = pool.conns.pop()
c.headers = heads result.headers = heads
template use*(pool: HttpPool; heads: HttpHeaders; body: untyped): untyped =
let c {.inject.} = pool.acquire(heads)
try: try:
body body

View file

@ -45,10 +45,15 @@ proc fetchTimeline*(after: string; query: Query; skipRail=false):
else: else:
rail = getCachedPhotoRail(name) rail = getCachedPhotoRail(name)
# var timeline =
# case query.kind
# of posts: await getTimeline(profileId, after)
# of replies: await getTimeline(profileId, after, replies=true)
# of media: await getMediaTimeline(profileId, after)
# else: await getSearch[Tweet](query, after)
var timeline = var timeline =
case query.kind case query.kind
of posts: await getTimeline(profileId, after)
of replies: await getTimeline(profileId, after, replies=true)
of media: await getMediaTimeline(profileId, after) of media: await getMediaTimeline(profileId, after)
else: await getSearch[Tweet](query, after) else: await getSearch[Tweet](query, after)