summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/sampling/riot_api.rb11
-rw-r--r--lib/throttled_api_request.rb25
2 files changed, 24 insertions, 12 deletions
diff --git a/lib/sampling/riot_api.rb b/lib/sampling/riot_api.rb
index 7e14551..7d75475 100644
--- a/lib/sampling/riot_api.rb
+++ b/lib/sampling/riot_api.rb
@@ -39,7 +39,11 @@ module Sampling
class Job < ThrottledApiRequest
def initialize(request, args={})
@url = Sampling::RiotApi::url(request, args)
- super(api_name, 10.seconds, 10)
+ limits = [
+ {:unit_time => 10.seconds, :requests_per => 10},
+ {:unit_time => 10.minutes, :requests_per => 500},
+ ]
+ super(api_name, limits)
end
def perform
@@ -153,7 +157,8 @@ module Sampling
def start
@match.teams.each do |team|
team.users.each do |user|
- Delayed::Job.enqueue(MatchJob.new(user, @match, @match.stats_from(self.class), nil), :queue => api_name)
+ #For demo purposes, we are hard coding in a league of legends game id.
+ Delayed::Job.enqueue(MatchJob.new(user, @match, @match.stats_from(self.class), 1362730546), :queue => api_name)
end
end
end
@@ -177,7 +182,7 @@ module Sampling
Delayed::Job.enqueue(MatchJob.new(user, match, data["games"][0]["gameId"]), :queue => api_name)
else
if @last_game_id == data["games"][0]["gameId"]
- # TODO: perhaps insert a delay here?
+ sleep(4.minutes)
Delayed::Job.enqueue(MatchJob.new(user, match, @last_game_id), :queue => api_name)
else
@stats.each do |stat|
diff --git a/lib/throttled_api_request.rb b/lib/throttled_api_request.rb
index 3f30c56..1020071 100644
--- a/lib/throttled_api_request.rb
+++ b/lib/throttled_api_request.rb
@@ -1,18 +1,25 @@
-class ThrottledApiRequest < Struct.new(:api_name, :unit_time, :requests_per)
+# limits is in the format:
+# limits = [
+# {:unit_time => 10.seconds, :requests_per => 10},
+# {:unit_time => 10.minutes, :requests_per => 500},
+# ]
+class ThrottledApiRequest < Struct.new(:api_name, :limits)
def before(job)
loop do
sleep_for = -1
ActiveRecord::Base.transaction do
ApiRequests.create(:api_name => self.api_name)
- recent_requests = ApiRequets.
- where(:api_name => self.api_name).
- where("updated_at > ?", Time.now.utc - self.unit_time).
- order(:updated_at)
- if (recent_requests.count > self.requests_per)
- sleep_for = Time.now.utc - recent_requests[recent_requests.count-self.requests_per].updated_at
+ self.limits.each do |limit|
+ recent_requests = ApiRequets.
+ where(:api_name => self.api_name).
+ where("updated_at > ?", Time.now.utc - limit[:unit_time]).
+ order(:updated_at)
+ if (recent_requests.count > limit[:requests_per])
+ sleep_for = [sleep_for, Time.now.utc - recent_requests[recent_requests.count-limit[:requests_per]].updated_at].max
+ end
+ end
+ if sleep_for != -1
raise ActiveRecord::Rollback
- else
- sleep_for = -1
end
end
if sleep_for != -1