From 226762daf63c69812e8ae83499c847ebce29ef8b Mon Sep 17 00:00:00 2001 From: Jarrod Funnell Date: Sat, 19 Dec 2020 19:39:18 +1100 Subject: [PATCH 1/2] Add basic connection cache --- src/mangadex/downloader.cr | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/mangadex/downloader.cr b/src/mangadex/downloader.cr index e2babb69..504845a1 100644 --- a/src/mangadex/downloader.cr +++ b/src/mangadex/downloader.cr @@ -4,7 +4,7 @@ require "compress/zip" module MangaDex class PageJob property success = false - property url : String + property url : URI property filename : String property writer : Compress::Zip::Writer property tries_remaning : Int32 @@ -17,6 +17,7 @@ module MangaDex @wait_seconds : Int32 = Config.current.mangadex["download_wait_seconds"] .to_i32 @retries : Int32 = Config.current.mangadex["download_retries"].to_i32 + @client_cache = {} of String => HTTP::Client use_default @@ -77,7 +78,7 @@ module MangaDex fn, url = tuple ext = File.extname fn fn = "#{i.to_s.rjust len, '0'}#{ext}" - page_job = PageJob.new url, fn, writer, @retries + page_job = PageJob.new URI.parse(url), fn, writer, @retries Logger.debug "Downloading #{url}" loop do sleep @wait_seconds.seconds @@ -149,8 +150,9 @@ module MangaDex headers = HTTP::Headers{ "User-agent" => "Mangadex.cr", } + begin - HTTP::Client.get job.url, headers do |res| + cached_client(job.url).get job.url.full_path, headers do |res| unless res.success? raise "Failed to download page #{job.url}. " \ "[#{res.status_code}] #{res.status_message}" @@ -163,5 +165,10 @@ module MangaDex job.success = false end end + + private def cached_client(url : URI) + Logger.debug "getting client for #{url.host}" + return @client_cache[url.host.not_nil!]? || (@client_cache[url.host.not_nil!] = HTTP::Client.new(url)) + end end end From 6f11135f8b6bb1528fe3ba3eb02322086f533c52 Mon Sep 17 00:00:00 2001 From: Jarrod Funnell Date: Sat, 19 Dec 2020 21:43:37 +1100 Subject: [PATCH 2/2] Fix lint issue --- src/mangadex/downloader.cr | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mangadex/downloader.cr b/src/mangadex/downloader.cr index 504845a1..ce158df7 100644 --- a/src/mangadex/downloader.cr +++ b/src/mangadex/downloader.cr @@ -167,8 +167,7 @@ module MangaDex end private def cached_client(url : URI) - Logger.debug "getting client for #{url.host}" - return @client_cache[url.host.not_nil!]? || (@client_cache[url.host.not_nil!] = HTTP::Client.new(url)) + @client_cache[url.host.not_nil!]? || (@client_cache[url.host.not_nil!] = HTTP::Client.new(url)) end end end