boto-rb-redis 0.7.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/LICENSE +21 -0
- data/lib/boto/redis/rate_limit_store/script.rb +27 -0
- data/lib/boto/redis/rate_limit_store.rb +137 -0
- data/lib/boto/redis/version.rb +7 -0
- data/lib/boto/redis.rb +15 -0
- metadata +88 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 9a578801c73f152ea030e51b155884c26410a2474ea894c52c26d87719d1c055
|
|
4
|
+
data.tar.gz: ad4b3197b416cc92fec54c6f2f470e72012815f234d91180afcbd6b1e99de636
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: bfedb7b64c7eea3f04b22619681cabd04586ee134e27a60c86d63360119edeccb8f55ea81dabfedec29887dcdfdf0654ebf9c6a9066541ce933042104e61e54e
|
|
7
|
+
data.tar.gz: d735bd937446e90abbe265a2896a933b9aedf39d4b10fe8e70ebc403a0a34c8012c7bddf2c7a1a5258dea81b10fba8c99012b33a7861d50fdab75ee14a34a58c
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Evo Tech Builder
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Redis
|
|
7
|
+
class RateLimitStore
|
|
8
|
+
# INCR and, on the first hit of a window, PEXPIRE, in one atomic round trip.
|
|
9
|
+
# Returns [count, pttl]: the counter and the milliseconds left before it expires.
|
|
10
|
+
module Script
|
|
11
|
+
SOURCE = <<~LUA.freeze
|
|
12
|
+
local count = redis.call('INCR', KEYS[1])
|
|
13
|
+
if count == 1 then redis.call('PEXPIRE', KEYS[1], ARGV[1]) end
|
|
14
|
+
return {count, redis.call('PTTL', KEYS[1])}
|
|
15
|
+
LUA
|
|
16
|
+
SHA1 = Digest::SHA1.hexdigest(SOURCE).freeze
|
|
17
|
+
|
|
18
|
+
# Runs by SHA1 and loads the source when Redis has not cached it yet (restart, SCRIPT FLUSH).
|
|
19
|
+
def self.run(client, key, ttl_ms)
|
|
20
|
+
client.call("EVALSHA", SHA1, 1, key, ttl_ms)
|
|
21
|
+
rescue ::RedisClient::NoScriptError
|
|
22
|
+
client.call("EVAL", SOURCE, 1, key, ttl_ms)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "uri"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Redis
|
|
7
|
+
# Fixed-window counter in Redis, shared by every process that uses the same Redis.
|
|
8
|
+
# The URL never reaches logs, errors or inspect: only host:port is described.
|
|
9
|
+
class RateLimitStore
|
|
10
|
+
DEFAULT_PREFIX = "boto:rate_limit"
|
|
11
|
+
DEFAULT_TIMEOUT = 0.1
|
|
12
|
+
# Measured on Falcon with wrk -c50: 5 -> ~9.1k req/s, 20 -> ~11.1k with the best p99; more gains nothing and worsens p99.
|
|
13
|
+
# Connections are created on demand, so an idle pool costs nothing.
|
|
14
|
+
DEFAULT_POOL_SIZE = 20
|
|
15
|
+
DEFAULT_CLOCK = -> { Process.clock_gettime(Process::CLOCK_REALTIME) }.freeze
|
|
16
|
+
ON_ERROR_MODES = %i[allow deny].freeze
|
|
17
|
+
FAILURES = [::RedisClient::Error, ConnectionPool::TimeoutError].freeze
|
|
18
|
+
CUSTOM_CLIENT = "custom client"
|
|
19
|
+
UNAVAILABLE_MESSAGE = "rate limit store unavailable"
|
|
20
|
+
MIN_RESET_SECONDS = 1
|
|
21
|
+
MIN_TTL_MS = 1
|
|
22
|
+
MS_PER_SECOND = 1000
|
|
23
|
+
|
|
24
|
+
def initialize(url: nil, client: nil, prefix: DEFAULT_PREFIX, timeout: DEFAULT_TIMEOUT,
|
|
25
|
+
pool_size: DEFAULT_POOL_SIZE, on_error: :allow, logger: $stderr, clock: DEFAULT_CLOCK)
|
|
26
|
+
url = url.call if url.respond_to?(:call)
|
|
27
|
+
validate_source!(url, client)
|
|
28
|
+
validate_options!(prefix, timeout, pool_size, on_error)
|
|
29
|
+
|
|
30
|
+
@prefix = prefix
|
|
31
|
+
@on_error = on_error
|
|
32
|
+
@logger = logger
|
|
33
|
+
@clock = clock
|
|
34
|
+
@outage_mutex = Mutex.new
|
|
35
|
+
@outage_window = nil
|
|
36
|
+
@key_prefixes = {}
|
|
37
|
+
@client, @endpoint = url ? connect(url, timeout, pool_size) : [client, CUSTOM_CLIENT]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Returns [count, reset_in]: requests counted for key in the current window and whole seconds until it ends.
|
|
41
|
+
def increment(key, period)
|
|
42
|
+
now = @clock.call
|
|
43
|
+
window = window_of(now, period)
|
|
44
|
+
count, pttl = Script.run(@client, counter_key(key, period, window), ms_until_window_end(now, window, period))
|
|
45
|
+
[count, seconds_from_ms(pttl)]
|
|
46
|
+
rescue *FAILURES => error
|
|
47
|
+
handle_failure(error, now, period)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def inspect
|
|
51
|
+
"#<#{self.class.name} endpoint=#{@endpoint} prefix=#{@prefix} on_error=#{@on_error}>"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def validate_source!(url, client)
|
|
57
|
+
raise ArgumentError, "pass exactly one of url: or client:" if url.nil? == client.nil?
|
|
58
|
+
raise ArgumentError, "url must be a String" unless url.nil? || url.is_a?(String)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def validate_options!(prefix, timeout, pool_size, on_error)
|
|
62
|
+
raise ArgumentError, "prefix must be a non-empty String" unless prefix.is_a?(String) && !prefix.empty?
|
|
63
|
+
raise ArgumentError, "timeout must be a positive Numeric" unless timeout.is_a?(Numeric) && timeout.positive?
|
|
64
|
+
raise ArgumentError, "pool_size must be a positive Integer" unless pool_size.is_a?(Integer) && pool_size.positive?
|
|
65
|
+
raise ArgumentError, "on_error must be one of #{ON_ERROR_MODES.inspect}" unless ON_ERROR_MODES.include?(on_error)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Returns [pool, endpoint]. The pool lets concurrent fibers and threads each use their own connection.
|
|
69
|
+
# endpoint_of runs first: redis-client parses the URL too, and its parse errors echo the whole URL.
|
|
70
|
+
def connect(url, timeout, pool_size)
|
|
71
|
+
endpoint = endpoint_of(url)
|
|
72
|
+
config = ::RedisClient.config(url: url, connect_timeout: timeout, read_timeout: timeout, write_timeout: timeout)
|
|
73
|
+
[config.new_pool(size: pool_size, timeout: timeout), endpoint]
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Raises without the original message, which may contain the URL.
|
|
77
|
+
def endpoint_of(url)
|
|
78
|
+
uri = URI(url)
|
|
79
|
+
uri.host ? "#{uri.host}:#{uri.port}" : uri.scheme
|
|
80
|
+
rescue URI::Error
|
|
81
|
+
raise ArgumentError, "url is not a valid Redis URL"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def window_of(now, period)
|
|
85
|
+
(now / period).floor
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def counter_key(key, period, window)
|
|
89
|
+
"#{key_prefix(period, window)}#{key}"
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Cached per period while the window is the same. Two threads may rebuild it at once; both get the same value.
|
|
93
|
+
def key_prefix(period, window)
|
|
94
|
+
cached_window, prefix = @key_prefixes[period]
|
|
95
|
+
return prefix if cached_window == window
|
|
96
|
+
|
|
97
|
+
prefix = "#{@prefix}:#{period}:#{window}:".freeze
|
|
98
|
+
@key_prefixes[period] = [window, prefix]
|
|
99
|
+
prefix
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def ms_until_window_end(now, window, period)
|
|
103
|
+
[((window + 1) * period - now) * MS_PER_SECOND, MIN_TTL_MS].max.ceil
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def reset_in(now, window, period)
|
|
107
|
+
[((window + 1) * period - now).ceil, MIN_RESET_SECONDS].max
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def seconds_from_ms(pttl)
|
|
111
|
+
[(pttl / MS_PER_SECOND.to_f).ceil, MIN_RESET_SECONDS].max
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def handle_failure(error, now, period)
|
|
115
|
+
raise ::Boto::Error::ServiceUnavailable.new(UNAVAILABLE_MESSAGE) if @on_error == :deny
|
|
116
|
+
|
|
117
|
+
window = window_of(now, period)
|
|
118
|
+
log_outage_once(error, window)
|
|
119
|
+
[0, reset_in(now, window, period)]
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def log_outage_once(error, window)
|
|
123
|
+
return unless first_outage_in?(window)
|
|
124
|
+
|
|
125
|
+
@logger.puts("Boto::Redis::RateLimitStore: Redis unavailable at #{@endpoint} (#{error.class}); allowing requests")
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def first_outage_in?(window)
|
|
129
|
+
@outage_mutex.synchronize do
|
|
130
|
+
first = @outage_window != window
|
|
131
|
+
@outage_window = window
|
|
132
|
+
first
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
data/lib/boto/redis.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "boto/security"
|
|
4
|
+
require "connection_pool"
|
|
5
|
+
require "redis-client"
|
|
6
|
+
require "zeitwerk"
|
|
7
|
+
|
|
8
|
+
loader = Zeitwerk::Loader.for_gem_extension(Boto)
|
|
9
|
+
loader.setup
|
|
10
|
+
|
|
11
|
+
module Boto
|
|
12
|
+
# Redis-backed stores for Boto plugins.
|
|
13
|
+
module Redis
|
|
14
|
+
end
|
|
15
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: boto-rb-redis
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.7.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Evo Tech Builder
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: boto-rb-security
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - '='
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 0.7.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.7.0
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: connection_pool
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '3.0'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '3.0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: redis-client
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0.31'
|
|
47
|
+
type: :runtime
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0.31'
|
|
54
|
+
executables: []
|
|
55
|
+
extensions: []
|
|
56
|
+
extra_rdoc_files: []
|
|
57
|
+
files:
|
|
58
|
+
- LICENSE
|
|
59
|
+
- lib/boto/redis.rb
|
|
60
|
+
- lib/boto/redis/rate_limit_store.rb
|
|
61
|
+
- lib/boto/redis/rate_limit_store/script.rb
|
|
62
|
+
- lib/boto/redis/version.rb
|
|
63
|
+
homepage: https://github.com/EvoTechBuilder/boto-rb
|
|
64
|
+
licenses:
|
|
65
|
+
- MIT
|
|
66
|
+
metadata:
|
|
67
|
+
rubygems_mfa_required: 'true'
|
|
68
|
+
source_code_uri: https://github.com/EvoTechBuilder/boto-rb
|
|
69
|
+
changelog_uri: https://github.com/EvoTechBuilder/boto-rb/blob/main/CHANGELOG.md
|
|
70
|
+
rdoc_options: []
|
|
71
|
+
require_paths:
|
|
72
|
+
- lib
|
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
74
|
+
requirements:
|
|
75
|
+
- - ">="
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: '3.4'
|
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
requirements: []
|
|
84
|
+
rubygems_version: 4.0.20
|
|
85
|
+
specification_version: 4
|
|
86
|
+
summary: Redis-backed stores for Boto plugins (rate limiting today, sessions and cache
|
|
87
|
+
later).
|
|
88
|
+
test_files: []
|