ruby_rate_limiter 0.1.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
+ SHA256:
3
+ metadata.gz: 440ed0f666d00afdb77283021ab3805b4fa18a08e5a5aea371b04d7b6d6cf299
4
+ data.tar.gz: 75309c235f17894fbe85bc034dcfb2126431d421e3c11ef089879fcedc280355
5
+ SHA512:
6
+ metadata.gz: 9ae4e96315c08a5e84cadc97260e0a1c08f9f201e5ef1ecbb9a235718370007bbb69d9f8625103f97ab2868a000403350503c754f2ae984b0b088e93ec6626d1
7
+ data.tar.gz: 2377e14dd62b12f9e3640781b3be5561a0d6ef0e84301a4d2b52c49928ec2d9f89ac9b510ecb9e467527b34a2bd26c51cf18c132b739ead39e25aae2ba21c663
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # RateLimiter
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ 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/rate_limiter`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ 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.
26
+
27
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rate_limiter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/rate_limiter/blob/main/CODE_OF_CONDUCT.md).
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the RateLimiter project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rate_limiter/blob/main/CODE_OF_CONDUCT.md).
@@ -0,0 +1,13 @@
1
+ module RubyRateLimiter
2
+ module Storage
3
+ class AbstractStorage
4
+ def get(_key)
5
+ raise NotImplementedError, 'You must implement the get method'
6
+ end
7
+
8
+ def set(_key, _value)
9
+ raise NotImplementedError, 'You must implement the set method'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ require 'redis'
2
+ require_relative 'abstract_storage'
3
+
4
+ module RubyRateLimiter
5
+ module Storage
6
+ class RedisStorage < AbstractStorage
7
+ def initialize
8
+ @redis = Redis.new
9
+ end
10
+
11
+ def get(key)
12
+ @redis.get(key)
13
+ end
14
+
15
+ def set(key, value)
16
+ @redis.set(key, value)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,61 @@
1
+ require 'forwardable'
2
+ require_relative 'storage/abstract_storage'
3
+ require_relative 'storage/redis_storage'
4
+
5
+ module RubyRateLimiter
6
+ class TokenBucket
7
+ extend Forwardable
8
+
9
+ DEFAULT_BUCKET_SIZE = 10
10
+ DEFAULT_REFILL_RATE = 1
11
+
12
+ def_delegators :@storage, :get, :set
13
+
14
+ def initialize(user_id, bucket_size: DEFAULT_BUCKET_SIZE, refill_rate: DEFAULT_REFILL_RATE, storage: RateLimiter::Storage::RedisStorage.new)
15
+ @user_id = user_id
16
+ @bucket_size = bucket_size
17
+ @refill_rate = refill_rate
18
+ @storage = storage
19
+ end
20
+
21
+ def allow_request?
22
+ refill_tokens
23
+ tokens = get_bucket_size
24
+ return false if tokens < 1
25
+
26
+ update_bucket_size(tokens - 1)
27
+ true
28
+ end
29
+
30
+ private
31
+
32
+ def get_bucket_size
33
+ (@storage.get("#{@user_id}_tokens") || @bucket_size).to_i
34
+ end
35
+
36
+ def get_last_refill_time
37
+ (@storage.get("#{@user_id}_last_refill") || Time.now.to_f).to_f
38
+ end
39
+
40
+ def update_bucket_size(tokens)
41
+ @storage.set("#{@user_id}_tokens", tokens)
42
+ end
43
+
44
+ def update_last_refill_time(timestamp)
45
+ @storage.set("#{@user_id}_last_refill", timestamp)
46
+ end
47
+
48
+ def refill_tokens
49
+ current_time = Time.now.to_f
50
+ last_refill_time = get_last_refill_time
51
+ elapsed_time = current_time - last_refill_time
52
+
53
+ new_tokens = (elapsed_time * @refill_rate).to_i
54
+ return if new_tokens <= 0
55
+
56
+ tokens = [get_bucket_size + new_tokens, @bucket_size].min
57
+ update_bucket_size(tokens)
58
+ update_last_refill_time(current_time)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RateLimiter
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ruby_rate_limiter/version"
4
+
5
+ module RubyRateLimiter
6
+ class Error < StandardError; end
7
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_rate_limiter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mutuba
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-07-15 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
+ description: A Ruby gem for rate limiting using the token bucket algorithm.
28
+ email:
29
+ - danielmutubait@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - lib/ruby_rate_limiter.rb
36
+ - lib/ruby_rate_limiter/storage/abstract_storage.rb
37
+ - lib/ruby_rate_limiter/storage/redis_storage.rb
38
+ - lib/ruby_rate_limiter/token_bucket.rb
39
+ - lib/ruby_rate_limiter/version.rb
40
+ homepage: https://github.com/Mutuba/rate-limiter
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ allowed_push_host: https://rubygems.org
45
+ homepage_uri: https://github.com/Mutuba/rate-limiter
46
+ changelog_uri: https://github.com/Mutuba/limiter/blob/main/CHANGELOG.md
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.6.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.4.10
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Rate Limiter using Token Bucket Algorithm
66
+ test_files: []