Permalink
Browse files

Performance improvement (#269)

* Update deps

* Fix typo

* Moved is_live check to a extra thread

Start a new thread in is_live? check every 5 sec
to reduce load on heroku and the streaming server.
  • Loading branch information...
fliiiix authored and rb2k committed Sep 10, 2018
1 parent db76e29 commit d17676d3082e23a1daf2b3e36b8484df8d01d77c
Showing with 55 additions and 26 deletions.
  1. +6 −6 Gemfile.lock
  2. +31 −20 serious/lib/serious.rb
  3. +18 −0 tests/smoke_test.rb
View
@@ -27,14 +27,14 @@ GEM
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
ruby_dep (~> 1.2)
mustermann (1.0.2)
power_assert (1.1.2)
public_suffix (3.0.2)
puma (3.11.4)
mustermann (1.0.3)
power_assert (1.1.3)
public_suffix (3.0.3)
puma (3.12.0)
rack (2.0.5)
rack-protection (2.0.3)
rack
rack-test (1.0.0)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rake (12.3.1)
rb-fsevent (0.10.3)
@@ -82,4 +82,4 @@ RUBY VERSION
ruby 2.5.1p57
BUNDLED WITH
1.16.2
1.16.4
View
@@ -17,15 +17,38 @@
puts "CUSTOM"
class Background
def self.update_is_live
stream_response = false
begin
#create connection
Net::HTTP.start('stream.radiotux.de', 8000, {read_timeout: 5, open_timeout: 5}) {|http|
http.read_timeout = 5
http.open_timeout = 5
response = http.get('/status.xsl')
#get data
stream_response = response.body.include? "binaergewitter.mp3"
}
rescue Exception => e
stream_response = false
end
stream_response
end
end
class Serious < Sinatra::Base
# worker thread to update stream status
$stream_response = false
set :articles, Proc.new { File.join(Dir.getwd, 'articles') }
set :pages, Proc.new { File.join(Dir.getwd, 'pages') }
set :static, true # Required to serve static files, see http://www.sinatrarb.com/configuration.html
set :static_cache_control, [:public, :max_age => 21600]
set :future, true
set :stream_response_time, Time.now - 20 # setting a time where a cache update is needed
set :stream_response, nil
set :stream_response_time, Time.now - 140 # setting a time where a cache update is needed
not_found do
erb :"404"
@@ -103,25 +126,13 @@ def catetory_archive_path(category)
end
def is_live?
# update the cached response every ~360 sec
if Time.now - settings.stream_response_time > 360
begin
#create connection
Net::HTTP.start('stream.radiotux.de', 8000, {read_timeout: 5, open_timeout: 5}) {|http|
http.read_timeout = 5
http.open_timeout = 5
response = http.get('/status.xsl')
#get data
settings.stream_response = response.body.include? "binaergewitter.mp3"
settings.stream_response_time = Time.now
}
rescue Exception => e
settings.stream_response = false
if Time.now - settings.stream_response_time > 120
settings.stream_response_time = Time.now
Thread.new do
$stream_response = Background.update_is_live
end
end
settings.stream_response
$stream_response
end
end
@@ -188,7 +199,7 @@ def is_live?
@selected_audio_codec = audio_format
@current_url = request.url
# If our current page is filled, ther is probably a next one too
# If our current page is filled, there is probably a next one too
# If it isn't, we'll serve an empty page... for now
if @articles.size == feed_size
parsed_uri = URI(request.url)
View
@@ -116,4 +116,22 @@ def test_that_all_episodes_can_be_downloaded
end
WebMock.disable_net_connect!
end
def test_the_podcast_is_live
stub_request(:get, "http://stream.radiotux.de:8000/status.xsl")
.to_return(:status => 200, :body => "binaergewitter.mp3")
assert_equal(true, Background.update_is_live)
end
def test_the_podcast_is_not_live
stub_request(:head, "http://stream.radiotux.de:8000/binaergewitter.mp3").to_return(:status => [404, "404 - The file you requested could not be found"])
assert_equal(false, Background.update_is_live)
end
def test_the_server_timeout
stub_request(:head, "http://stream.radiotux.de:8000/binaergewitter.mp3").to_timeout
assert_equal(false, Background.update_is_live)
end
end

0 comments on commit d17676d

Please sign in to comment.