summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOmar Roth <omarroth@hotmail.com>2018-02-05 19:07:49 -0600
committerOmar Roth <omarroth@hotmail.com>2018-02-05 19:07:49 -0600
commit990bca5af536322bb11a6d783e182855258ccbc6 (patch)
treebb570158b8b7a7fae0e522e4b1276318a03ed120
parent0e704056a17e1af4ea91da1e9f85884101bf1bd9 (diff)
downloadinvidious-990bca5af536322bb11a6d783e182855258ccbc6.tar.gz
invidious-990bca5af536322bb11a6d783e182855258ccbc6.tar.bz2
invidious-990bca5af536322bb11a6d783e182855258ccbc6.zip
Refactor client creation
-rw-r--r--src/helpers.cr7
-rw-r--r--src/invidious.cr15
2 files changed, 10 insertions, 12 deletions
diff --git a/src/helpers.cr b/src/helpers.cr
index 23cab087..27c49c6e 100644
--- a/src/helpers.cr
+++ b/src/helpers.cr
@@ -209,3 +209,10 @@ def rank_videos(db, n)
# Return top
return top[1..n]
end
+
+def make_client(url, context)
+ client = HTTP::Client.new(URL, CONTEXT)
+ client.read_timeout = 10.seconds
+ client.connect_timeout = 10.seconds
+ return client
+end
diff --git a/src/invidious.cr b/src/invidious.cr
index 592028e3..8573d2a5 100644
--- a/src/invidious.cr
+++ b/src/invidious.cr
@@ -48,10 +48,7 @@ CONTEXT.add_options(
OpenSSL::SSL::Options::NO_SSL_V3
)
pool = Deque.new((threads * 1.2 + 1).to_i) do
- client = HTTP::Client.new(URL, CONTEXT)
- client.read_timeout = 10.seconds
- client.connect_timeout = 10.seconds
- client
+ make_client(URL, CONTEXT)
end
# Refresh pool by crawling YT
@@ -74,10 +71,7 @@ threads.times do
end
if rand(300) < 1
- client = HTTP::Client.new(URL, CONTEXT)
- client.read_timeout = 10.seconds
- client.connect_timeout = 10.seconds
- pool << client
+ pool << make_client(URL, CONTEXT)
end
time = Time.now
@@ -87,10 +81,7 @@ threads.times do
video = get_video(id, client, PG_DB)
rescue ex
io << id << " : " << ex << "\n"
- client = HTTP::Client.new(URL, CONTEXT)
- client.read_timeout = 10.seconds
- client.connect_timeout = 10.seconds
- pool << client
+ pool << make_client(URL, CONTEXT)
next
ensure
ids.delete(id)