implement api limiting
This commit is contained in:
@@ -164,6 +164,10 @@ class AnonymousUser
|
||||
def enable_sequential_post_navigation
|
||||
true
|
||||
end
|
||||
|
||||
def api_hourly_limit
|
||||
500
|
||||
end
|
||||
|
||||
%w(member banned privileged builder platinum contributor janitor moderator admin).each do |name|
|
||||
define_method("is_#{name}?") do
|
||||
|
||||
9
app/logical/api_limiter.rb
Normal file
9
app/logical/api_limiter.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
module ApiLimiter
|
||||
def throttled?(ip_addr)
|
||||
key = "#{ip_addr}:#{Time.now.hour}"
|
||||
MEMCACHE.fetch(key, 1.hour, true) {0}
|
||||
MEMCACHE.incr(key).to_i > CurrentUser.user.api_hourly_limit
|
||||
end
|
||||
|
||||
module_function :throttled?
|
||||
end
|
||||
@@ -1,15 +1,11 @@
|
||||
class Cache
|
||||
def self.incr(key, expiry = 0)
|
||||
val = Cache.get(key, expiry)
|
||||
Cache.put(key, val.to_i + 1)
|
||||
def self.incr(key)
|
||||
MEMCACHE.incr(key)
|
||||
ActiveRecord::Base.logger.debug('MemCache Incr %s' % [key])
|
||||
end
|
||||
|
||||
def self.decr(key, expiry = 0)
|
||||
val = Cache.get(key, expiry)
|
||||
if val.to_i > 0
|
||||
Cache.put(key, val.to_i - 1)
|
||||
end
|
||||
def self.decr(key)
|
||||
MEMCACHE.decr(key)
|
||||
ActiveRecord::Base.logger.debug('MemCache Decr %s' % [key])
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user