redis_rate_limiter 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/redis_rate_limiter.rb +25 -4
- metadata +33 -1
data/lib/redis_rate_limiter.rb
CHANGED
@@ -1,11 +1,24 @@
|
|
1
1
|
require 'redis'
|
2
2
|
|
3
3
|
class RedisRateLimiter
|
4
|
+
|
5
|
+
# @!attribute key
|
6
|
+
# @return [String] Name which uniquely identifies this rate limiter
|
7
|
+
# @!attribute redis
|
8
|
+
# @return [Redis] Redis client associated with this rate limiter
|
9
|
+
# @!attribute interval
|
10
|
+
# @return [Integer] Time span this rate limiter tracks in seconds
|
11
|
+
# @!attribute limit
|
12
|
+
# @return [Integer] Max count allowed by rate limiter in interval
|
4
13
|
attr_accessor :key, :redis, :limit, :interval
|
5
14
|
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
15
|
+
# Initializes a new RedisRateLimiter object
|
16
|
+
#
|
17
|
+
# @param [String] key A name to uniquely identify this rate limiter
|
18
|
+
# @param [Redis] redis Redis client associated with rate limiter
|
19
|
+
# @param options [Integer] :interval Time span to track in seconds
|
20
|
+
# @param options [Integer] :limit Max count allowed in interval
|
21
|
+
# @return [RedisRateLimiter] Instance of this rate limiter
|
9
22
|
def initialize key, redis, options = {}
|
10
23
|
@key = key
|
11
24
|
@redis = redis
|
@@ -13,6 +26,10 @@ class RedisRateLimiter
|
|
13
26
|
@interval = options[:interval] || 60
|
14
27
|
end
|
15
28
|
|
29
|
+
# Add to subject's count
|
30
|
+
#
|
31
|
+
# @param [String] subject A name to uniquely identify subject
|
32
|
+
# @param [time] time UNIX timestamp of event
|
16
33
|
def add subject, time = Time.now.to_i
|
17
34
|
subject = "#{@key}:#{subject}"
|
18
35
|
@redis.multi do
|
@@ -22,10 +39,14 @@ class RedisRateLimiter
|
|
22
39
|
end
|
23
40
|
end
|
24
41
|
|
42
|
+
# Check if subject has exceeded count
|
43
|
+
#
|
44
|
+
# @param [String] subject Name which uniquely identifies subject
|
45
|
+
# @return [Boolean] Returns true if subject has exceeded count
|
25
46
|
def exceeded? subject
|
26
47
|
subject = "#{@key}:#{subject}"
|
27
48
|
return false if @redis.llen(subject) < @limit
|
28
|
-
last = @redis.
|
49
|
+
last = @redis.lindex(subject, -1)
|
29
50
|
Time.now.to_i - last.to_i < @interval
|
30
51
|
end
|
31
52
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis_rate_limiter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -75,6 +75,38 @@ dependencies:
|
|
75
75
|
- - ! '>='
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: coveralls
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: yard
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
78
110
|
description: Redis-backed rate limiter
|
79
111
|
email: sean.xie.sx@gmail.com
|
80
112
|
executables: []
|