redis_rate_limit 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4bb561742b30caf0fde6c6483febf005b1977d81
4
+ data.tar.gz: 0616bfb579d95deaf434aa1f845d4a6de5e729c4
5
+ SHA512:
6
+ metadata.gz: e7706879fcdc0731ba444d8420a9b19d157cf87eda6b3521b558ea09b7314bb72d42fc051a19e90636ca47d94809067f00e5b98d6e4c649e829d8df1b2a02675
7
+ data.tar.gz: f0865113f790f8f519e62c70749af4e7526b6c062e7b139f8a358d18bb6ac27e97f54c81fc1cd152d3150a9025c9413b6318952ee72dbca0e5ecca4c8fc7e1ce
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --drb
2
+ --format Fuubar
3
+ --color
data/.simplecov ADDED
@@ -0,0 +1,3 @@
1
+ SimpleCov.start do
2
+ add_group "lib", "lib"
3
+ end
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ sudo: false
2
+ script: "bundle exec rspec"
3
+ language: ruby
4
+ rvm:
5
+ - 2.3.0
6
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redis_rate_limit.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Hacene Boubguel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # RedisRateLimit
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/redis_rate_limit`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'redis_rate_limit'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install redis_rate_limit
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/redis_rate_limit.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "redis_rate_limit"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,16 @@
1
+ module RedisRateLimit
2
+ class Daily < Period
3
+
4
+ # Create an instance of Daily
5
+ # @param [String] name A unique namespace that identify the subject to track : users, emails, ip ...
6
+ # @param [Hash] options Options hash
7
+ # @option options [Integer] :limit (86_400) How many transactions to perform during the defined interval
8
+ # @option options [Redis] :redis (nil) Redis client
9
+ # @return [Daily] Daily instance
10
+ def initialize(name, options = {})
11
+ super(name, options.merge({format: '%Y-%m-%d', interval: 86_400}))
12
+ end
13
+
14
+ end
15
+ end
16
+
@@ -0,0 +1,16 @@
1
+ module RedisRateLimit
2
+ class Hourly < Period
3
+
4
+ # Create an instance of Hourly
5
+ # @param [String] name A unique namespace that identify the subject to track : users, emails, ip ...
6
+ # @param [Hash] options Options hash
7
+ # @option options [Integer] :limit (3600) How many transactions to perform during the defined interval
8
+ # @option options [Redis] :redis (nil) Redis client
9
+ # @return [Hourly] Hourly instance
10
+ def initialize(name, options = {})
11
+ super(name, options.merge({format: '%Y-%m-%dT%H', interval: 3600}))
12
+ end
13
+
14
+ end
15
+ end
16
+
@@ -0,0 +1,16 @@
1
+ module RedisRateLimit
2
+ class Minute < Period
3
+
4
+ # Create an instance of Minute
5
+ # @param [String] name A unique namespace that identify the subject to track : users, emails, ip ...
6
+ # @param [Hash] options Options hash
7
+ # @option options [Integer] :limit (60) How many transactions to perform during the defined interval
8
+ # @option options [Redis] :redis (nil) Redis client
9
+ # @return [Minute] Minute instance
10
+ def initialize(name, options = {})
11
+ super(name, options.merge({format: '%Y-%m-%dT%H:%M', interval: 60}))
12
+ end
13
+
14
+ end
15
+ end
16
+
@@ -0,0 +1,84 @@
1
+ module RedisRateLimit
2
+ class Period
3
+
4
+ # Create an instance of Period.
5
+ # @param [String] name A unique namespace that identify the subject to track : users, emails, ip ...
6
+ # @param [Hash] options Options hash
7
+ # @option options [String] :format ('%Y-%m-%dT%H:%M') Formated date that represents the time span to track
8
+ # @option options [Integer] :interval (60) The duration in seconds before the next reset
9
+ # @option options [Integer] :limit (60) How many transactions to perform during the defined interval
10
+ # @option options [Redis] :redis (nil) Redis client
11
+ # @return [Period] Period instance
12
+ def initialize(name, options = {})
13
+ @ratelimit_name = "rate_limit:#{name}"
14
+ @format = options[:format] || '%Y-%m-%dT%H:%M'
15
+ @interval = options[:interval] || 60
16
+ @limit = options[:limit] || @interval
17
+ @redis = options[:redis]
18
+ end
19
+
20
+ # Get an access if the rate limit is not exeeded
21
+ # @param [String] client The value of the subject to track : 1234, foo@bar.com, 127.0.0.1
22
+ # @return [Hash] Access ticket
23
+ def get_access(client)
24
+ current_time = Time.now
25
+ range = current_time.strftime(@format)
26
+ current_counter = counter(client)
27
+ reset = current_time.to_i + @interval - (current_time.to_i % @interval)
28
+ unless current_counter < @limit
29
+ return {
30
+ "pass" => false,
31
+ "RateLimit" => {"X-RateLimit-Limit" => @limit, "X-RateLimit-Remaining" => 0, "X-RateLimit-Reset" => reset}
32
+ }
33
+ end
34
+ counter = @redis.hincrby(key(client), range, 1)
35
+ remaining = @limit - counter
36
+ return {
37
+ "pass" => true,
38
+ "RateLimit" => {"X-RateLimit-Limit" => @limit, "X-RateLimit-Remaining" => remaining, "X-RateLimit-Reset" => reset}
39
+ }
40
+ end
41
+
42
+ # Get an access if the rate limit is not exeeded or wait few seconds
43
+ # @param [String] client The value of the subject to track : 1234, foo@bar.com, 127.0.0.1
44
+ # @return [Hash] Access ticket
45
+ def get_access_or_wait(client)
46
+ result = nil
47
+ result = get_access(client)
48
+ return result if result["pass"]
49
+ time_to_sleep = result["RateLimit"]["X-RateLimit-Reset"] - Time.now.to_i
50
+ sleep(time_to_sleep)
51
+ get_access(client)
52
+ end
53
+
54
+ # Get the access count for a given client within the current time range
55
+ # @param [String] client The value of the subject to track : 1234, foo@bar.com, 127.0.0.1
56
+ # @return [Integer] access count
57
+ def counter(client)
58
+ range = Time.now.strftime(@format)
59
+ @redis.hget(key(client), range).to_i
60
+ end
61
+
62
+ # Get access history for a given client
63
+ # @param [String] client The value of the subject to track : 1234, foo@bar.com, 127.0.0.1
64
+ # @return [Array] Array of hash. key : time range, value : access counter
65
+ def history(client)
66
+ @redis.hgetall(key(client))
67
+ end
68
+
69
+ # Clean access history
70
+ # @param [String] client The value of the subject to track : 1234, foo@bar.com, 127.0.0.1
71
+ # @return [Integer] access count
72
+ def clear(client)
73
+ @redis.expire(key(client), 0)
74
+ end
75
+
76
+ private
77
+
78
+ def key(client)
79
+ "#{@ratelimit_name}:#{client}"
80
+ end
81
+
82
+ end
83
+ end
84
+
@@ -0,0 +1,3 @@
1
+ module RedisRateLimit
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ require "redis"
2
+ require "redis_rate_limit/version"
3
+ require "redis_rate_limit/period"
4
+ require "redis_rate_limit/daily"
5
+ require "redis_rate_limit/hourly"
6
+ require "redis_rate_limit/minute"
7
+
8
+ module RedisRateLimit
9
+ # Your code goes here...
10
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redis_rate_limit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "redis_rate_limit"
8
+ spec.version = RedisRateLimit::VERSION
9
+ spec.authors = ["neceha-bgl"]
10
+ spec.email = ["neceha.bgl@gmail.com"]
11
+
12
+ spec.summary = %q{Rate Limit controler based on Redis}
13
+ spec.description = %q{Controle the number of requests permitted to make per minute/hour/day}
14
+ spec.homepage = "https://github.com/neceha-bgl/redis_rate_limit"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "redis"
25
+ spec.add_development_dependency "fakeredis", "~> 0.3.0"
26
+ spec.add_development_dependency "activesupport"
27
+ spec.add_development_dependency "pry"
28
+ spec.add_development_dependency "fuubar"
29
+ spec.add_development_dependency "timecop"
30
+ spec.add_development_dependency "bundler", "~> 1.13"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ spec.add_development_dependency "simplecov"
34
+ spec.add_development_dependency "yard"
35
+ end
metadata ADDED
@@ -0,0 +1,216 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_rate_limit
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - neceha-bgl
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fakeredis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: fuubar
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: timecop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.13'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.13'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '10.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '10.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: yard
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: Controle the number of requests permitted to make per minute/hour/day
168
+ email:
169
+ - neceha.bgl@gmail.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - ".rspec"
176
+ - ".simplecov"
177
+ - ".travis.yml"
178
+ - Gemfile
179
+ - LICENSE.txt
180
+ - README.md
181
+ - Rakefile
182
+ - bin/console
183
+ - bin/setup
184
+ - lib/redis_rate_limit.rb
185
+ - lib/redis_rate_limit/daily.rb
186
+ - lib/redis_rate_limit/hourly.rb
187
+ - lib/redis_rate_limit/minute.rb
188
+ - lib/redis_rate_limit/period.rb
189
+ - lib/redis_rate_limit/version.rb
190
+ - redis_rate_limit.gemspec
191
+ homepage: https://github.com/neceha-bgl/redis_rate_limit
192
+ licenses:
193
+ - MIT
194
+ metadata: {}
195
+ post_install_message:
196
+ rdoc_options: []
197
+ require_paths:
198
+ - lib
199
+ required_ruby_version: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - ">="
202
+ - !ruby/object:Gem::Version
203
+ version: '0'
204
+ required_rubygems_version: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ requirements: []
210
+ rubyforge_project:
211
+ rubygems_version: 2.5.1
212
+ signing_key:
213
+ specification_version: 4
214
+ summary: Rate Limit controler based on Redis
215
+ test_files: []
216
+ has_rdoc: