diff options
| author | Omar Roth <omarroth@hotmail.com> | 2018-01-07 17:18:24 -0600 |
|---|---|---|
| committer | Omar Roth <omarroth@hotmail.com> | 2018-01-07 17:18:24 -0600 |
| commit | 60c618942f2a69b2e1ece259680b35994c947b07 (patch) | |
| tree | 74955104985ca8ddb37ce1318cd9853595e3efc2 /src | |
| parent | dfdeb02c7f8bd511fd8c1a4d850e245b785d74c5 (diff) | |
| download | invidious-60c618942f2a69b2e1ece259680b35994c947b07.tar.gz invidious-60c618942f2a69b2e1ece259680b35994c947b07.tar.bz2 invidious-60c618942f2a69b2e1ece259680b35994c947b07.zip | |
Add wait for empty connection pool
Diffstat (limited to 'src')
| -rw-r--r-- | src/invidious.cr | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/src/invidious.cr b/src/invidious.cr index ca3a40da..9ed41865 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -7,10 +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 = [] of HTTP::Client +10.times do POOL << HTTP::Client.new("www.youtube.com", 443, CONTEXT) -end +end macro templated(filename) render "src/views/#{{{filename}}}.ecr", "src/views/layout.ecr" @@ -71,6 +71,10 @@ end def fetch_video(id) # Grab connection from pool + while POOL.empty? + sleep rand(0..10).milliseconds + end + client = POOL.pop # client = HTTP::Client.new("www.youtube.com", 443, CONTEXT) @@ -86,9 +90,9 @@ def fetch_video(id) # Return connection to pool POOL << client - + video = Video.new(id, info, html, Time.now) - + return video end @@ -196,6 +200,10 @@ end get "/search" do |env| query = env.params.query["q"] + while POOL.empty? + sleep rand(0..10).milliseconds + end + client = POOL.pop html = client.get("https://www.youtube.com/results?q=#{URI.escape(query)}&page=1").body |
