limiter-ruby 0.1.7 → 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -34
- data/lib/limiter/client.rb +22 -15
- data/lib/limiter/version.rb +1 -1
- data/lib/limiter.rb +0 -1
- data/limiter.gemspec +3 -3
- metadata +9 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ec1028bbed03695ce7f6722da569723cbe4dbe54a692c8b3b161dde3aa9b3e7
|
4
|
+
data.tar.gz: 954bf67ede37fad7012f0b82e375901db14e2ed7f63ca5b7421f286810cba977
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e5b789e9516d90cf049e2027b9bac3c0967ccac67445ba5a1856cfcc21ef22e5d0b63a2dc56da3bc055c2d0a4cece9291638ee42655ad579de39ad68af30d16
|
7
|
+
data.tar.gz: 4599777458c083480cc0aff6c2a181e7043090d445f1b514cea6afa68a91df1188ad8fca58bf9b8b26e9e4966d635ce806dfaf829b27226102bd6f39fe2386d5
|
data/README.md
CHANGED
@@ -25,49 +25,24 @@ gem "limiter-ruby"
|
|
25
25
|
Assuming this is a Ruby on Rails app within ActiveJob
|
26
26
|
|
27
27
|
```ruby
|
28
|
-
class
|
28
|
+
class ApiController < ApplicationController
|
29
|
+
before_action :rate_limit
|
29
30
|
|
30
|
-
def
|
31
|
-
|
32
|
-
|
33
|
-
if rate_limit.exhausted? # Rate limit got hit
|
34
|
-
self.class.set(wait: rate_limit.resets_in).perform_later(..args)
|
35
|
-
return
|
36
|
-
end
|
37
|
-
|
38
|
-
# Good to continue expensive operation
|
39
|
-
# ...
|
31
|
+
def index
|
32
|
+
# Good to continue
|
40
33
|
end
|
41
34
|
|
42
35
|
private
|
43
|
-
def
|
44
|
-
|
45
|
-
end
|
46
|
-
end
|
47
|
-
```
|
48
|
-
|
49
|
-
## Point Based Rate Limit Example
|
50
|
-
|
51
|
-
```ruby
|
52
|
-
class ExpensiveJob < ApplicationJob
|
53
|
-
|
54
|
-
def perform
|
55
|
-
rate_limit = limiter.check(shop.id) # A unique identifier
|
36
|
+
def rate_limit
|
37
|
+
rate_limit = limiter.check(user.id)
|
56
38
|
|
57
|
-
if rate_limit.exhausted?
|
58
|
-
|
59
|
-
return
|
60
|
-
else
|
61
|
-
# Good to continue expensive operation
|
62
|
-
# ...
|
63
|
-
# Perform query
|
64
|
-
limiter.used(250) # Points deducted for last query
|
39
|
+
if rate_limit.exhausted?
|
40
|
+
render json: { error: "Rate limit exceeded" }, status: :too_many_requests
|
65
41
|
end
|
66
42
|
end
|
67
43
|
|
68
|
-
private
|
69
44
|
def limiter
|
70
|
-
Limiter::
|
45
|
+
Limiter::Client.new(token: ENV["LIMITER_TOKEN"], namespace: "openai", limit: 60, interval: 1.minute)
|
71
46
|
end
|
72
47
|
end
|
73
48
|
```
|
data/lib/limiter/client.rb
CHANGED
@@ -5,15 +5,15 @@ module Limiter
|
|
5
5
|
|
6
6
|
BASE_DOMAIN = "https://api.limiter.dev".freeze
|
7
7
|
|
8
|
-
attr_reader :namespace, :limit, :
|
8
|
+
attr_reader :namespace, :limit, :interval, :identifier, :token
|
9
9
|
|
10
|
-
def initialize(namespace:, limit:,
|
10
|
+
def initialize(namespace:, limit:, interval:, token: ENV["LIMITER_TOKEN"])
|
11
11
|
@namespace = namespace
|
12
12
|
@limit = limit
|
13
|
-
@
|
14
|
-
@token = ENV["LIMITER_TOKEN"]
|
13
|
+
@interval = interval.to_i
|
14
|
+
@token = token || ENV["LIMITER_TOKEN"]
|
15
15
|
|
16
|
-
ErrorHandler.error("
|
16
|
+
ErrorHandler.error("token is not set") if @token.nil?
|
17
17
|
end
|
18
18
|
|
19
19
|
def check(identifier)
|
@@ -21,27 +21,34 @@ module Limiter
|
|
21
21
|
RateLimitResponse.new(request)
|
22
22
|
end
|
23
23
|
|
24
|
-
def
|
25
|
-
case @
|
24
|
+
def formatted_interval
|
25
|
+
case @interval
|
26
26
|
when 1..59
|
27
|
-
(@
|
27
|
+
(@interval).to_s + "s"
|
28
28
|
when 60..3599
|
29
|
-
(@
|
29
|
+
(@interval / 60).to_s + "m"
|
30
30
|
when 3600..86_399
|
31
|
-
(@
|
31
|
+
(@interval / 3600).to_s + "h"
|
32
32
|
when 86_400..1_209_599
|
33
|
-
(@
|
33
|
+
(@interval / 86_400).to_s + "d"
|
34
34
|
else
|
35
|
-
ErrorHandler.error("Invalid
|
35
|
+
ErrorHandler.error("Invalid interval")
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
def
|
40
|
-
"
|
39
|
+
def limiter_path
|
40
|
+
"/rl/#{namespace}/#{limit}/#{formatted_interval}/#{identifier}"
|
41
41
|
end
|
42
42
|
|
43
43
|
def request(params = {})
|
44
|
-
|
44
|
+
@_conn ||= Faraday.new(url: BASE_DOMAIN) do |conn|
|
45
|
+
conn.request :authorization, :Bearer, token
|
46
|
+
conn.response :json
|
47
|
+
conn.adapter Faraday.default_adapter
|
48
|
+
conn.headers["User-Agent"] = "Limiter-Ruby/#{Limiter::VERSION}"
|
49
|
+
end
|
50
|
+
|
51
|
+
@_conn.get(limiter_path, params)
|
45
52
|
end
|
46
53
|
end
|
47
54
|
end
|
data/lib/limiter/version.rb
CHANGED
data/lib/limiter.rb
CHANGED
data/limiter.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.authors = ["Bilal Budhani"]
|
9
9
|
spec.email = ["bilal@bilalbudhani.com"]
|
10
10
|
|
11
|
-
spec.summary = "
|
12
|
-
spec.description = "Limiter
|
11
|
+
spec.summary = "Toolkit for building rate limits for your application"
|
12
|
+
spec.description = "Limiter is a toolkit for building rate limits for your application. It provides a simple interface to define rate limits and track usage."
|
13
13
|
spec.homepage = "https://limiter.dev"
|
14
14
|
spec.license = "MIT"
|
15
15
|
spec.required_ruby_version = ">= 2.6.0"
|
@@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
|
|
31
31
|
spec.require_paths = ["lib"]
|
32
32
|
|
33
33
|
# Uncomment to register a new dependency of your gem
|
34
|
-
spec.add_dependency "
|
34
|
+
spec.add_dependency "faraday", "~> 2.0"
|
35
35
|
|
36
36
|
# For more information and examples about making a new gem, check out our
|
37
37
|
# guide at: https://bundler.io/guides/creating_gem.html
|
metadata
CHANGED
@@ -1,31 +1,31 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: limiter-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bilal Budhani
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: faraday
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
27
|
-
description: Limiter
|
28
|
-
|
26
|
+
version: '2.0'
|
27
|
+
description: Limiter is a toolkit for building rate limits for your application. It
|
28
|
+
provides a simple interface to define rate limits and track usage.
|
29
29
|
email:
|
30
30
|
- bilal@bilalbudhani.com
|
31
31
|
executables: []
|
@@ -66,8 +66,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
66
|
- !ruby/object:Gem::Version
|
67
67
|
version: '0'
|
68
68
|
requirements: []
|
69
|
-
rubygems_version: 3.5.
|
69
|
+
rubygems_version: 3.5.18
|
70
70
|
signing_key:
|
71
71
|
specification_version: 4
|
72
|
-
summary:
|
72
|
+
summary: Toolkit for building rate limits for your application
|
73
73
|
test_files: []
|