redis_queued_locks 0.0.17 → 0.0.18

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: eb745a97296f4661f7a68d7719d7bdb894521348d6aa25b92b23b4fcffce56cd
4
- data.tar.gz: e12566b1c653ad97bf973887d4511482c628d8ec27fd265901d1d4bfba9c112b
3
+ metadata.gz: '0993079d1a7cca97094c741d65c4ef1e8a5694184212b6597960e0b3c91d83cb'
4
+ data.tar.gz: 22535e254fa22db6bfb0225f391e3d4606ad680bb401034fcbdfcd8e93a1fc5c
5
5
  SHA512:
6
- metadata.gz: c3e6f4c421c8e6fb9b3933c01ebcd23d5762f0bdb0581333e2d85ab478a8002d8bce360bfabaab29d819c92a4f148e6562d0fa426e5999b0fc2d1c9b59f3eaa7
7
- data.tar.gz: 42a5800757191ea80797c7462b765c5dd4187771ce73c6c9af70f6653dea5599710d8569b5a2fc1a0c30cb0b3483abfac557bd888a7f4bcbde4ac4a7de308021
6
+ metadata.gz: 7db77251aefc59a83a9727d756774f5eb6b30a156fd058d69ec5170d252cc8fb875b2f213da2e15a4bdf9f5565565aa5ca699545ac0a83dcbc333fbed8c7aec1
7
+ data.tar.gz: 659c47a6aaa421ade9c7ffaea000a7133c1ba72f5ab0a2e66bc56f5ad72f84f4d9525fc86024e5d52b8196a6dfc7b0a714100462915a91405c97c3b3d77d3e26
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.0.18] - 2024-03-04
4
+ ### Changed
5
+ - Semantic results for methods returning `{ ok: true/false, result: Any }` hash objects.
6
+ Now these objects are represented as `RedisQueuedLocks::Data` objects inherited from `Hash`;
7
+
3
8
  ## [0.0.17] - 2024-02-29
4
9
  ### Added
5
10
  - `RedisQueuedLocks::Client#locks` - list of obtained locks;
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # RedisQueuedLocks · [![Gem Version](https://badge.fury.io/rb/redis_queued_locks.svg)](https://badge.fury.io/rb/redis_queued_locks)
1
+ # RedisQueuedLocks
2
2
 
3
3
  Distributed locks with "lock acquisition queue" capabilities based on the Redis Database.
4
4
 
@@ -244,6 +244,7 @@ Return value:
244
244
  #### #lock! - exceptional lock obtaining
245
245
 
246
246
  - fails when (and with):
247
+ - (`RedisQueuedLocks::LockAlreadyObtainedError`) when `fail_fast` is `true` and lock is already obtained;
247
248
  - (`RedisQueuedLocks::LockAcquiermentTimeoutError`) `timeout` limit reached before lock is obtained;
248
249
  - (`RedisQueuedLocks::LockAcquiermentRetryLimitError`) `retry_count` limit reached before lock is obtained;
249
250
 
@@ -8,7 +8,7 @@ module RedisQueuedLocks::Acquier::Release
8
8
  # @param redis [RedisClient]
9
9
  # @param lock_key [String]
10
10
  # @param lock_key_queue [String]
11
- # @return [Hash<Symbol,Any>] Format: { ok: true/false, result: Any }
11
+ # @return [RedisQueuedLocks::Data,Hash<Symbol,Any>] Format: { ok: true/false, result: Any }
12
12
  #
13
13
  # @api private
14
14
  # @since 0.1.0
@@ -18,14 +18,14 @@ module RedisQueuedLocks::Acquier::Release
18
18
  transact.call('EXPIRE', lock_key, '0')
19
19
  end
20
20
 
21
- { ok: true, result: }
21
+ RedisQueuedLocks::Data[ok: true, result:]
22
22
  end
23
23
 
24
24
  # Release all locks: clear all lock queus and expire all locks.
25
25
  #
26
26
  # @param redis [RedisClient]
27
27
  # @param batch_size [Integer]
28
- # @return [Hash<Symbol,Any>] Format: { ok: true/false, result: Any }
28
+ # @return [RedisQueuedLocks::Data,Hash<Symbol,Any>] Format: { ok: true/false, result: Any }
29
29
  #
30
30
  # @api private
31
31
  # @since 0.1.0
@@ -55,6 +55,6 @@ module RedisQueuedLocks::Acquier::Release
55
55
 
56
56
  rel_keys = result.count { |red_res| red_res == 0 }
57
57
 
58
- { ok: true, result: { rel_keys: rel_keys } }
58
+ RedisQueuedLocks::Data[ok: true, result: { rel_keys: rel_keys }]
59
59
  end
60
60
  end
@@ -130,13 +130,13 @@ module RedisQueuedLocks::Acquier::Try
130
130
  case
131
131
  when fail_fast && inter_result == :fail_fast_no_try
132
132
  # Step 7.a: lock is still acquired and we should exit from the logic as soon as possible
