istox 0.2.12 → 0.2.14.2

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: e20a7e8c3bb246c95ff7ac01eb4b73049b429e8741c9347dac1d38ecfa349854
4
- data.tar.gz: 859b3ade6a2587bfac4f760205afdbce1da4b64c3cdaa99c1a96003c59a486bc
3
+ metadata.gz: 0bce8aeff0172a935744cd551d7484db72b031c6d5c1987e333556ca78371472
4
+ data.tar.gz: c64f7d731ad1295b6713225e347484c20cc52e240f33c096a5b2196f4ab3f1e0
5
5
  SHA512:
6
- metadata.gz: d9e42d23f68b197f2d446cbc3a978af16e02f71afb7694a74b3a6fd83460d0da81c13ac644e0c3bfde3f6671358f0f20f55d0b8c9bbb0ebd4c6d0cb6d0780ed7
7
- data.tar.gz: d73207c0bfcb94e29a92723f57395dd475dc94102ab186727e7567dd4e586283d9e9f14dbb3353e5e60b09ad0c7a69edc8629ef03bbe43cb10510d64a23f4793
6
+ metadata.gz: 0aff2668703907f10658e8d083224e26a15b01871aa73f7118b4405475b96d1442b8c451fa0261a173cc8b42c06c60af939fb2be60b91811e8a36818de53f19b
7
+ data.tar.gz: c5bef66c72b43dcd00f9c536a6bc8e186c1d9f657ec749346638ccc624b1762c08a1a5f9173ba609b426187353c8af40784ebfad09aee849dc1a33fe3c1efb11
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- istox (0.2.12)
4
+ istox (0.2.14.2)
5
5
  amazing_print
6
6
  awesome_print
7
7
  aws-sdk-sns (~> 1)
@@ -74,7 +74,7 @@ GEM
74
74
  aws-partitions (~> 1, >= 1.239.0)
75
75
  aws-sigv4 (~> 1.1)
76
76
  jmespath (~> 1.0)
77
- aws-sdk-sns (1.41.0)
77
+ aws-sdk-sns (1.42.0)
78
78
  aws-sdk-core (~> 3, >= 3.112.0)
79
79
  aws-sigv4 (~> 1.1)
80
80
  aws-sdk-xray (1.4.0)
@@ -112,36 +112,40 @@ GEM
112
112
  i18n (~> 0.5)
113
113
  fakeredis (0.7.0)
114
114
  redis (>= 3.2, < 5.0)
115
- faraday (1.4.3)
115
+ faraday (1.5.1)
116
116
  faraday-em_http (~> 1.0)
117
117
  faraday-em_synchrony (~> 1.0)
118
118
  faraday-excon (~> 1.1)
119
+ faraday-httpclient (~> 1.0.1)
119
120
  faraday-net_http (~> 1.0)
120
121
  faraday-net_http_persistent (~> 1.1)
122
+ faraday-patron (~> 1.0)
121
123
  multipart-post (>= 1.2, < 3)
122
124
  ruby2_keywords (>= 0.0.4)
123
125
  faraday-em_http (1.0.0)
124
126
  faraday-em_synchrony (1.0.0)
125
127
  faraday-excon (1.1.0)
128
+ faraday-httpclient (1.0.1)
126
129
  faraday-net_http (1.0.1)
127
- faraday-net_http_persistent (1.1.0)
130
+ faraday-net_http_persistent (1.2.0)
131
+ faraday-patron (1.0.0)
128
132
  faraday_middleware (1.0.0)
129
133
  faraday (~> 1.0)
130
134
  ffi (1.15.3)
131
135
  globalid (0.4.2)
132
136
  activesupport (>= 4.2.0)
133
- google-protobuf (3.17.3)
134
- googleapis-common-protos-types (1.0.6)
137
+ google-protobuf (3.17.3-universal-darwin)
138
+ googleapis-common-protos-types (1.1.0)
135
139
  google-protobuf (~> 3.14)
136
140
  graphlient (0.5.0)
137
141
  faraday (>= 1.0)
138
142
  faraday_middleware
139
143
  graphql-client
140
- graphql (1.12.12)
144
+ graphql (1.12.14)
141
145
  graphql-client (0.16.0)
142
146
  activesupport (>= 3.0)
143
147
  graphql (~> 1.8)
144
- grpc (1.38.0)
148
+ grpc (1.38.0-universal-darwin)
145
149
  google-protobuf (~> 3.15)
146
150
  googleapis-common-protos-types (~> 1.0)
147
151
  grpc-tools (1.38.0)
@@ -174,7 +178,7 @@ GEM
174
178
  nokogiri (1.11.7)
175
179
  mini_portile2 (~> 2.5.0)
176
180
  racc (~> 1.4)
177
- oj (3.11.7)
181
+ oj (3.12.2)
178
182
  ougai (2.0.0)
179
183
  oj (~> 3.10)
180
184
  paranoia (2.4.3)
