nocoffee_redis_rate_limit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d3812f6d2f2f6dde5af25214c972cc6b5e3d04e30d8021255ebf383e7bd88e35
4
+ data.tar.gz: ddfc5ddbe7d0597ef1b7dfbf3579b63340dc7688e4f4e2cb29d2087a4e2f93bd
5
+ SHA512:
6
+ metadata.gz: 8460e1b07604f68494fc31ee7d2c5bf5cf559cd4b615b1a85ab3ab1b3f4fda7d3dd97b5c8c2d7f84a8c4af8b5f797ff814dc243673195e3ef0bce2693fdbd089
7
+ data.tar.gz: 7401bec585ab4b527c74a85f744ce62d55d6a84dd12e64288867df69a96079640ff1af39d637c7dcbd306ec8d78d0ee09a2bb2600a939b6e764a8703d479b9d9
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg/
data/.rspec_status ADDED
@@ -0,0 +1,7 @@
1
+ example_id | status | run_time |
2
+ ---------------------------------------- | ------ | --------------- |
3
+ ./spec/redis_rate_limit_spec.rb[1:1] | passed | 0.00466 seconds |
4
+ ./spec/redis_rate_limit_spec.rb[1:2:1:1] | passed | 0.01318 seconds |
5
+ ./spec/redis_rate_limit_spec.rb[1:2:2:1] | passed | 0.00462 seconds |
6
+ ./spec/redis_rate_limit_spec.rb[1:2:3:1] | passed | 1.01 seconds |
7
+ ./spec/redis_rate_limit_spec.rb[1:2:4:1] | passed | 2.01 seconds |
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in redis_rate_limit.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nocoffee_redis_rate_limit (0.1.0)
5
+ redis (>= 5)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ connection_pool (2.3.0)
11
+ diff-lcs (1.5.0)
12
+ rake (13.0.6)
13
+ redis (5.0.6)
14
+ redis-client (>= 0.9.0)
15
+ redis-client (0.13.0)
16
+ connection_pool
17
+ rspec (3.12.0)
18
+ rspec-core (~> 3.12.0)
19
+ rspec-expectations (~> 3.12.0)
20
+ rspec-mocks (~> 3.12.0)
21
+ rspec-core (3.12.1)
22
+ rspec-support (~> 3.12.0)
23
+ rspec-expectations (3.12.2)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.12.0)
26
+ rspec-mocks (3.12.3)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.12.0)
29
+ rspec-support (3.12.0)
30
+
31
+ PLATFORMS
32
+ x86_64-darwin-21
33
+
34
+ DEPENDENCIES
35
+ nocoffee_redis_rate_limit!
36
+ rake (~> 13.0)
37
+ rspec (~> 3.0)
38
+
39
+ BUNDLED WITH
40
+ 2.3.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Didier Lafforgue, Julien Girard
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,45 @@
1
+ # RedisRateLimit
2
+
3
+ If an application goes beyond quota defined by public APIs, they might ban it. To overcome this issue, developers have to wrap each call to public APIs and make sure they don't make more requests than allowed.
4
+
5
+ Or in another words, this gem helps you to respect the ratio limit defined by external services like Twilio or others before your account gets suspended.
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
+ ```ruby
26
+ rate_limit = new('Twilio', max_rpm: 60, redis: $redis)
27
+
28
+ rate_limite.safe_call do
29
+ twilio_sdk.send_verification_sms('+123456789')
30
+ end
31
+ ```
32
+
33
+ ## Development
34
+
35
+ 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.
36
+
37
+ 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).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/redis_rate_limit.
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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(__FILE__)
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,3 @@
1
+ module RedisRateLimit
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,41 @@
1
+ require 'redis_rate_limit/version'
2
+
3
+ module RedisRateLimit
4
+ class RateLimit
5
+ DURATION_IN_SECONDS = {
6
+ max_rpd: 60 * 60 * 24,
7
+ max_rph: 60 * 60,
8
+ max_rpm: 60,
9
+ max_rps: 1,
10
+ }
11
+ attr_reader :topic, :limits, :redis
12
+ def initialize(topic, max_rpd: nil, max_rpm: nil, max_rph: nil, max_rps: nil, redis: nil)
13
+ @topic, @redis = topic, redis
14
+ @limits = [[:max_rps, max_rps], [:max_rpm, max_rpm], [:max_rph, max_rph], [:max_rpd, max_rpd]] # order is important
15
+ raise 'You must set the redis option' if @redis.nil?
16
+ raise 'You must set at least a limit (max_rpd, max_rpm, max_rph or max_rps)' if @limits.map(&:last).all?(&:nil?)
17
+ end
18
+ def safe_call(&block)
19
+ @limits.each do |(type, value)|
20
+ next if value.nil? || value == 0
21
+ _topic = "#{topic}-#{type}"
22
+ current = redis.get(_topic).to_i
23
+ if current >= value
24
+ return [:error, "Too many requests (#{type}: #{value})"]
25
+ else
26
+ redis.multi do
27
+ redis.incr(_topic)
28
+ redis.expire(_topic, DURATION_IN_SECONDS[type])
29
+ end
30
+ end
31
+ end
32
+ [:ok, yield]
33
+ end
34
+ def reset
35
+ @limits.each do |(type, _)|
36
+ _topic = "#{topic}-#{type}"
37
+ redis.set(_topic, 0)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 1.17.2
@@ -0,0 +1,32 @@
1
+
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 = 'nocoffee_redis_rate_limit'
8
+ spec.version = RedisRateLimit::VERSION
9
+ spec.authors = ['Didier Lafforgue', 'Julien Girard']
10
+ spec.email = ['didier@nocoffee.fr']
11
+
12
+ spec.summary = %q{This gem is a possible usage of https://redis.io/commands/INCR.}
13
+ spec.description = %q{If an application goes beyond quota defined by public APIs, they might ban it.
14
+ To overcome this issue, developers have to wrap each call to public APIs
15
+ and make sure they don't make more requests than allowed.}
16
+ spec.homepage = "https://github.com/NoCoffeeHQ/rate_limit"
17
+ spec.license = "MIT"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "bin"
25
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency 'rake', '~> 13.0'
29
+ spec.add_development_dependency 'rspec', '~> 3.0'
30
+
31
+ spec.add_dependency 'redis', '>= 5'
32
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nocoffee_redis_rate_limit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Didier Lafforgue
8
+ - Julien Girard
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2023-03-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '13.0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '13.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '3.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '3.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: redis
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '5'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '5'
56
+ description: |-
57
+ If an application goes beyond quota defined by public APIs, they might ban it.
58
+ To overcome this issue, developers have to wrap each call to public APIs
59
+ and make sure they don't make more requests than allowed.
60
+ email:
61
+ - didier@nocoffee.fr
62
+ executables:
63
+ - console
64
+ - setup
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - ".gitignore"
69
+ - ".rspec_status"
70
+ - Gemfile
71
+ - Gemfile.lock
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - bin/console
76
+ - bin/setup
77
+ - lib/redis_rate_limit.rb
78
+ - lib/redis_rate_limit/version.rb
79
+ - redis_rate_limit.gemspec
80
+ - redis_rate_limit/.gitignore
81
+ - redis_rate_limit/.rspec
82
+ - redis_rate_limit/.travis.yml
83
+ homepage: https://github.com/NoCoffeeHQ/rate_limit
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.2.32
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: This gem is a possible usage of https://redis.io/commands/INCR.
106
+ test_files: []