133
- { ok: false, result: inter_result }
133
+ RedisQueuedLocks::Data[ok: false, result: inter_result]
134
134
  when inter_result == :lock_is_still_acquired || inter_result == :acquier_is_not_first_in_queue
135
135
  # Step 7.b: lock is still acquired by another process => failed to acquire
136
- { ok: false, result: inter_result }
136
+ RedisQueuedLocks::Data[ok: false, result: inter_result]
137
137
  when result == nil || (result.is_a?(::Array) && result.empty?)
138
138
  # Step 7.c: lock is already acquired durign the acquire race => failed to acquire
139
- { ok: false, result: :lock_is_acquired_during_acquire_race }
139
+ RedisQueuedLocks::Data[ok: false, result: :lock_is_acquired_during_acquire_race]
140
140
  when result.is_a?(::Array) && result.size == 3 # NOTE: 3 is a count of redis lock commands
141
141
  # TODO:
142
142
  # => (!) analyze the command result and do actions with the depending on it;
@@ -147,10 +147,13 @@ module RedisQueuedLocks::Acquier::Try
147
147
  # 3. pexpire should return 1 (expiration time is successfully applied)
148
148
 
149
149
  # Step 7.d: locked! :) let's go! => successfully acquired
150
- { ok: true, result: { lock_key: lock_key, acq_id: acquier_id, ts: timestamp, ttl: ttl } }
150
+ RedisQueuedLocks::Data[
151
+ ok: true,
152
+ result: { lock_key: lock_key, acq_id: acquier_id, ts: timestamp, ttl: ttl }
153
+ ]
151
154
  else
152
155
  # Ste 7.3: unknown behaviour :thinking:
153
- { ok: false, result: :unknown }
156
+ RedisQueuedLocks::Data[ok: false, result: :unknown]
154
157
  end
155
158
  # rubocop:enable Lint/DuplicateBranch
156
159
  end
@@ -170,6 +173,6 @@ module RedisQueuedLocks::Acquier::Try
170
173
  "Step ~ [СМЕРТЬ ПРОЦЕССА]: [#{acquier_id} :: #{lock_key_queue}] РЕЗУЛЬТАТ: #{result}"
171
174
  )
172
175
 
173
- { ok: true, result: result }
176
+ RedisQueuedLocks::Data[ok: true, result: result]
174
177
  end
175
178
  end
@@ -63,7 +63,7 @@ module RedisQueuedLocks::Acquier
63
63
  # already obtained.
64
64
  # @param [Block]
65
65
  # A block of code that should be executed after the successfully acquired lock.
66
- # @return [Hash<Symbol,Any>,yield]
66
+ # @return [RedisQueuedLocks::Data,Hash<Symbol,Any>,yield]
67
67
  # - Format: { ok: true/false, result: Any }
68
68
  # - If block is given the result of block's yeld will be returned.
69
69
  #
@@ -232,7 +232,7 @@ module RedisQueuedLocks::Acquier
232
232
  end
233
233
  end
234
234
  else
235
- { ok: true, result: acq_process[:lock_info] }
235
+ RedisQueuedLocks::Data[ok: true, result: acq_process[:lock_info]]
236
236
  end
237
237
  else
238
238
  if acq_process[:result] != :retry_limit_reached &&
@@ -245,7 +245,7 @@ module RedisQueuedLocks::Acquier
245
245
  acq_process[:result] = :timeout_reached
246
246
  end
247
247
  # Step 3.b: lock is not acquired (acquier is dequeued by timeout callback)
248
- { ok: false, result: acq_process[:result] }
248
+ RedisQueuedLocks::Data[ok: false, result: acq_process[:result]]
249
249
  end
250
250
  end
251
251
  # rubocop:enable Metrics/MethodLength, Metrics/BlockNesting
@@ -257,10 +257,14 @@ module RedisQueuedLocks::Acquier
257
257
  # It is safe because the lock obtain logic is transactional and
258
258
  # watches the original lock for changes.
259
259
  #
260
- # @param redis [RedisClient] Redis connection client.
261
- # @param lock_name [String] The lock name that should be released.
262
- # @param isntrumenter [#notify] See RedisQueuedLocks::Instrument::ActiveSupport for example.
263
- # @return [Hash<Symbol,Any>] Format: { ok: true/false, result: Hash<Symbil,Numeric|String> }
260
+ # @param redis [RedisClient]
261
+ # Redis connection client.
262
+ # @param lock_name [String]
263
+ # The lock name that should be released.
264
+ # @param isntrumenter [#notify]
265
+ # See RedisQueuedLocks::Instrument::ActiveSupport for example.
266
+ # @return [RedisQueuedLocks::Data,Hash<Symbol,Any>]
267
+ # Format: { ok: true/false, result: Hash<Symbil,Numeric|String> }
264
268
  #
265
269
  # @api private
266
270
  # @since 0.1.0
@@ -283,17 +287,24 @@ module RedisQueuedLocks::Acquier
283
287
  })
284
288
  end
285
289
 
286
- { ok: true, result: { rel_time: rel_time, rel_key: lock_key, rel_queue: lock_key_queue } }
290
+ RedisQueuedLocks::Data[
291
+ ok: true,
292
+ result: { rel_time: rel_time, rel_key: lock_key, rel_queue: lock_key_queue }
293
+ ]
287
294
  end
