From 9a8f1a0b0a961c29d48b192e6edebecbe7cd05ad Mon Sep 17 00:00:00 2001 From: Andrew Zhao Date: Sat, 3 Apr 2021 16:11:35 -0400 Subject: [PATCH] add option to disable quic --- src/invidious.cr | 2 +- src/invidious/helpers/helpers.cr | 1 + src/invidious/helpers/utils.cr | 18 +++++++++++------- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 463a286a..581e0782 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -95,7 +95,7 @@ LOCALES = { "zh-TW" => load_locale("zh-TW"), } -YT_POOL = QUICPool.new(YT_URL, capacity: CONFIG.pool_size, timeout: 2.0) +YT_POOL = YoutubeConnectionPool.new(YT_URL, capacity: CONFIG.pool_size, timeout: 2.0, use_quic: CONFIG.use_quic) # CLI Kemal.config.extra_options do |parser| diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.cr index f6b6acd1..3767cb50 100644 --- a/src/invidious/helpers/helpers.cr +++ b/src/invidious/helpers/helpers.cr @@ -98,6 +98,7 @@ class Config property port : Int32 = 3000 # Port to listen for connections (overrided by command line argument) property host_binding : String = "0.0.0.0" # Host to bind (overrided by command line argument) property pool_size : Int32 = 100 # Pool size for HTTP requests to youtube.com and ytimg.com (each domain has a separate pool of `pool_size`) + property use_quic : Bool = true # Use quic transport for youtube api @[YAML::Field(converter: Preferences::StringToCookies)] property cookies : HTTP::Cookies = HTTP::Cookies.new # Saved cookies in "name1=value1; name2=value2..." format diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index 67f496df..10d4e6b6 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -16,15 +16,15 @@ def add_yt_headers(request) end end -struct QUICPool +struct YoutubeConnectionPool property! url : URI property! capacity : Int32 property! timeout : Float64 - property pool : ConnectionPool(QUIC::Client) + property pool : ConnectionPool(QUIC::Client | HTTP::Client) - def initialize(url : URI, @capacity = 5, @timeout = 5.0) + def initialize(url : URI, @capacity = 5, @timeout = 5.0, use_quic = true) @url = url - @pool = build_pool + @pool = build_pool(use_quic) end def client(region = nil, &block) @@ -50,9 +50,13 @@ struct QUICPool response end - private def build_pool - ConnectionPool(QUIC::Client).new(capacity: capacity, timeout: timeout) do - conn = QUIC::Client.new(url) + private def build_pool(use_quic) + ConnectionPool(QUIC::Client | HTTP::Client).new(capacity: capacity, timeout: timeout) do + if use_quic + conn = QUIC::Client.new(url) + else + conn = HTTP::Client.new(url) + end conn.family = (url.host == "www.youtube.com") ? CONFIG.force_resolve : Socket::Family::INET conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC conn.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com"