request_meter 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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +20 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +76 -0
- data/Rakefile +12 -0
- data/lib/request_meter/configuration.rb +69 -0
- data/lib/request_meter/middleware.rb +61 -0
- data/lib/request_meter/version.rb +8 -0
- data/lib/request_meter.rb +31 -0
- data/sig/request_meter.rbs +4 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d1b3dd163f8c18bf998e16c82da60cab3a6d945aded55fff5d5e8f64d2dd0b76
|
4
|
+
data.tar.gz: 034aa52d3fa74680282e82a925d9e1eac6c03d5c33b16b0de855fe61b8359c9f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fbc6956b570a7dc13fff0880e517862083aa0adff8f6f3fc32ee5efccfa8c6ac55b1d35796354e28fc1b72bc77d07bb73a92191628ec2670a211632ad68c90b7
|
7
|
+
data.tar.gz: 4d774eb9652b7b8bbb69dd1102c6b9505673c899a8ecb32c8d5b7482c084b39636c8c3716b8265ea64ced32edd4568f8cae6f79015a8fc30ad105cefbd11b221
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 3.1
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
EnforcedStyle: double_quotes
|
6
|
+
|
7
|
+
Style/StringLiteralsInInterpolation:
|
8
|
+
EnforcedStyle: double_quotes
|
9
|
+
|
10
|
+
Style/BlockLength:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
Style/MethodLength:
|
14
|
+
Enabled: false
|
15
|
+
|
16
|
+
Style/Documentation:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Metrics/AbcSize:
|
20
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 mihai9909
|
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,76 @@
|
|
1
|
+
# RequestMeter
|
2
|
+
|
3
|
+
|
4
|
+
RequestMeter is a Rack middleware gem for Rails applications that limits API requests per API key within a specified timeframe.
|
5
|
+
|
6
|
+
It is ideal for Rails APIs wanting built-in request limits.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Install the gem and add to the application's Gemfile by executing:
|
11
|
+
|
12
|
+
```bash
|
13
|
+
bundle add request_meter
|
14
|
+
```
|
15
|
+
|
16
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
17
|
+
|
18
|
+
```bash
|
19
|
+
gem install request_meter
|
20
|
+
```
|
21
|
+
|
22
|
+
## Setup
|
23
|
+
|
24
|
+
Add this line to `config/application.rb`
|
25
|
+
|
26
|
+
<pre lang="ruby">
|
27
|
+
Rails.application.config.middleware.use RequestMeter::Middleware
|
28
|
+
</pre>
|
29
|
+
|
30
|
+
Example `RequestMeter` configuration in `config/initializers/request_meter.rb`:
|
31
|
+
|
32
|
+
|
33
|
+
<pre lang="ruby">
|
34
|
+
RequestMeter.configure do |config|
|
35
|
+
config.api_key_header = "X-API-Key"
|
36
|
+
config.cache_client = Rails.application.config.redis
|
37
|
+
config.quota_limit = -> (api_key) {
|
38
|
+
user = User.find_by(api_key: api_key)
|
39
|
+
user&.quota_limit
|
40
|
+
}
|
41
|
+
config.quota_period_seconds = -> (api_key) {
|
42
|
+
user = User.find_by(api_key: api_key)
|
43
|
+
user&.quota_period_seconds
|
44
|
+
}
|
45
|
+
end
|
46
|
+
</pre>
|
47
|
+
|
48
|
+
|
49
|
+
- `api_key_header` used to get the API key from the request headers
|
50
|
+
- default: `X-API-Key`
|
51
|
+
|
52
|
+
- `quota_limit` maximum number of requests that can be performed in the timeframe `quota_period_seconds` by an API key.
|
53
|
+
- default: `1000`
|
54
|
+
- type: `Proc` or `Integer`
|
55
|
+
|
56
|
+
- `quota_period_seconds` number of seconds in a timeframe
|
57
|
+
- default: `3600`
|
58
|
+
- type: `Proc` or `Integer`
|
59
|
+
|
60
|
+
- `cache_client` Client used to track request counts. **Note: Only has support for Redis**
|
61
|
+
|
62
|
+
The default configuration defines a global limit of `1000 requests per hour`
|
63
|
+
|
64
|
+
For custom behavior (e.g., per-user limits), use a `Proc` to dynamically determine values.
|
65
|
+
|
66
|
+
## Development
|
67
|
+
|
68
|
+
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.
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
Bug reports, pull requests and ideas for improvement are welcome on GitHub at https://github.com/mihai9909/request_meter.
|
73
|
+
|
74
|
+
## License
|
75
|
+
|
76
|
+
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,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RequestMeter
|
4
|
+
# Configuration class holds settings for the RequestMeter middleware.
|
5
|
+
#
|
6
|
+
# It manages API request quota limits, time windows, header keys, and the cache client.
|
7
|
+
#
|
8
|
+
# @example Default configuration
|
9
|
+
# config = RequestMeter::Configuration.new
|
10
|
+
# config.quota_limit #=> 1000
|
11
|
+
# config.quota_period_seconds #=> 3600
|
12
|
+
# config.api_key_header #=> "X-API-Key"
|
13
|
+
#
|
14
|
+
# @api public
|
15
|
+
class Configuration
|
16
|
+
# @return [Integer, Proc] maximum allowed requests per quota period or a Proc returning limit by api_key
|
17
|
+
attr_accessor :quota_limit
|
18
|
+
# @return [Integer, Proc] quota window length in seconds or a Proc returning period by api_key
|
19
|
+
attr_accessor :quota_period_seconds
|
20
|
+
# @return [String] HTTP header name that holds the API key
|
21
|
+
attr_accessor :api_key_header
|
22
|
+
# @return [Object, nil] Cache client (e.g. Redis) used for tracking requests
|
23
|
+
attr_accessor :cache_client
|
24
|
+
|
25
|
+
# Initializes a new Configuration with default values.
|
26
|
+
#
|
27
|
+
# Defaults:
|
28
|
+
# - quota_limit: 1000
|
29
|
+
# - quota_period_seconds: 3600 (1 hour)
|
30
|
+
# - api_key_header: "X-API-Key"
|
31
|
+
# - cache_client: nil
|
32
|
+
#
|
33
|
+
# @return [void]
|
34
|
+
def initialize
|
35
|
+
@quota_limit = 1000
|
36
|
+
@quota_period_seconds = 3600
|
37
|
+
@api_key_header = "X-API-Key"
|
38
|
+
@cache_client = nil
|
39
|
+
end
|
40
|
+
|
41
|
+
# Returns the quota limit for a given API key.
|
42
|
+
#
|
43
|
+
# If quota_limit is a Proc, it is called with the api_key, otherwise returns the fixed limit.
|
44
|
+
#
|
45
|
+
# @param api_key [String] the API key to check
|
46
|
+
# @return [Integer] the quota limit for that API key
|
47
|
+
def get_quota_limit(api_key)
|
48
|
+
if @quota_limit.is_a?(Proc)
|
49
|
+
@quota_limit.call(api_key)
|
50
|
+
else
|
51
|
+
@quota_limit
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Returns the quota period (in seconds) for a given API key.
|
56
|
+
#
|
57
|
+
# If quota_period_seconds is a Proc, it is called with the api_key, otherwise returns the fixed period.
|
58
|
+
#
|
59
|
+
# @param api_key [String] the API key to check
|
60
|
+
# @return [Integer] the quota period in seconds for that API key
|
61
|
+
def get_quota_period_seconds(api_key)
|
62
|
+
if @quota_period_seconds.is_a?(Proc)
|
63
|
+
@quota_period_seconds.call(api_key)
|
64
|
+
else
|
65
|
+
@quota_period_seconds
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rack/request"
|
4
|
+
|
5
|
+
# RequestMeter::Middleware is a Rack middleware for API rate limiting.
|
6
|
+
#
|
7
|
+
# It checks for a configured API key header and limits requests per key using Redis.
|
8
|
+
#
|
9
|
+
# @example Basic usage
|
10
|
+
# use RequestMeter::Middleware
|
11
|
+
#
|
12
|
+
# @api public
|
13
|
+
|
14
|
+
module RequestMeter
|
15
|
+
class Middleware
|
16
|
+
# @param app [#call] the next middleware or Rack app
|
17
|
+
def initialize(app)
|
18
|
+
@app = app
|
19
|
+
@cache_client = RequestMeter.configuration.cache_client
|
20
|
+
|
21
|
+
return unless @cache_client.nil?
|
22
|
+
|
23
|
+
raise MissingCacheClientError, "Cache client is required"
|
24
|
+
end
|
25
|
+
|
26
|
+
# Processes an incoming request, enforces quota limits, and forwards it
|
27
|
+
#
|
28
|
+
# @param env [Hash] Rack environment
|
29
|
+
# @return [Array] Rack response triplet
|
30
|
+
def call(env)
|
31
|
+
req = Rack::Request.new(env)
|
32
|
+
api_key = req.get_header("HTTP_#{RequestMeter.configuration.api_key_header.upcase.gsub("-", "_")}")
|
33
|
+
|
34
|
+
if api_key.nil? || api_key.empty?
|
35
|
+
return [400, { "Content-Type" => "application/json" }, [{ error: "API key is required" }.to_json]]
|
36
|
+
end
|
37
|
+
|
38
|
+
quota_limit = RequestMeter.configuration.get_quota_limit(api_key)
|
39
|
+
quota_period = RequestMeter.configuration.get_quota_period_seconds(api_key)
|
40
|
+
|
41
|
+
if quota_limit.nil? || quota_period.nil?
|
42
|
+
return [400, { "Content-Type" => "application/json" },
|
43
|
+
[{ error: "Limits are not configured for the provided token" }.to_json]]
|
44
|
+
end
|
45
|
+
|
46
|
+
cache_key = "request_meter:#{api_key}"
|
47
|
+
|
48
|
+
current_count = @cache_client.incr(cache_key)
|
49
|
+
|
50
|
+
@cache_client.expire(cache_key, quota_period) if current_count == 1
|
51
|
+
|
52
|
+
if current_count > quota_limit
|
53
|
+
reset_in = @cache_client.ttl(cache_key)
|
54
|
+
headers = { "Retry-After" => reset_in.to_s }
|
55
|
+
return [429, headers, ["Quota exceeded. Try again in #{reset_in} seconds."]]
|
56
|
+
end
|
57
|
+
|
58
|
+
@app.call(env)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "request_meter/version"
|
4
|
+
require "request_meter/middleware"
|
5
|
+
require "request_meter/configuration"
|
6
|
+
|
7
|
+
module RequestMeter
|
8
|
+
class MissingCacheClientError < StandardError; end
|
9
|
+
|
10
|
+
class << self
|
11
|
+
# @return [RequestMeter::Configuration] current configuration instance
|
12
|
+
attr_accessor :configuration
|
13
|
+
end
|
14
|
+
|
15
|
+
# Configure the RequestMeter settings.
|
16
|
+
#
|
17
|
+
# Yields the configuration object to the given block.
|
18
|
+
#
|
19
|
+
# @yieldparam config [RequestMeter::Configuration] the configuration object
|
20
|
+
# @return [void]
|
21
|
+
#
|
22
|
+
# @example
|
23
|
+
# RequestMeter.configure do |config|
|
24
|
+
# config.api_key_header = "X-API-Key"
|
25
|
+
# config.quota_limit = 1000
|
26
|
+
# end
|
27
|
+
def self.configure
|
28
|
+
self.configuration ||= Configuration.new
|
29
|
+
yield(configuration)
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: request_meter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- mihai9909
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: rack
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '0'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: pry
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: rspec
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
type: :development
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
description: "RequestMeter is a Rack middleware gem for Rails applications that tracks
|
55
|
+
and limits\nAPI requests per API key within a specified time window. \n\nIt is ideal
|
56
|
+
for Rails APIs wanting built-in request quota enforcement.\n"
|
57
|
+
email:
|
58
|
+
- neagoimihai@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".rspec"
|
64
|
+
- ".rubocop.yml"
|
65
|
+
- CHANGELOG.md
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/request_meter.rb
|
70
|
+
- lib/request_meter/configuration.rb
|
71
|
+
- lib/request_meter/middleware.rb
|
72
|
+
- lib/request_meter/version.rb
|
73
|
+
- sig/request_meter.rbs
|
74
|
+
homepage: https://github.com/mihai9909/request_meter
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata:
|
78
|
+
homepage_uri: https://github.com/mihai9909/request_meter
|
79
|
+
source_code_uri: https://github.com/mihai9909/request_meter
|
80
|
+
changelog_uri: https://github.com/mihai9909/request_meter/blob/main/CHANGELOG.md
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 3.1.0
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubygems_version: 3.6.7
|
96
|
+
specification_version: 4
|
97
|
+
summary: Middleware to limit per API key requests.
|
98
|
+
test_files: []
|