288
295
 
289
296
  # Release all locks:
290
297
  # - 1. clear all lock queus: drop them all from Redis database by the lock queue pattern;
291
298
  # - 2. delete all locks: drop lock keys from Redis by the lock key pattern;
292
299
  #
293
- # @param redis [RedisClient] Redis connection client.
294
- # @param batch_size [Integer] The number of lock keys that should be released in a time.
295
- # @param isntrumenter [#notify] See RedisQueuedLocks::Instrument::ActiveSupport for example.
296
- # @return [Hash<Symbol,Any>] Format: { ok: true/false, result: Hash<Symbol,Numeric> }
300
+ # @param redis [RedisClient]
301
+ # Redis connection client.
302
+ # @param batch_size [Integer]
303
+ # The number of lock keys that should be released in a time.
304
+ # @param isntrumenter [#notify]
305
+ # See RedisQueuedLocks::Instrument::ActiveSupport for example.
306
+ # @return [RedisQueuedLocks::Data,Hash<Symbol,Any>]
307
+ # Format: { ok: true/false, result: Hash<Symbol,Numeric> }
297
308
  #
298
309
  # @api private
299
310
  # @since 0.1.0
@@ -312,7 +323,10 @@ module RedisQueuedLocks::Acquier
312
323
  })
313
324
  end
314
325
 
315
- { ok: true, result: { rel_key_cnt: result[:rel_keys], rel_time: rel_time } }
326
+ RedisQueuedLocks::Data[
327
+ ok: true,
328
+ result: { rel_key_cnt: result[:rel_keys], rel_time: rel_time }
329
+ ]
316
330
  end
317
331
 
318
332
  # @param redis_client [RedisClient]
@@ -92,7 +92,7 @@ class RedisQueuedLocks::Client
92
92
  # by another process while the lock request queue was initially empty;
93
93
  # @param block [Block]
94
94
  # A block of code that should be executed after the successfully acquired lock.
95
- # @return [Hash<Symbol,Any>,yield]
95
+ # @return [RedisQueuedLocks::Data,Hash<Symbol,Any>,yield]
96
96
  # - Format: { ok: true/false, result: Symbol/Hash }.
97
97
  # - If block is given the result of block's yeld will be returned.
98
98
  #
@@ -163,8 +163,10 @@ class RedisQueuedLocks::Client
163
163
  )
164
164
  end
165
165
 
166
- # @param lock_name [String] The lock name that should be released.
167
- # @return [Hash<Symbol,Any>] Format: { ok: true/false, result: Symbol/Hash }.
166
+ # @param lock_name [String]
167
+ # The lock name that should be released.
168
+ # @return [RedisQueuedLocks::Data, Hash<Symbol,Any>]
169
+ # Format: { ok: true/false, result: Symbol/Hash }.
168
170
  #
169
171
  # @api public
170
172
  # @since 0.1.0
@@ -219,7 +221,8 @@ class RedisQueuedLocks::Client
219
221
  end
220
222
 
221
223
  # @option batch_size [Integer]
222
- # @return [Hash<Symbol,Any>] Format: { ok: true/false, result: Symbol/Hash }.
224
+ # @return [RedisQueuedLocks::Data,Hash<Symbol,Any>]
225
+ # Format: { ok: true/false, result: Symbol/Hash }.
223
226
  #
224
227
  # @api public
225
228
  # @since 0.1.0
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # NOTE: wiill be rewritten with Ruby's 3.2 "Data" class;
4
+ class RedisQueuedLocks::Data < Hash
5
+ end
@@ -5,6 +5,6 @@ module RedisQueuedLocks
5
5
  #
6
6
  # @api public
7
7
  # @since 0.0.1
8
- # @version 0.0.17
9
- VERSION = '0.0.17'
8
+ # @version 0.0.18
9
+ VERSION = '0.0.18'
10
10
  end
@@ -10,6 +10,7 @@ require 'securerandom'
10
10
  module RedisQueuedLocks
11
11
  require_relative 'redis_queued_locks/version'
12
12
  require_relative 'redis_queued_locks/errors'
13
+ require_relative 'redis_queued_locks/data'
13
14
  require_relative 'redis_queued_locks/debugger'
14
15
  require_relative 'redis_queued_locks/resource'
15
16
  require_relative 'redis_queued_locks/acquier'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_queued_locks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17
4
+ version: 0.0.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rustam Ibragimov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-29 00:00:00.000000000 Z
11
+ date: 2024-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis-client
@@ -61,6 +61,7 @@ files:
61
61
  - lib/redis_queued_locks/acquier/release.rb
62
62
  - lib/redis_queued_locks/acquier/try.rb
63
63
  - lib/redis_queued_locks/client.rb
64
+ - lib/redis_queued_locks/data.rb
64
65
  - lib/redis_queued_locks/debugger.rb
65
66
  - lib/redis_queued_locks/debugger/interface.rb
66
67
  - lib/redis_queued_locks/errors.rb