-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
initial base_url companion support + proxy companion #5266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
companion = CONFIG.invidious_companion.sample | ||
next make_client(companion.private_url, use_http_proxy: false) | ||
client = make_client(companion.private_url, use_http_proxy: false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is leaking a client
client = make_client(companion.private_url, use_http_proxy: false) |
response = wrapper.client.post(companion_base_url + endpoint, headers: headers, body: data.to_json) | ||
response_body = response.body |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON.parse
can actually accept an IO as an argument. It'll be more efficient to stream the request and parse the JSON as data comes in.
You should be able to do something like this:
wrapper.client.post("#{companion_base_url}#{endpoint}", headers: headers, body: data.to_json) do | response |
response_body = JSON.parse(response.body_io)
end
|
||
if response.status_code != 200 | ||
raise Exception.new( | ||
"Error while communicating with Invidious companion: " \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is duplicating the error message twice when it'd be appended to the InfoException
below
"Error while communicating with Invidious companion: " \ |
|
||
def initialize(companion : Config::CompanionConfig) | ||
@companion = companion | ||
@client = HTTP::Client.new(companion.private_url) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@client = HTTP::Client.new(companion.private_url) | |
@client = make_client(companion.private_url, use_http_proxy: false) |
@@ -0,0 +1,37 @@ | |||
module Invidious::Routes::Companion |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the Accept-Encoding
header to each companion request as so you aren't decompressing the body. Invidious should just directly proxy the compressed response from companion to the end user.
https://crystal-lang.org/api/1.16.3/HTTP/Client.html
If compress [
HTTP::Client
instance property] isn't set to false, and no Accept-Encoding header is explicitly specified, an HTTP::Client will add an "Accept-Encoding": "gzip, deflate" header, and automatically decompress the response body/body_io.
@@ -194,10 +194,14 @@ module Invidious::Routes::Watch | |||
|
|||
if CONFIG.invidious_companion.present? | |||
invidious_companion = CONFIG.invidious_companion.sample |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced with invidious_companion_urls
invidious_companion = CONFIG.invidious_companion.sample |
rescue ex | ||
conn.close | ||
wrapper.client.close |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrapper.client.close | |
wrapper.close |
|
||
companion = CONFIG.invidious_companion.sample | ||
conn = make_client(companion.private_url, use_http_proxy: false) | ||
client = make_client(companion.private_url, use_http_proxy: false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
client = make_client(companion.private_url, use_http_proxy: false) |
@@ -46,8 +46,22 @@ struct YoutubeConnectionPool | |||
end | |||
end | |||
|
|||
class CompanionWrapper |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there's a reason for this to be a class.
class CompanionWrapper | |
struct CompanionWrapper |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think you can also document this type? It wasn't immediately obvious to me what this wrapper was used for. Maybe something like:
# Packages a `HTTP::Client` to an invidious-companion instance alongside the configuration for that instance.
#
# This is used as the resource for the `CompanionPool` as to allow the ability to
# query companion instances hosted on a subpath
struct CompanionWrapper
|
||
COMPANION_POOL.client do |wrapper| | ||
companion_base_url = wrapper.companion.private_url.path | ||
puts "Using companion: #{wrapper.companion.private_url}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug puts
puts "Using companion: #{wrapper.companion.private_url}" |
Explained what I want to implement: iv-org/invidious-companion#118 (comment)
And support proxying the requests to invidious companion from invidious: #5215 in order to ease the deployment of invidious + companion.
Fixes #5215
TODO