actionlimiter 0.2.0 → 1.0.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/action_limiter/config.rb +41 -0
- data/lib/action_limiter/token_bucket.rb +30 -10
- data/lib/action_limiter/version.rb +1 -1
- data/lib/action_limiter.rb +9 -0
- metadata +11 -8
- data/lib/action_limiter/redis_provider.rb +0 -56
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a45ffa8e18273c24ca2f9407c0909fbc719fb88c49295676256940eecfe3a73f
|
4
|
+
data.tar.gz: e0904db27c728ac00ce0774533a33bf154902682cc867a0a5ed8a313d8c04037
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99d0dc2ec88875dee1d954c146b0c1d2aefb9aa3bcce808e39dc0fb5dae936b176123a3e0ef78691744fd531c9fbc6c5303057738aba636718308cccee1c6527
|
7
|
+
data.tar.gz: 642c0875a2112631fc652c869cef869b6953e36244c55d4d547c07269e250a57ac34767c6386ec42652384a1078dcde3c4cd66590576dac5acb54c84f4813b1d
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'connection_pool'
|
4
|
+
require 'redis'
|
5
|
+
require 'singleton'
|
6
|
+
|
7
|
+
module ActionLimiter # rubocop:disable Style/Documentation
|
8
|
+
##
|
9
|
+
# Provides configuration for the Gem
|
10
|
+
#
|
11
|
+
# @author Maddie Schipper
|
12
|
+
# @since 1.0.0
|
13
|
+
class Config
|
14
|
+
include Singleton
|
15
|
+
|
16
|
+
##
|
17
|
+
# The shared Redis connection pool
|
18
|
+
attr_accessor :redis
|
19
|
+
|
20
|
+
##
|
21
|
+
# The URL used for new Redis connections in the default pool
|
22
|
+
attr_accessor :redis_url
|
23
|
+
|
24
|
+
##
|
25
|
+
# @private
|
26
|
+
def initialize
|
27
|
+
pool_size = ENV.fetch('RAILS_MAX_THREADS', 1).to_i
|
28
|
+
|
29
|
+
self.redis_url = 'redis://localhost:6379/0'
|
30
|
+
self.redis = ConnectionPool.new(size: pool_size) do
|
31
|
+
Redis.new(url: redis_url)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
# @private
|
38
|
+
def self.with_redis_connection(...)
|
39
|
+
Config.instance.redis.with(...)
|
40
|
+
end
|
41
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'action_limiter/config'
|
3
4
|
require 'action_limiter/instrumentation'
|
4
|
-
require 'action_limiter/redis_provider'
|
5
5
|
require 'action_limiter/scripts'
|
6
6
|
|
7
7
|
module ActionLimiter
|
@@ -11,7 +11,9 @@ module ActionLimiter
|
|
11
11
|
# @author Maddie Schipper
|
12
12
|
# @since 0.1.0
|
13
13
|
class TokenBucket
|
14
|
-
|
14
|
+
##
|
15
|
+
# @private
|
16
|
+
Bucket = Struct.new(:name, :value, :max_size, :period, keyword_init: true)
|
15
17
|
|
16
18
|
##
|
17
19
|
# The period length for the bucket in seconds
|
@@ -41,7 +43,7 @@ module ActionLimiter
|
|
41
43
|
@period = period.to_i
|
42
44
|
@size = size.to_i
|
43
45
|
@namespace = namespace&.to_s || 'action_limiter/token_bucket'
|
44
|
-
@script_hash = ActionLimiter
|
46
|
+
@script_hash = ActionLimiter.with_redis_connection do |connection|
|
45
47
|
connection.script(:load, ActionLimiter::SCRIPTS.fetch(:token_bucket))
|
46
48
|
end
|
47
49
|
end
|
@@ -54,20 +56,38 @@ module ActionLimiter
|
|
54
56
|
#
|
55
57
|
# @return [true, false] The limiting status of the bucket
|
56
58
|
def limited?(bucket, time: Time.now)
|
57
|
-
|
59
|
+
increment_and_return_bucket(bucket, time).value > size
|
60
|
+
end
|
61
|
+
|
62
|
+
##
|
63
|
+
# Delete a bucket's current value
|
64
|
+
#
|
65
|
+
# @param bucket [String] The name of the bucket to delete
|
66
|
+
#
|
67
|
+
# @return [void]
|
68
|
+
def delete(bucket)
|
69
|
+
ActionLimiter.instrument('action_limiter.token_bucket.reset') do
|
70
|
+
ActionLimiter.with_redis_connection do |connection|
|
71
|
+
connection.del(bucket_key(bucket))
|
72
|
+
end
|
73
|
+
end
|
58
74
|
end
|
59
75
|
|
60
76
|
##
|
61
77
|
# @private
|
62
|
-
def
|
78
|
+
def increment_and_return_bucket(bucket, time)
|
63
79
|
ActionLimiter.instrument('action_limiter.token_bucket.increment') do
|
64
|
-
ActionLimiter
|
65
|
-
|
66
|
-
|
67
|
-
value = connection.evalsha(@script_hash, [bucket_key], [period.to_s, time_stamp.to_s])
|
68
|
-
Bucket.new(bucket, value)
|
80
|
+
ActionLimiter.with_redis_connection do |connection|
|
81
|
+
value = connection.evalsha(@script_hash, [bucket_key(bucket)], [period.to_s, time.to_f])
|
82
|
+
Bucket.new(name: bucket, value: value, max_size: size, period: period)
|
69
83
|
end
|
70
84
|
end
|
71
85
|
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def bucket_key(bucket)
|
90
|
+
"#{namespace}/#{bucket}"
|
91
|
+
end
|
72
92
|
end
|
73
93
|
end
|
data/lib/action_limiter.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'action_limiter/config'
|
3
4
|
require 'action_limiter/middleware'
|
4
5
|
require 'action_limiter/rails'
|
5
6
|
require 'action_limiter/token_bucket'
|
@@ -11,4 +12,12 @@ require 'action_limiter/version'
|
|
11
12
|
# @author Maddie Schipper
|
12
13
|
# @since 0.1.0
|
13
14
|
module ActionLimiter
|
15
|
+
##
|
16
|
+
# Perform configuration
|
17
|
+
#
|
18
|
+
# @author Maddie Schipper
|
19
|
+
# @since 1.0.0
|
20
|
+
def self.configure
|
21
|
+
yield(Config.instance) if block_given?
|
22
|
+
end
|
14
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: actionlimiter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0.beta2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maddie Schipper
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: connection_pool
|
@@ -50,7 +50,9 @@ dependencies:
|
|
50
50
|
- - "<"
|
51
51
|
- !ruby/object:Gem::Version
|
52
52
|
version: '5.0'
|
53
|
-
description: Redis backed
|
53
|
+
description: 'Redis backed token bucket rate limting implementation.
|
54
|
+
|
55
|
+
'
|
54
56
|
email:
|
55
57
|
- maddie@angryboat.com
|
56
58
|
executables: []
|
@@ -60,12 +62,12 @@ files:
|
|
60
62
|
- LICENSE
|
61
63
|
- README.md
|
62
64
|
- lib/action_limiter.rb
|
65
|
+
- lib/action_limiter/config.rb
|
63
66
|
- lib/action_limiter/instrumentation.rb
|
64
67
|
- lib/action_limiter/middleware.rb
|
65
68
|
- lib/action_limiter/middleware/ip.rb
|
66
69
|
- lib/action_limiter/rails.rb
|
67
70
|
- lib/action_limiter/rails/engine.rb
|
68
|
-
- lib/action_limiter/redis_provider.rb
|
69
71
|
- lib/action_limiter/scripts.rb
|
70
72
|
- lib/action_limiter/scripts/token_bucket.lua
|
71
73
|
- lib/action_limiter/token_bucket.rb
|
@@ -76,6 +78,7 @@ licenses:
|
|
76
78
|
- MIT
|
77
79
|
metadata:
|
78
80
|
allowed_push_host: https://rubygems.org
|
81
|
+
rubygems_mfa_required: 'true'
|
79
82
|
homepage_uri: https://github.com/angryboat/actionlimiter
|
80
83
|
source_code_uri: https://github.com/angryboat/actionlimiter
|
81
84
|
changelog_uri: https://github.com/angryboat/actionlimiter
|
@@ -87,15 +90,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
90
|
requirements:
|
88
91
|
- - ">="
|
89
92
|
- !ruby/object:Gem::Version
|
90
|
-
version: 2.7
|
93
|
+
version: '2.7'
|
91
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
95
|
requirements:
|
93
|
-
- - "
|
96
|
+
- - ">"
|
94
97
|
- !ruby/object:Gem::Version
|
95
|
-
version:
|
98
|
+
version: 1.3.1
|
96
99
|
requirements: []
|
97
100
|
rubygems_version: 3.1.6
|
98
101
|
signing_key:
|
99
102
|
specification_version: 4
|
100
|
-
summary:
|
103
|
+
summary: Rate Limiting
|
101
104
|
test_files: []
|
@@ -1,56 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'connection_pool'
|
4
|
-
require 'redis'
|
5
|
-
|
6
|
-
##
|
7
|
-
# @private
|
8
|
-
module ActionLimiter
|
9
|
-
##
|
10
|
-
# Private
|
11
|
-
class RedisProvider
|
12
|
-
MUTEX = Mutex.new
|
13
|
-
|
14
|
-
class << self
|
15
|
-
def pool_size
|
16
|
-
ENV.fetch('ACTION_LIMITER_POOL_SIZE', 5).to_i
|
17
|
-
end
|
18
|
-
|
19
|
-
def pool_connection_timeout
|
20
|
-
ENV.fetch('ACTION_LIMITER_TIMEOUT', 30).to_i
|
21
|
-
end
|
22
|
-
|
23
|
-
def redis_connection_host
|
24
|
-
ENV.fetch('ACTION_LIMITER_REDIS_HOST', '127.0.0.1')
|
25
|
-
end
|
26
|
-
|
27
|
-
def redis_connection_port
|
28
|
-
ENV.fetch('ACTION_LIMITER_REDIS_PORT', 6379).to_i
|
29
|
-
end
|
30
|
-
|
31
|
-
def redis_connection_database
|
32
|
-
ENV.fetch('ACTION_LIMITER_REDIS_DB', 0).to_i
|
33
|
-
end
|
34
|
-
|
35
|
-
def connection_pool
|
36
|
-
MUTEX.synchronize do
|
37
|
-
@connection_pool ||= unsafe_create_connection_pool
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def unsafe_create_connection_pool
|
42
|
-
ConnectionPool.new(size: pool_size, timeout: pool_connection_timeout) do
|
43
|
-
RedisProvider.unsafe_create_redis_connection
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def unsafe_create_redis_connection
|
48
|
-
Redis.new(
|
49
|
-
host: redis_connection_host,
|
50
|
-
port: redis_connection_port,
|
51
|
-
db: redis_connection_database
|
52
|
-
)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|