Fix client reuse

This commit is contained in:
Omar Roth 2018-03-03 17:45:54 -06:00
parent baeb44be13
commit fc2e10a54f

View file

@ -66,19 +66,19 @@ threads.times do
io = STDOUT io = STDOUT
ids = Deque(String).new ids = Deque(String).new
random = Random.new random = Random.new
client = get_client(youtube_pool) yt_client = get_client(youtube_pool)
search(random.base64(3), client) do |id| search(random.base64(3), yt_client) do |id|
ids << id ids << id
end end
youtube_pool << client youtube_pool << yt_client
loop do loop do
yt_client = get_client(youtube_pool) yt_client = get_client(youtube_pool)
if ids.empty? if ids.empty?
search(random.base64(3), client) do |id| search(random.base64(3), yt_client) do |id|
ids << id ids << id
end end
end end
@ -90,7 +90,7 @@ threads.times do
begin begin
id = ids[0] id = ids[0]
video = get_video(id, client, PG_DB) video = get_video(id, yt_client, PG_DB)
rescue ex rescue ex
io << id << " : " << ex.message << "\n" io << id << " : " << ex.message << "\n"
youtube_pool << make_client(YT_URL, CONTEXT) youtube_pool << make_client(YT_URL, CONTEXT)
@ -118,7 +118,7 @@ threads.times do
sleep sleep_time.seconds sleep sleep_time.seconds
youtube_pool << client youtube_pool << yt_client
end end
end end
end end
@ -129,8 +129,9 @@ threads.times do
client = get_client(reddit_pool) client = get_client(reddit_pool)
begin begin
client.get("/") client.get("/")
rescue ex rescue ex
STDOUT << "Reddit client : " << ex.message << "\n"
reddit_pool << make_client(URI.parse("https://api.reddit.com"), CONTEXT) reddit_pool << make_client(URI.parse("https://api.reddit.com"), CONTEXT)
next next
end end
@ -201,6 +202,8 @@ get "/watch" do |env|
rescue ex rescue ex
error_message = ex.message error_message = ex.message
next templated "error" next templated "error"
ensure
youtube_pool << yt_client
end end
fmt_stream = [] of HTTP::Params fmt_stream = [] of HTTP::Params
@ -253,17 +256,15 @@ get "/watch" do |env|
end end
reddit_client = get_client(reddit_pool) reddit_client = get_client(reddit_pool)
begin begin
reddit_comments, reddit_thread = get_reddit_comments(id, reddit_client) reddit_comments, reddit_thread = get_reddit_comments(id, reddit_client)
rescue ex rescue ex
reddit_comments = JSON.parse("[]") reddit_comments = JSON.parse("[]")
reddit_thread = nil reddit_thread = nil
ensure
reddit_pool << reddit_client
end end
reddit_pool << reddit_client
youtube_pool << yt_client
templated "watch" templated "watch"
end end