From dfdeb02c7f8bd511fd8c1a4d850e245b785d74c5 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sun, 7 Jan 2018 13:03:53 -0600 Subject: [PATCH] Try using connection pool for getting videos --- src/invidious.cr | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 6d84b1e1..ca3a40da 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -7,6 +7,10 @@ require "time" PG_DB = DB.open "postgres://kemal:kemal@localhost:5432/invidious" CONTEXT = OpenSSL::SSL::Context::Client.insecure +POOL = [] of HTTP::Client +10.times do + POOL << HTTP::Client.new("www.youtube.com", 443, CONTEXT) +end macro templated(filename) render "src/views/#{{{filename}}}.ecr", "src/views/layout.ecr" @@ -66,7 +70,10 @@ def ci_lower_bound(pos, n) end def fetch_video(id) - client = HTTP::Client.new("www.youtube.com", 443, CONTEXT) + # Grab connection from pool + client = POOL.pop + + # client = HTTP::Client.new("www.youtube.com", 443, CONTEXT) info = client.get("/get_video_info?video_id=#{id}&el=info&ps=default&eurl=&gl=US&hl=en").body info = HTTP::Params.parse(info) @@ -77,8 +84,11 @@ def fetch_video(id) raise info["reason"] end + # Return connection to pool + POOL << client + video = Video.new(id, info, html, Time.now) - + return video end @@ -186,7 +196,8 @@ end get "/search" do |env| query = env.params.query["q"] - client = HTTP::Client.new("www.youtube.com", 443, CONTEXT) + client = POOL.pop + html = client.get("https://www.youtube.com/results?q=#{URI.escape(query)}&page=1").body html = XML.parse(html) @@ -208,6 +219,8 @@ get "/search" do |env| end end + POOL << client + templated "search" end