Add wait for empty connection pool

This commit is contained in:
Omar Roth 2018-01-07 17:18:24 -06:00
parent dfdeb02c7f
commit 60c618942f

View File

@ -7,10 +7,10 @@ require "time"
PG_DB = DB.open "postgres://kemal:kemal@localhost:5432/invidious" PG_DB = DB.open "postgres://kemal:kemal@localhost:5432/invidious"
CONTEXT = OpenSSL::SSL::Context::Client.insecure CONTEXT = OpenSSL::SSL::Context::Client.insecure
POOL = [] of HTTP::Client POOL = [] of HTTP::Client
10.times do 10.times do
POOL << HTTP::Client.new("www.youtube.com", 443, CONTEXT) POOL << HTTP::Client.new("www.youtube.com", 443, CONTEXT)
end end
macro templated(filename) macro templated(filename)
render "src/views/#{{{filename}}}.ecr", "src/views/layout.ecr" render "src/views/#{{{filename}}}.ecr", "src/views/layout.ecr"
@ -71,6 +71,10 @@ end
def fetch_video(id) def fetch_video(id)
# Grab connection from pool # Grab connection from pool
while POOL.empty?
sleep rand(0..10).milliseconds
end
client = POOL.pop client = POOL.pop
# client = HTTP::Client.new("www.youtube.com", 443, CONTEXT) # client = HTTP::Client.new("www.youtube.com", 443, CONTEXT)
@ -86,9 +90,9 @@ def fetch_video(id)
# Return connection to pool # Return connection to pool
POOL << client POOL << client
video = Video.new(id, info, html, Time.now) video = Video.new(id, info, html, Time.now)
return video return video
end end
@ -196,6 +200,10 @@ end
get "/search" do |env| get "/search" do |env|
query = env.params.query["q"] query = env.params.query["q"]
while POOL.empty?
sleep rand(0..10).milliseconds
end
client = POOL.pop client = POOL.pop
html = client.get("https://www.youtube.com/results?q=#{URI.escape(query)}&page=1").body html = client.get("https://www.youtube.com/results?q=#{URI.escape(query)}&page=1").body