co-limit 0.1.1 → 0.1.3
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 +4 -4
- data/lib/limit/version.rb +1 -1
- data/lib/limit.rb +19 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0298c1dc7c99033e8919babcecbfdc0a25fd25ff05a8ebdc53437b898bc7ee7
|
4
|
+
data.tar.gz: 63b245b43b37eaa0340298d9b4317a078868fc37862b01d414764e671ef4c84b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b959ef55df27bea377e777d83e2acaad02f166eff5ff3e6140253ab54fb5e4fd0e8d6bcf40f2ea440a20cef7099cae8f3ac468d960ce090b6fa1fbb220b4406
|
7
|
+
data.tar.gz: 9a3b26e770dda72c4096c20ed601d672faaf4ee26bff9c6cfa155c8ba8319e977c7c0477acc27417b77252b92e250e64782da291d02de5f5c97629ca8e03ad8a
|
data/lib/limit/version.rb
CHANGED
data/lib/limit.rb
CHANGED
@@ -43,8 +43,8 @@ module Limit
|
|
43
43
|
|
44
44
|
def initialize(identifier_prefix:, limit_calculator:, host: nil, port: nil, password: nil)
|
45
45
|
|
46
|
-
|
47
|
-
|
46
|
+
# @param identifier_prefix: [String] A namespace prefix for redis keys for this limiter instance
|
47
|
+
# @param limit_calculator: [Lambda] A method that takes a key(String) and returns hash: {max_requests: Integer, window_seconds: Integer}
|
48
48
|
|
49
49
|
unless identifier_prefix.is_a?(String) && !identifier_prefix.empty?
|
50
50
|
raise ArgumentError, 'identifier_prefix must be a non-empty String'
|
@@ -54,6 +54,7 @@ module Limit
|
|
54
54
|
|
55
55
|
# Will be using the same connection across all instance unless wanted to connect to diff instance of redis
|
56
56
|
|
57
|
+
#TODO: Connection creation can be lazy
|
57
58
|
@redis = if host && port && password
|
58
59
|
self.class.send(:create_connection, host: host, port: port, password: password)
|
59
60
|
else
|
@@ -78,7 +79,7 @@ module Limit
|
|
78
79
|
raise NotImplementedError "#{self.class.name} must implement the allowed? method"
|
79
80
|
end
|
80
81
|
|
81
|
-
def get_key(
|
82
|
+
def get_key(key)
|
82
83
|
raise NotImplementedError "#{self.class.name} must implement the get_key() method"
|
83
84
|
end
|
84
85
|
|
@@ -98,9 +99,19 @@ module Limit
|
|
98
99
|
end
|
99
100
|
|
100
101
|
def redis_pipeline(&block)
|
102
|
+
attempts ||= 0
|
101
103
|
begin
|
102
104
|
@redis.pipelined { |pipe| block.call(pipe) }
|
103
|
-
rescue Redis::
|
105
|
+
rescue Redis::CannotConnectError, Redis::TimeoutError, Redis::ConnectionError => e
|
106
|
+
if attempts < 3
|
107
|
+
@logger.warn(e.message)
|
108
|
+
@redis = self.class.connection
|
109
|
+
attempts += 1
|
110
|
+
retry
|
111
|
+
else
|
112
|
+
@logger.error("Error connecting to Redis: #{e.message}")
|
113
|
+
end
|
114
|
+
rescue Redis::BaseError => e
|
104
115
|
@logger.error(e.message)
|
105
116
|
end
|
106
117
|
end
|
@@ -136,9 +147,9 @@ module Limit
|
|
136
147
|
results[0] <= max_requests
|
137
148
|
end
|
138
149
|
|
139
|
-
def get_key(
|
150
|
+
def get_key(key, window_seconds)
|
140
151
|
time_window = (Time.now.to_i / window_seconds) * window_seconds
|
141
|
-
"#{@identifier_prefix}:#{
|
152
|
+
"#{@identifier_prefix}:#{key}:#{time_window.to_s}"
|
142
153
|
end
|
143
154
|
end
|
144
155
|
|
@@ -167,8 +178,8 @@ module Limit
|
|
167
178
|
results[2] <= max_requests
|
168
179
|
end
|
169
180
|
|
170
|
-
def get_key(
|
171
|
-
"#{@identifier_prefix}:#{
|
181
|
+
def get_key(key)
|
182
|
+
"#{@identifier_prefix}:#{key}"
|
172
183
|
end
|
173
184
|
end
|
174
185
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: co-limit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- CosmicOppai
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-
|
11
|
+
date: 2025-08-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|