@@ -260,7 +264,7 @@ GEM
260
264
  rspec-mocks (~> 3.8.0)
261
265
  rspec-support (~> 3.8.0)
262
266
  rspec-support (3.8.0)
263
- ruby2_keywords (0.0.4)
267
+ ruby2_keywords (0.0.5)
264
268
  set (1.0.1)
265
269
  slop (4.9.1)
266
270
  sorted_set (1.0.3)
data/lib/istox.rb CHANGED
@@ -27,6 +27,7 @@ module Istox
27
27
  require 'istox/helpers/graphql_client'
28
28
  require 'istox/helpers/gruf_listener_hook'
29
29
  require 'istox/helpers/messaging'
30
+ require 'istox/helpers/race_condition_helper'
30
31
  require 'istox/helpers/blockchain_service'
31
32
  require 'istox/helpers/f_math'
32
33
  require 'istox/helpers/my_open_struct'
@@ -30,6 +30,7 @@ module Istox
30
30
  request_reason: request_reason,
31
31
  resource_action: action
32
32
  )
33
+ key = nil
33
34
 
34
35
  chainhub_processing = true
35
36
 
@@ -39,6 +40,13 @@ module Istox
39
40
  # execute before proc, and get the model that blockchain receipt should bind to
40
41
  model = before.call(uuid)
41
42
 
43
+ key = model.class.name + '-' + model.id.to_s
44
+ ## for request there we need a lock to prevent there is not duplicate request exist for single model
45
+ ::Istox::RaceConditionHelper.acquire!(key) if is_request
46
+
47
+ ## validate the request, this validation is overridable by inherited class
48
+ receipt.validate_request!(model.class.name, model.id) if is_request
49
+
42
50
  # delete the previous existing blockchain receipts
43
51
  klass.where(resource_name: model.class.name, resource_id: model.id).destroy_all
44
52
 
@@ -57,11 +65,14 @@ module Istox
57
65
  chainhub_processing = false
58
66
  end
59
67
  end
68
+
69
+ ::Istox::RaceConditionHelper.release(key) if is_request
60
70
  end
61
71
 
62
72
  chainhub_processing
63
73
  rescue StandardError => e
64
74
  receipt&.destroy
75
+ ::Istox::RaceConditionHelper.release(key) if is_request && key.present?
65
76
  raise e
66
77
  end
67
78
 
@@ -13,7 +13,9 @@ module Istox
13
13
 
14
14
  result = format("%.#{precision}f", input)
15
15
 
16
- result.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
16
+ parts = result.to_s.split('.')
17
+ parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, '\\1,')
18
+ parts.join('.')
17
19
  end
18
20
 
19
21
  # format a money, eg. 20000.134 > SGD 20,000.14, position can be :front or :behind, abs_num: whether to absolute the number
@@ -0,0 +1,30 @@
1
+ module Istox
2
+ class RaceConditionHelper
3
+ class << self
4
+ def acquire!(key, expired_seconds: 20)
5
+ result = acquire(key, expired_seconds: expired_seconds)
6
+
7
+ raise 'Duplicate request detected, please refresh the page and try again.' if result == false
8
+ end
9
+
10
+ def acquire(key, expired_seconds: 20)
11
+ result = request_redis.set('race-condition||' + key.to_s, 'EMPTY', nx: true, ex: expired_seconds.to_i.seconds)
12
+
13
+ log.info("Acquiring lock for key: #{key}, expired in #{expired_seconds} seconds, result: #{result}")
14
+
15
+ # if result false means the key already exist
16
+ result
17
+ end
18
+
19
+ def release(key)
20
+ request_redis.del('race-condition||' + key.to_s)
21
+
22
+ log.info("Releasing lock for key: #{key}")
23
+ end
24
+
25
+ def request_redis
26
+ @request_redis ||= ::Istox::RedisManager.request_redis
27
+ end
28
+ end
29
+ end
30
+ end
@@ -5,5 +5,7 @@ module Istox
5
5
  def activity
6
6
  I18n.t("#{resource_name}.#{resource_action}") if resource_name.present? && resource_action.present?
7
7
  end
8
+
9
+ def validate_request!(model_name, model_id); end
8
10
  end
9
11
  end
data/lib/istox/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Istox
2
- VERSION = '0.2.12'.freeze
2
+ VERSION = '0.2.14.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: istox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.12
4
+ version: 0.2.14.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Siong Leng
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-30 00:00:00.000000000 Z
11
+ date: 2021-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: amazing_print
@@ -495,6 +495,7 @@ files:
495
495
  - lib/istox/helpers/order_book_price_time.rb
496
496
  - lib/istox/helpers/order_book_prorate.rb
497
497
  - lib/istox/helpers/publisher.rb
498
+ - lib/istox/helpers/race_condition_helper.rb
498
499
  - lib/istox/helpers/rate_limit.rb
499
500
  - lib/istox/helpers/redis_boot.rb
500
501
  - lib/istox/helpers/redis_manager.rb