feat(invidious-companion): use connection pool
All checks were successful
Invidious CI / build (push) Successful in 5m39s
All checks were successful
Invidious CI / build (push) Successful in 5m39s
From https://github.com/iv-org/invidious/pull/4985#discussion_r1889356705 Thanks syeopite!
This commit is contained in:
parent
d8ecfd9c04
commit
b3e24c703d
3 changed files with 44 additions and 3 deletions
|
@ -114,6 +114,10 @@ YT_POOL = YoutubeConnectionPool.new(YT_URL, capacity: CONFIG.pool_size)
|
||||||
|
|
||||||
GGPHT_POOL = YoutubeConnectionPool.new(URI.parse("https://yt3.ggpht.com"), capacity: CONFIG.pool_size)
|
GGPHT_POOL = YoutubeConnectionPool.new(URI.parse("https://yt3.ggpht.com"), capacity: CONFIG.pool_size)
|
||||||
|
|
||||||
|
COMPANION_POOL = CompanionConnectionPool.new(
|
||||||
|
capacity: CONFIG.pool_size
|
||||||
|
)
|
||||||
|
|
||||||
# CLI
|
# CLI
|
||||||
Kemal.config.extra_options do |parser|
|
Kemal.config.extra_options do |parser|
|
||||||
parser.banner = "Usage: invidious [arguments]"
|
parser.banner = "Usage: invidious [arguments]"
|
||||||
|
|
|
@ -46,6 +46,45 @@ struct YoutubeConnectionPool
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
struct CompanionConnectionPool
|
||||||
|
property pool : DB::Pool(HTTP::Client)
|
||||||
|
|
||||||
|
def initialize(capacity = 5, timeout = 5.0)
|
||||||
|
options = DB::Pool::Options.new(
|
||||||
|
initial_pool_size: 0,
|
||||||
|
max_pool_size: capacity,
|
||||||
|
max_idle_pool_size: capacity,
|
||||||
|
checkout_timeout: timeout
|
||||||
|
)
|
||||||
|
|
||||||
|
@pool = DB::Pool(HTTP::Client).new(options) do
|
||||||
|
companion = CONFIG.invidious_companion.sample
|
||||||
|
next make_client(companion.private_url, force_resolve: true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def client(&)
|
||||||
|
conn = pool.checkout
|
||||||
|
# Proxy needs to be reinstated every time we get a client from the pool
|
||||||
|
conn.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy
|
||||||
|
|
||||||
|
begin
|
||||||
|
response = yield conn
|
||||||
|
rescue ex
|
||||||
|
conn.close
|
||||||
|
|
||||||
|
companion = CONFIG.invidious_companion.sample
|
||||||
|
conn = make_client(companion.private_url, force_resolve: true)
|
||||||
|
|
||||||
|
response = yield conn
|
||||||
|
ensure
|
||||||
|
pool.release(conn)
|
||||||
|
end
|
||||||
|
|
||||||
|
response
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def add_yt_headers(request)
|
def add_yt_headers(request)
|
||||||
request.headers.delete("User-Agent") if request.headers["User-Agent"] == "Crystal"
|
request.headers.delete("User-Agent") if request.headers["User-Agent"] == "Crystal"
|
||||||
request.headers["User-Agent"] ||= "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
|
request.headers["User-Agent"] ||= "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
|
||||||
|
|
|
@ -686,9 +686,7 @@ module YoutubeAPI
|
||||||
# Send the POST request
|
# Send the POST request
|
||||||
|
|
||||||
begin
|
begin
|
||||||
invidious_companion = CONFIG.invidious_companion.sample
|
response = COMPANION_POOL.client &.post(endpoint, headers: headers, body: data.to_json)
|
||||||
response = make_client(invidious_companion.private_url, use_http_proxy: false,
|
|
||||||
&.post(endpoint, headers: headers, body: data.to_json))
|
|
||||||
body = response.body
|
body = response.body
|
||||||
if (response.status_code != 200)
|
if (response.status_code != 200)
|
||||||
raise Exception.new(
|
raise Exception.new(
|
||||||
|
|
Loading…
Add table
Reference in a new issue