actionlimiter 0.2.0 → 1.0.0.beta4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 01f5bc1107b2d90f94cb1805894dc9047764da8c0cfe298836a63ab01f4ad38b
4
- data.tar.gz: f6eca9a81f6492e8f6a9c3b2231a5f85b0d5417773e57e4495b8976652b8564f
3
+ metadata.gz: 596e0724078e69a1a1a9fa6c220cfc338f71effe4ddd5e5b6e97bd3d5238d3ef
4
+ data.tar.gz: db1489d41e0fc62ecb8a41ab310bbeae3541d30934d3575db25d0adf24ea7c10
5
5
  SHA512:
6
- metadata.gz: 40a691dd3379ac0c52dd09ba9c3a159953be02457a76984fe6a2d3d36339a4fd0bf2710030d5c8428e020d12053104c192d09807ad47ed57c3266650a914472c
7
- data.tar.gz: d2bcd2fff1eb33902cbd3993675232224dacfd0d07e9abb49a2adc940c381b84fdcc2f02a0cd6c079675c338c3eac6e0f1313d4c25a018ef8a9454a7640d8f83
6
+ metadata.gz: 18d46303d422c38445b7f100f9125c86028649e6971fbbf1035652345675ad4f3c448934871fd275de27582e9cc649be8cd432106fd2c8c183b46fc999d81708
7
+ data.tar.gz: e0ab0fd97db9ac5c75780fe228efb51b5b7c3a70ea87f790601b5d4954325c9edf7700539c87b2516f37cdcdfc7cdd96bb095cab378f75c59e0168df61a264aa
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ActionLimiter
2
2
 
