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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 25467dbc0ee42d2648e2dd267edb23be7972a6af0ba2d233e21d455c663fc643
4
- data.tar.gz: 4dc4e7373dce6370eb850134d25c164fd67ef5c54dc12c3585267d4774d1f58f
3
+ metadata.gz: d52f16d55268a056fc1a8239d3112fcc4ef621442e78f5f4100a6093d3279711
4
+ data.tar.gz: fd9bd7e9290369d1ec2e583eea23bd3ac41d2b1b143cff1faebcc3c65ab17565
5
5
  SHA512:
6
- metadata.gz: 9af0cf3e406cbea6807cdfedc32110984ac63225dbe9ffe671cfcd8b842dd639db8e80ead393b1bbb7a14d6977f4edbe02aec913f4a5b2e41093a8cf12577c23
7
- data.tar.gz: b54740669825c315a164fb42a4a58e54fd1ac1cc6e2802d3c40f4c8e5123bbfb9742e93f53f42667c392982e68784ff3721cb6123e7ea77b41cd4ca3ae762dca
6
+ metadata.gz: 55daca8c55cc835f24a72c94e72636c3935e27d223238c34fec6d5795fd7b1ec5e6bda6da277783ba3f97af68451356583cbffc02b65791cd6bee2518f4f5ce3
7
+ data.tar.gz: 04cf35af5be08d455d9aca35f5a41c7924b8d3934b47dec10fb8c0eb5a89555279d5e3d601b02d7ceb835781ef3598c0c442668862bae16c43418290fce81158
@@ -1,5 +1,9 @@
1
1
  # Idempotent Request Changelog #
2
2
 
3
+ ## v0.1.3 ##
4
+
5
+ * Fix an issue, when concurrent requests sent to an endpoint won't be protected by idempotency until the 1st request is finished
6
+
3
7
  ## v0.1.2 ##
4
8
 
5
9
  * Fix rack response
@@ -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
- storage = RequestManager.new(request, config)
18
- storage.read || storage.write(*app.call(request.env))
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
- namespaced_key(key),
18
- payload,
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
- private
36
+ def lock_key(key)
37
+ namespaced_key("lock:#{key}")
38
+ end
27
39
 
28
- def namespaced_key(idempotency_key)
29
- [namespace, idempotency_key.strip]
40
+ def namespaced_key(key)
41
+ [namespace, key.strip]
30
42
  .compact
31
43
  .join(':')
32
44
  .downcase
@@ -8,6 +8,10 @@ module IdempotentRequest
8
8
  @callback = config[:callback]
9
9
  end
10
10
 
11
+ def lock
12
+ storage.lock(key)
13
+ end
14
+
11
15
  def read
12
16
  status, headers, response = parse_data(storage.read(key)).values
13
17
 
@@ -1,3 +1,3 @@
1
1
  module IdempotentRequest
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
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.2
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-04-24 00:00:00.000000000 Z
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.6
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.