ruby_rate_limiter 0.1.0 → 0.1.1
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 +4 -4
- data/README.md +87 -26
- data/lib/ruby_rate_limiter/storage/abstract_storage.rb +1 -0
- data/lib/ruby_rate_limiter/storage/redis_storage.rb +3 -2
- data/lib/ruby_rate_limiter/token_bucket.rb +1 -0
- data/lib/ruby_rate_limiter/version.rb +1 -1
- data/lib/ruby_rate_limiter.rb +5 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6d487b84e9e255e4a2fe403fc98961ee09083d7be86a5bc929e9a106e9455b87
|
4
|
+
data.tar.gz: 28bf084c36db3488761ef6ff9014a0a79743a23c78d1085e55188a2d09262217
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ece0c93d76158c3192290bb0eecbcfe09b82b71e23035b8a6616967a3fddd2af174956d72edde13fb9df3bc00fd579d8195c2115ca8586f51dbad0a8771e03d
|
7
|
+
data.tar.gz: ac79381bda71677bd9a0fed4935f3d167b1a840f51ef4656df951c2563a0fe4f81d561cd4d61c5420a13ef375672c596a05baf258dfab1bda3205f91446cf711
|
data/README.md
CHANGED
@@ -1,39 +1,100 @@
|
|
1
|
-
#
|
1
|
+
# Ruby Rate Limiter
|
2
2
|
|
3
|
-
|
3
|
+
`ruby_rate_limiter` is a Ruby gem that implements a rate limiting mechanism using the Token Bucket algorithm. This gem allows you to control the rate of requests from users or clients, which can be useful for API rate limiting, request throttling, and more.
|
4
4
|
|
5
|
-
|
5
|
+
## Features
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
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:
|
7
|
+
- Token Bucket Algorithm: A flexible and efficient algorithm for rate limiting.
|
8
|
+
- Redis Backend: Utilizes Redis for token storage and management.
|
9
|
+
- Customizable Parameters: Configure the bucket size and refill rate to meet your needs.
|
10
|
+
- Supports Time Travel: For easier testing with time manipulation.
|
16
11
|
|
17
|
-
|
18
|
-
|
19
|
-
## Usage
|
20
|
-
|
21
|
-
TODO: Write usage instructions here
|
12
|
+
## Installation
|
22
13
|
|
23
|
-
|
14
|
+
Add `ruby_rate_limiter` to your application's Gemfile:
|
24
15
|
|
25
|
-
|
16
|
+
```ruby
|
17
|
+
gem 'ruby_rate_limiter'
|
26
18
|
|
27
|
-
|
19
|
+
```
|
28
20
|
|
29
|
-
|
21
|
+
Then run:
|
30
22
|
|
31
|
-
|
23
|
+
```ruby
|
24
|
+
bundle install
|
25
|
+
```
|
32
26
|
|
33
|
-
|
27
|
+
Alternatively, you can install the gem directly using:
|
34
28
|
|
35
|
-
|
29
|
+
```ruby
|
30
|
+
gem install ruby_rate_limiter
|
31
|
+
```
|
36
32
|
|
37
|
-
##
|
33
|
+
## Usage
|
38
34
|
|
39
|
-
|
35
|
+
### Configuration
|
36
|
+
|
37
|
+
First, configure the RateLimiter::TokenBucket with a unique identifier for each user and specify the storage backend (e.g., Redis).
|
38
|
+
|
39
|
+
Create a file in the initialize directory
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# config/initializers/ruby_rate_limiter.rb
|
43
|
+
require 'ruby_rate_limiter'
|
44
|
+
require 'redis'
|
45
|
+
|
46
|
+
module RateLimiter
|
47
|
+
def self.bucket
|
48
|
+
@bucket ||= RubyRateLimiter::TokenBucket.new(
|
49
|
+
'user123',
|
50
|
+
storage: RubyRateLimiter::Storage::RedisStorage.new(redis_client),
|
51
|
+
bucket_size: 10, # Optional: specify bucket size
|
52
|
+
refill_rate: 2 # Optional: specify refill rate (tokens per second)
|
53
|
+
)
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def self.redis_client
|
59
|
+
@redis_client ||= Redis.new(url: 'redis://localhost:6379/1')
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
```
|
64
|
+
|
65
|
+
### Usage somewhere, in the controller or better still in a service
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
class SomeController < ApplicationController
|
69
|
+
def some_action
|
70
|
+
if RateLimiter.bucket.allow_request?
|
71
|
+
# Process the request
|
72
|
+
render json: { message: 'Request processed' }
|
73
|
+
else
|
74
|
+
# Rate limit exceeded
|
75
|
+
render status: 429, json: { error: 'Too many requests' }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
class SomeService
|
83
|
+
def perform
|
84
|
+
if RateLimiter.bucket.allow_request?
|
85
|
+
# Process the request
|
86
|
+
# Perform the service logic here
|
87
|
+
else
|
88
|
+
# Rate limit exceeded
|
89
|
+
raise 'Too many requests'
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
```
|
95
|
+
|
96
|
+
Contributing
|
97
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Mutuba/ruby-rate-limiter.
|
98
|
+
|
99
|
+
License
|
100
|
+
The gem is available as open source under the terms of the MIT License.
|
@@ -1,11 +1,12 @@
|
|
1
|
+
# lib/ruby_rate_limiter/storage/redis_storage.rb
|
1
2
|
require 'redis'
|
2
3
|
require_relative 'abstract_storage'
|
3
4
|
|
4
5
|
module RubyRateLimiter
|
5
6
|
module Storage
|
6
7
|
class RedisStorage < AbstractStorage
|
7
|
-
def initialize
|
8
|
-
@redis =
|
8
|
+
def initialize(redis_client = Redis.new)
|
9
|
+
@redis = redis_client
|
9
10
|
end
|
10
11
|
|
11
12
|
def get(key)
|
data/lib/ruby_rate_limiter.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
2
|
+
# lib/ruby_rate_limiter.rb
|
3
3
|
require_relative "ruby_rate_limiter/version"
|
4
|
+
require 'ruby_rate_limiter/token_bucket'
|
5
|
+
require 'ruby_rate_limiter/storage/abstract_storage'
|
6
|
+
require 'ruby_rate_limiter/storage/redis_storage'
|
7
|
+
|
4
8
|
|
5
9
|
module RubyRateLimiter
|
6
10
|
class Error < StandardError; end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_rate_limiter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mutuba
|
@@ -37,13 +37,13 @@ files:
|
|
37
37
|
- lib/ruby_rate_limiter/storage/redis_storage.rb
|
38
38
|
- lib/ruby_rate_limiter/token_bucket.rb
|
39
39
|
- lib/ruby_rate_limiter/version.rb
|
40
|
-
homepage: https://github.com/Mutuba/rate-limiter
|
40
|
+
homepage: https://github.com/Mutuba/ruby-rate-limiter
|
41
41
|
licenses:
|
42
42
|
- MIT
|
43
43
|
metadata:
|
44
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
|
45
|
+
homepage_uri: https://github.com/Mutuba/ruby-rate-limiter
|
46
|
+
changelog_uri: https://github.com/Mutuba/ruby-rate-limiter/blob/main/CHANGELOG.md
|
47
47
|
post_install_message:
|
48
48
|
rdoc_options: []
|
49
49
|
require_paths:
|