idempotent-request 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/idempotent-request/middleware.rb +20 -2
- data/lib/idempotent-request/redis_storage.rb +17 -5
- data/lib/idempotent-request/request_manager.rb +4 -0
- data/lib/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d52f16d55268a056fc1a8239d3112fcc4ef621442e78f5f4100a6093d3279711
|
4
|
+
data.tar.gz: fd9bd7e9290369d1ec2e583eea23bd3ac41d2b1b143cff1faebcc3c65ab17565
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55daca8c55cc835f24a72c94e72636c3935e27d223238c34fec6d5795fd7b1ec5e6bda6da277783ba3f97af68451356583cbffc02b65791cd6bee2518f4f5ce3
|
7
|
+
data.tar.gz: 04cf35af5be08d455d9aca35f5a41c7924b8d3934b47dec10fb8c0eb5a89555279d5e3d601b02d7ceb835781ef3598c0c442668862bae16c43418290fce81158
|
data/CHANGELOG.md
CHANGED
@@ -14,12 +14,30 @@ module IdempotentRequest
|
|
14
14
|
def process(env)
|
15
15
|
set_request(env)
|
16
16
|
return app.call(request.env) unless process?
|
17
|
-
|
18
|
-
|
17
|
+
read_idempotent_request ||
|
18
|
+
write_idempotent_request ||
|
19
|
+
concurrent_request_response
|
19
20
|
end
|
20
21
|
|
21
22
|
private
|
22
23
|
|
24
|
+
def storage
|
25
|
+
@storage ||= RequestManager.new(request, config)
|
26
|
+
end
|
27
|
+
|
28
|
+
def read_idempotent_request
|
29
|
+
storage.read
|
30
|
+
end
|
31
|
+
|
32
|
+
def write_idempotent_request
|
33
|
+
return unless storage.lock
|
34
|
+
storage.write(*app.call(request.env))
|
35
|
+
end
|
36
|
+
|
37
|
+
def concurrent_request_response
|
38
|
+
[429, {}, []]
|
39
|
+
end
|
40
|
+
|
23
41
|
attr_reader :app, :env, :config, :request, :policy
|
24
42
|
|
25
43
|
def process?
|
@@ -8,14 +8,24 @@ module IdempotentRequest
|
|
8
8
|
@expire_time = config[:expire_time]
|
9
9
|
end
|
10
10
|
|
11
|
+
def lock(key)
|
12
|
+
setnx_with_expiration(lock_key(key), true)
|
13
|
+
end
|
14
|
+
|
11
15
|
def read(key)
|
12
16
|
redis.get(namespaced_key(key))
|
13
17
|
end
|
14
18
|
|
15
19
|
def write(key, payload)
|
20
|
+
setnx_with_expiration(namespaced_key(key), payload)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def setnx_with_expiration(key, data)
|
16
26
|
redis.set(
|
17
|
-
|
18
|
-
|
27
|
+
key,
|
28
|
+
data,
|
19
29
|
{}.tap do |options|
|
20
30
|
options[:nx] = true
|
21
31
|
options[:ex] = expire_time.to_i if expire_time.to_i > 0
|
@@ -23,10 +33,12 @@ module IdempotentRequest
|
|
23
33
|
)
|
24
34
|
end
|
25
35
|
|
26
|
-
|
36
|
+
def lock_key(key)
|
37
|
+
namespaced_key("lock:#{key}")
|
38
|
+
end
|
27
39
|
|
28
|
-
def namespaced_key(
|
29
|
-
[namespace,
|
40
|
+
def namespaced_key(key)
|
41
|
+
[namespace, key.strip]
|
30
42
|
.compact
|
31
43
|
.join(':')
|
32
44
|
.downcase
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: idempotent-request
|
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
|
- Dmytro Zakharov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -153,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
153
|
version: '0'
|
154
154
|
requirements: []
|
155
155
|
rubyforge_project:
|
156
|
-
rubygems_version: 2.7.
|
156
|
+
rubygems_version: 2.7.7
|
157
157
|
signing_key:
|
158
158
|
specification_version: 4
|
159
159
|
summary: Rack middleware ensuring at most once requests for mutating endpoints.
|