3
- [![Unit Test](https://github.com/angryboat/actionlimiter/actions/workflows/testing.yml/badge.svg)](https://github.com/angryboat/actionlimiter/actions/workflows/testing.yml) [![Linters](https://github.com/angryboat/actionlimiter/actions/workflows/linting.yml/badge.svg)](https://github.com/angryboat/actionlimiter/actions/workflows/linting.yml)
3
+ [![Ruby Gem](https://github.com/angryboat/actionlimiter/actions/workflows/ruby-gem.yml/badge.svg?event=push)](https://github.com/angryboat/actionlimiter/actions/workflows/ruby-gem.yml)
4
4
 
5
5
  Provides Redis backed rate limiting for Rails applications.
6
6
 
@@ -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
@@ -63,7 +63,7 @@ module ActionLimiter
63
63
  def _call(env)
64
64
  remote_ip = env.fetch('action_dispatch.remote_ip')
65
65
  bucket_key = Digest::MD5.hexdigest(remote_ip.to_s)
66
- bucket = @token_bucket.increment(bucket_key, Time.now)
66
+ bucket = @token_bucket.increment_and_return_bucket(bucket_key, Time.now)
67
67
 
68
68
  env['action_limiter.ip_bucket'] = bucket
69
69
 
@@ -13,6 +13,6 @@ local bucket_name = KEYS[1]
13
13
 
14
14
  redis.call('ZREMRANGEBYSCORE', bucket_name, '-inf', min)
15
15
  redis.call('ZADD', bucket_name, ts, ts)
16
- redis.call('EXPIRE', bucket_name, period * 5)
16
+ redis.call('EXPIRE', bucket_name, period + 2)
17
17
 
18
18
  return redis.call('ZCARD', bucket_name)
@@ -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
- Bucket = Struct.new(:name, :value)
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,9 +43,6 @@ 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::RedisProvider.connection_pool.with do |connection|
45
- connection.script(:load, ActionLimiter::SCRIPTS.fetch(:token_bucket))
46
- end
47
46
  end
48
47
 
49
48
  ##
@@ -54,20 +53,44 @@ module ActionLimiter
54
53
  #
55
54
  # @return [true, false] The limiting status of the bucket
56
55
  def limited?(bucket, time: Time.now)
57
- increment(bucket, time).value > size
56
+ increment_and_return_bucket(bucket, time).value > size
57
+ end
58
+
59
+ ##
60
+ # Delete a bucket's current value
61
+ #
62
+ # @param bucket [String] The name of the bucket to delete
63
+ #
64
+ # @return [void]
65
+ def delete(bucket)
66
+ ActionLimiter.instrument('action_limiter.token_bucket.reset') do
67
+ ActionLimiter.with_redis_connection do |connection|
68
+ connection.del(bucket_key(bucket))
69
+ end
70
+ end
58
71
  end
59
72
 
60
73
  ##
61
74
  # @private
62
- def increment(bucket, time)
75
+ def increment_and_return_bucket(bucket, time)
63
76
  ActionLimiter.instrument('action_limiter.token_bucket.increment') do
64
- ActionLimiter::RedisProvider.connection_pool.with do |connection|
65
- time_stamp = time.to_f
66
- bucket_key = "#{namespace}/#{bucket}"
67
- value = connection.evalsha(@script_hash, [bucket_key], [period.to_s, time_stamp.to_s])
68
- Bucket.new(bucket, value)
77
+ ActionLimiter.with_redis_connection do |connection|
78
+ value = connection.evalsha(script_hash, [bucket_key(bucket)], [period.to_s, time.to_f.to_s])
79
+ Bucket.new(name: bucket, value: value, max_size: size, period: period)
69
80
  end
70
81
  end
71
82
  end
83
+
84
+ private
85
+
86
+ def script_hash
87
+ @script_hash ||= ActionLimiter.with_redis_connection do |connection|
88
+ connection.script(:load, ActionLimiter::SCRIPTS.fetch(:token_bucket))
89
+ end
90
+ end
91
+
92
+ def bucket_key(bucket)
93
+ "#{namespace}/#{bucket}"
94
+ end
72
95
  end
73
96
  end
@@ -3,5 +3,5 @@
3
3
  module ActionLimiter
4
4
  ##
5
5
  # The current version number
6
- VERSION = '0.2.0'
6
+ VERSION = '1.0.0.beta4'
7
7
  end
@@ -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.2.0
4
+ version: 1.0.0.beta4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maddie Schipper
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-04 00:00:00.000000000 Z
11
+ date: 2022-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: connection_pool
@@ -50,7 +50,23 @@ dependencies:
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
52
  version: '5.0'
53
- description: Redis backed request rate limiter
53
+ - !ruby/object:Gem::Dependency
54
+ name: pry
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ description: 'Redis backed token bucket rate limting implementation.
68
+
69
+ '
54
70
  email:
55
71
  - maddie@angryboat.com
56
72
  executables: []
@@ -60,12 +76,12 @@ files:
60
76
  - LICENSE
61
77
  - README.md
62
78
  - lib/action_limiter.rb
79
+ - lib/action_limiter/config.rb
63
80
  - lib/action_limiter/instrumentation.rb
64
81
  - lib/action_limiter/middleware.rb
65
82
  - lib/action_limiter/middleware/ip.rb
66
83
  - lib/action_limiter/rails.rb
67
84
  - lib/action_limiter/rails/engine.rb
68
- - lib/action_limiter/redis_provider.rb
69
85
  - lib/action_limiter/scripts.rb
70
86
  - lib/action_limiter/scripts/token_bucket.lua
71
87
  - lib/action_limiter/token_bucket.rb
@@ -76,6 +92,7 @@ licenses:
76
92
  - MIT
77
93
  metadata:
78
94
  allowed_push_host: https://rubygems.org
95
+ rubygems_mfa_required: 'true'
79
96
  homepage_uri: https://github.com/angryboat/actionlimiter
80
97
  source_code_uri: https://github.com/angryboat/actionlimiter
81
98
  changelog_uri: https://github.com/angryboat/actionlimiter
@@ -87,15 +104,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
87
104
  requirements:
88
105
  - - ">="
89
106
  - !ruby/object:Gem::Version
90
- version: 2.7.0
107
+ version: '2.7'
91
108
  required_rubygems_version: !ruby/object:Gem::Requirement
92
109
  requirements:
93
- - - ">="
110
+ - - ">"
94
111
  - !ruby/object:Gem::Version
95
- version: '0'
112
+ version: 1.3.1
96
113
  requirements: []
97
114
  rubygems_version: 3.1.6
98
115
  signing_key:
99
116
  specification_version: 4
100
- summary: Redis backed request rate limiter
117
+ summary: Rate Limiting
101
118
  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