gcra 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/gcra/redis_store.rb +19 -16
- data/lib/gcra/version.rb +1 -1
- 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: d08cd7202437ad330ea13e7e5566e4d8625a951da36b5c096c53b0502f3d5d70
|
4
|
+
data.tar.gz: 9a7f6298e891d16172b6f08fce6adbf2e7f114a1309ab993e949ca485333bb38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cceec9abdfcc126ed38eec59778cf0ee4f1992d1414c61f545baee34cf9c590860ad6cfc3e6120a51369b1546f64c2dd30d47a43b95f40f66b0999ac150a3d27
|
7
|
+
data.tar.gz: e288210de6c6ac86d431468c881b438533490353b10040babd850da091e8aedb6788258adb7105a7b94240da93dfc4704cdb29dbb6ff3d3a253e2eba5e87921e
|
data/lib/gcra/redis_store.rb
CHANGED
@@ -15,10 +15,10 @@ module GCRA
|
|
15
15
|
|
16
16
|
# Digest::SHA1.hexdigest(CAS_SCRIPT)
|
17
17
|
CAS_SHA = "89118e702230c0d65969c5fc557a6e942a2f4d31".freeze
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
CAS_SCRIPT_MISSING_KEY_RESPONSE_PATTERN = Regexp.new('^key does not exist')
|
19
|
+
SCRIPT_NOT_IN_CACHE_RESPONSE_PATTERN = Regexp.new(
|
20
|
+
'^NOSCRIPT No matching script. Please use EVAL.',
|
21
|
+
)
|
22
22
|
|
23
23
|
def initialize(redis, key_prefix, options = {})
|
24
24
|
@redis = redis
|
@@ -30,9 +30,9 @@ module GCRA
|
|
30
30
|
# Returns the value of the key or nil, if it isn't in the store.
|
31
31
|
# Also returns the time from the Redis server, with microsecond precision.
|
32
32
|
def get_with_time(key)
|
33
|
-
time_response, value = @redis.pipelined do
|
34
|
-
|
35
|
-
|
33
|
+
time_response, value = @redis.pipelined do |pipeline|
|
34
|
+
pipeline.time # returns tuple (seconds since epoch, microseconds)
|
35
|
+
pipeline.get(@key_prefix + key)
|
36
36
|
end
|
37
37
|
# Convert tuple to nanoseconds
|
38
38
|
time = (time_response[0] * 1_000_000 + time_response[1]) * 1_000
|
@@ -51,9 +51,9 @@ module GCRA
|
|
51
51
|
begin
|
52
52
|
ttl_milli = calculate_ttl_milli(ttl_nano)
|
53
53
|
@redis.set(full_key, value, nx: true, px: ttl_milli)
|
54
|
-
rescue Redis::
|
55
|
-
if
|
56
|
-
@redis.
|
54
|
+
rescue Redis::ReadOnlyError => e
|
55
|
+
if @reconnect_on_readonly && !retried
|
56
|
+
@redis.close
|
57
57
|
retried = true
|
58
58
|
retry
|
59
59
|
end
|
@@ -70,17 +70,20 @@ module GCRA
|
|
70
70
|
begin
|
71
71
|
ttl_milli = calculate_ttl_milli(ttl_nano)
|
72
72
|
swapped = @redis.evalsha(CAS_SHA, keys: [full_key], argv: [old_value, new_value, ttl_milli])
|
73
|
+
rescue Redis::ReadOnlyError => e
|
74
|
+
if @reconnect_on_readonly && !retried
|
75
|
+
@redis.close
|
76
|
+
retried = true
|
77
|
+
retry
|
78
|
+
end
|
79
|
+
raise
|
73
80
|
rescue Redis::CommandError => e
|
74
|
-
if e.message
|
81
|
+
if e.message =~ CAS_SCRIPT_MISSING_KEY_RESPONSE_PATTERN
|
75
82
|
return false
|
76
|
-
elsif e.message
|
83
|
+
elsif e.message =~ SCRIPT_NOT_IN_CACHE_RESPONSE_PATTERN && !retried
|
77
84
|
@redis.script('load', CAS_SCRIPT)
|
78
85
|
retried = true
|
79
86
|
retry
|
80
|
-
elsif e.message == CONNECTED_TO_READONLY && @reconnect_on_readonly && !retried
|
81
|
-
@redis.client.reconnect
|
82
|
-
retried = true
|
83
|
-
retry
|
84
87
|
end
|
85
88
|
raise
|
86
89
|
end
|
data/lib/gcra/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gcra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Frister
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2024-03-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -17,31 +17,31 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '3.
|
20
|
+
version: '3.12'
|
21
21
|
type: :development
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: '3.
|
27
|
+
version: '3.12'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: redis
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
32
|
- - "~>"
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '
|
34
|
+
version: '5.1'
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
39
|
- - "~>"
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: '
|
41
|
+
version: '5.1'
|
42
42
|
description: GCRA implementation for rate limiting
|
43
43
|
email:
|
44
|
-
- tobias.schoknecht@
|
44
|
+
- tobias.schoknecht@viafintech.com
|
45
45
|
executables: []
|
46
46
|
extensions: []
|
47
47
|
extra_rdoc_files: []
|
@@ -49,7 +49,7 @@ files:
|
|
49
49
|
- lib/gcra/rate_limiter.rb
|
50
50
|
- lib/gcra/redis_store.rb
|
51
51
|
- lib/gcra/version.rb
|
52
|
-
homepage: https://github.com/
|
52
|
+
homepage: https://github.com/viafintech/gcra-ruby
|
53
53
|
licenses:
|
54
54
|
- MIT
|
55
55
|
metadata: {}
|
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0'
|
70
70
|
requirements: []
|
71
|
-
rubygems_version: 3.
|
71
|
+
rubygems_version: 3.4.14
|
72
72
|
signing_key:
|
73
73
|
specification_version: 4
|
74
74
|
summary: Ruby implementation of a generic cell rate algorithm (GCRA), ported from
|