redis_rate_limiter 0.0.9 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/redis_rate_limiter.rb +12 -4
- metadata +19 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 131cc6a6a6693dd690435dc804387a1f3bd82f28e7d3ff75b0ebe8d3ca23645f
|
4
|
+
data.tar.gz: d996d586a2676b65dc68eaefde091f217d697715629f13ef8a5abcf486490978
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8744a7a0947b3a242d7a220fcb0f6b06cb0202699b8bea34727f9e8ab65f00abbe7b7caf3c6b46454c773e22d2d3586cbc553dd1bf1f365d86fc16b4e0d25ec
|
7
|
+
data.tar.gz: 0a8d6010cd67df6215d89e0d98f6b6f8264b1baba4c171528ceadeba955dfe9e043c05029e201585b38f75a06c74951272485b0528f0e3ea18b87888c8aad1e7
|
data/lib/redis_rate_limiter.rb
CHANGED
@@ -32,10 +32,10 @@ class RedisRateLimiter
|
|
32
32
|
# @param [time] time UNIX timestamp of event
|
33
33
|
def add subject, time = Time.now.to_f
|
34
34
|
subject_key = "#{@key}:#{subject}"
|
35
|
-
@redis.multi do
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
@redis.multi do |pipeline|
|
36
|
+
pipeline.lpush(subject_key, time)
|
37
|
+
pipeline.ltrim(subject_key, 0, @limit - 1)
|
38
|
+
pipeline.expire(subject_key, @interval)
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
@@ -60,6 +60,14 @@ class RedisRateLimiter
|
|
60
60
|
elapsed > @interval ? 0.0 : @interval - elapsed
|
61
61
|
end
|
62
62
|
|
63
|
+
# Get number of events currently recorded for subject
|
64
|
+
#
|
65
|
+
# @param [String] subject Name which uniquely identifies subject
|
66
|
+
# @return [Integer] Returns number of events currently recorded for subject
|
67
|
+
def count subject
|
68
|
+
@redis.llen("#{@key}:#{subject}")
|
69
|
+
end
|
70
|
+
|
63
71
|
private
|
64
72
|
|
65
73
|
def time_since_oldest subject_key
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean Xie
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 4.6.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 4.6.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -108,6 +108,20 @@ dependencies:
|
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: timecop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
111
125
|
description: Redis-backed rate limiter
|
112
126
|
email: sean@tellapart.com
|
113
127
|
executables: []
|
@@ -134,10 +148,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
148
|
- !ruby/object:Gem::Version
|
135
149
|
version: '0'
|
136
150
|
requirements: []
|
137
|
-
|
138
|
-
rubygems_version: 2.2.2
|
151
|
+
rubygems_version: 3.0.3.1
|
139
152
|
signing_key:
|
140
153
|
specification_version: 4
|
141
154
|
summary: Redis-backed rate limiter
|
142
155
|
test_files: []
|
143
|
-
has_rdoc:
|