Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

unixfox
Copy link
Member

@unixfox unixfox commented May 2, 2025

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

  • Allow the ability to omit "public_url" in "invidious_companion" config for activating the built-in proxy in invidious.
  • Fix the built-in proxy
  • Re-review if the way of accessing the chosen invidious_companion created from the COMPANION_POOL is a correct way.

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)
Copy link
Member

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

Suggested change
client = make_client(companion.private_url, use_http_proxy: false)

Comment on lines +704 to +705
response = wrapper.client.post(companion_base_url + endpoint, headers: headers, body: data.to_json)
response_body = response.body
Copy link
Member

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: " \
Copy link
Member

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

Suggested change
"Error while communicating with Invidious companion: " \


def initialize(companion : Config::CompanionConfig)
@companion = companion
@client = HTTP::Client.new(companion.private_url)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@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
Copy link
Member

@syeopite syeopite May 15, 2025

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
Copy link
Member

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

Suggested change
invidious_companion = CONFIG.invidious_companion.sample

rescue ex
conn.close
wrapper.client.close
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
client = make_client(companion.private_url, use_http_proxy: false)

@@ -46,8 +46,22 @@ struct YoutubeConnectionPool
end
end

class CompanionWrapper
Copy link
Member

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.

Suggested change
class CompanionWrapper
struct CompanionWrapper

Copy link
Member

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}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug puts

Suggested change
puts "Using companion: #{wrapper.companion.private_url}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature request] Invidious companion: quality of life improvements
3 participants