Release client only when it still exists

@pool.release should not be called when the client has already been
deleted from the pool.
This commit is contained in:
syeopite 2024-11-14 16:59:17 -08:00
parent 75eb8b8d7f
commit 621c6abfe6
No known key found for this signature in database
GPG Key ID: A73C186DA3955A1A

View File

@ -51,21 +51,32 @@ module Invidious::ConnectionPool
# Checks out a client in the pool # Checks out a client in the pool
private def client(&) private def client(&)
pool.checkout do |http_client| # If a client has been deleted from the pool
# Proxy needs to be reinstated every time we get a client from the pool # we won't try to release it
http_client.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy client_exists_in_pool = true
response = yield http_client http_client = pool.checkout
return response # Proxy needs to be reinstated every time we get a client from the pool
rescue ex http_client.proxy = make_configured_http_proxy_client() if CONFIG.http_proxy
# Prevent broken client from being checked back into the pool
pool.delete(http_client) response = yield http_client
raise ConnectionPool::Error.new(ex.message, cause: ex)
end
rescue ex : DB::PoolTimeout rescue ex : DB::PoolTimeout
# Failed to checkout a client # Failed to checkout a client
raise ConnectionPool::Error.new(ex.message, cause: ex) raise ConnectionPool::Error.new(ex.message, cause: ex)
rescue ex
# An error occurred with the client itself.
# Delete the client from the pool and close the connection
if http_client
client_exists_in_pool = false
@pool.delete(http_client)
http_client.close
end
# Raise exception for outer methods to handle
raise ConnectionPool::Error.new(ex.message, cause: ex)
ensure
pool.release(http_client) if http_client && client_exists_in_pool
end end
private def build_pool private def build_pool