redis_queued_locks 1.23.0 → 1.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/.claude/project-overview.md +70 -33
  3. data/.claude/rules/acquirer.md +6 -3
  4. data/.claude/rules/arguments.md +2 -2
  5. data/.claude/rules/git.md +1 -1
  6. data/.claude/rules/logic.md +5 -4
  7. data/.claude/rules/readme.md +40 -0
  8. data/.claude/rules/swarm.md +2 -2
  9. data/.claude/rules/tests.md +3 -2
  10. data/.claude/rules/type-checking.md +3 -2
  11. data/.claude/rules/visitors.md +3 -2
  12. data/.claude/skills/rql-code-review/SKILL.md +6 -3
  13. data/.claude/skills/rql-project-full-review/SKILL.md +171 -0
  14. data/.claude/skills/rql-project-full-review/templates/report.en.md +41 -0
  15. data/.claude/skills/rql-project-full-review/templates/report.ru.md +41 -0
  16. data/CHANGELOG.md +30 -9
  17. data/CLAUDE.md +7 -5
  18. data/README.md +390 -17
  19. data/lib/redis_queued_locks/acquirer/acquire_lock/dequeue_from_lock_queue.rb +60 -1
  20. data/lib/redis_queued_locks/acquirer/acquire_lock/try_to_lock/log_visitor.rb +38 -1
  21. data/lib/redis_queued_locks/acquirer/acquire_lock/try_to_lock.rb +31 -4
  22. data/lib/redis_queued_locks/acquirer/acquire_lock/with_acq_timeout.rb +6 -3
  23. data/lib/redis_queued_locks/acquirer/acquire_lock/yield_expire.rb +257 -32
  24. data/lib/redis_queued_locks/acquirer/acquire_lock.rb +103 -4
  25. data/lib/redis_queued_locks/acquirer/lock_series/try_to_lock_series.rb +27 -1
  26. data/lib/redis_queued_locks/acquirer/lock_series.rb +160 -15
  27. data/lib/redis_queued_locks/client.rb +96 -16
  28. data/lib/redis_queued_locks/config/dsl.rb +2 -0
  29. data/lib/redis_queued_locks/config.rb +35 -11
  30. data/lib/redis_queued_locks/errors.rb +19 -2
  31. data/lib/redis_queued_locks/lock_keeper/instr_visitor.rb +173 -0
  32. data/lib/redis_queued_locks/lock_keeper/lease.rb +434 -0
  33. data/lib/redis_queued_locks/lock_keeper/lock_lease.rb +106 -0
  34. data/lib/redis_queued_locks/lock_keeper/log_visitor.rb +187 -0
  35. data/lib/redis_queued_locks/lock_keeper.rb +1007 -0
  36. data/lib/redis_queued_locks/resource.rb +1 -1
  37. data/lib/redis_queued_locks/swarm/swarm_element/threaded.rb +2 -2
  38. data/lib/redis_queued_locks/utilities.rb +1 -15
  39. data/lib/redis_queued_locks/version.rb +2 -2
  40. data/lib/redis_queued_locks.rb +1 -0
  41. data/sig/redis_queued_locks/acquirer/acquire_lock/dequeue_from_lock_queue.rbs +14 -0
  42. data/sig/redis_queued_locks/acquirer/acquire_lock/try_to_lock/log_visitor.rbs +12 -0
  43. data/sig/redis_queued_locks/acquirer/acquire_lock/try_to_lock.rbs +2 -0
  44. data/sig/redis_queued_locks/acquirer/acquire_lock/yield_expire.rbs +30 -2
  45. data/sig/redis_queued_locks/acquirer/acquire_lock.rbs +6 -1
  46. data/sig/redis_queued_locks/acquirer/lock_series.rbs +12 -1
  47. data/sig/redis_queued_locks/client.rbs +14 -0
  48. data/sig/redis_queued_locks/config.rbs +15 -1
  49. data/sig/redis_queued_locks/errors.rbs +12 -0
  50. data/sig/redis_queued_locks/instrument/void_notifier.rbs +2 -2
  51. data/sig/redis_queued_locks/lock_keeper/instr_visitor.rbs +67 -0
  52. data/sig/redis_queued_locks/lock_keeper/lease.rbs +78 -0
  53. data/sig/redis_queued_locks/lock_keeper/lock_lease.rbs +19 -0
  54. data/sig/redis_queued_locks/lock_keeper/log_visitor.rbs +62 -0
  55. data/sig/redis_queued_locks/lock_keeper.rbs +108 -0
  56. data/sig/redis_queued_locks/utilities.rbs +0 -2
  57. data/sig/vendor/redis_client.rbs +1 -0
  58. metadata +15 -1
@@ -0,0 +1,434 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The locks of one block of code that are kept alive by the lock keeper
4
+ # (`RedisQueuedLocks::LockKeeper`): one lock (`#lock`) or all locks of a series (`#lock_series`):
5
+ # - one lease per block of code, not per lock: the block is interrupted once and the hold limit
6
+ # is the limit of the whole block;
7
+ # - the locks are parallel arrays in the order of the lock names;
8
+ # - the time values are the milliseconds of the monotonic clock;
9
+ # - the extension schedule is changed by the keeper thread only; the block of code reads `#lost?`
10
+ # and moves the hold limit (`RedisQueuedLocks::LockKeeper::LockLease#hold_for`) under the lock
11
+ # of the keeper;
12
+ #
13
+ # @api private
14
+ # @since 1.24.0
15
+ # rubocop:disable Metrics/ClassLength
16
+ class RedisQueuedLocks::LockKeeper::Lease
17
+ # @return [Array<String>]
18
+ #
19
+ # @api private
20
+ # @since 1.24.0
21
+ attr_reader :lock_keys
22
+
23
+ # @return [Array<String>]
24
+ #
25
+ # @api private
26
+ # @since 1.24.0
27
+ attr_reader :lock_readers_keys
28
+
29
+ # @return [Array<String,NilClass>]
30
+ #
31
+ # @api private
32
+ # @since 1.24.0
33
+ attr_reader :read_lock_keys
34
+
35
+ # The modes of the really held locks (a read lock request under the own write lock holds the
36
+ # write lock).
37
+ #
38
+ # @return [Array<Symbol>]
39
+ #
40
+ # @api private
41
+ # @since 1.24.0
42
+ attr_reader :rw_modes
43
+
44
+ # @return [Array<String>]
45
+ #
46
+ # @api private
47
+ # @since 1.24.0
48
+ attr_reader :lock_names
49
+
50
+ # @return [String]
51
+ #
52
+ # @api private
53
+ # @since 1.24.0
54
+ attr_reader :acquirer_id
55
+
56
+ # @return [String]
57
+ #
58
+ # @api private
59
+ # @since 1.24.0
60
+ attr_reader :host_id
61
+
62
+ # How long the lock lives after an extension (in milliseconds).
63
+ #
64
+ # @return [Integer]
65
+ #
66
+ # @api private
67
+ # @since 1.24.0
68
+ attr_reader :extension_ttl
69
+
70
+ # The time between the extensions (in milliseconds).
71
+ #
72
+ # @return [Integer]
73
+ #
74
+ # @api private
75
+ # @since 1.24.0
76
+ attr_reader :extension_interval
77
+
78
+ # @return [Integer,NilClass]
79
+ #
80
+ # @api private
81
+ # @since 1.24.0
82
+ attr_reader :max_hold_time_during_auto_extend
83
+
84
+ # The start of the attempt that obtained the locks.
85
+ #
86
+ # @return [Integer]
87
+ #
88
+ # @api private
89
+ # @since 1.24.0
90
+ attr_reader :started_at
91
+
92
+ # @return [Integer]
93
+ #
94
+ # @api private
95
+ # @since 1.24.0
96
+ attr_reader :next_extension_at
97
+
98
+ # @return [Boolean]
99
+ #
100
+ # @api private
101
+ # @since 1.24.0
102
+ attr_reader :lock_index_enabled
103
+
104
+ # @return [Integer]
105
+ #
106
+ # @api private
107
+ # @since 1.24.0
108
+ attr_reader :lock_index_grace_period
109
+
110
+ # The fiber of the running `timed` block of code (`nil`: the block is not `timed`, it is not
111
+ # started yet or it is completed: `RedisQueuedLocks::LockKeeper#forget`): the block that finds its
112
+ # lock lost by itself (`RedisQueuedLocks::LockKeeper::LockLease#hold_for`) is interrupted right
113
+ # there, in any fiber. See `RedisQueuedLocks::LockKeeper#interrupt_timed_block`.
114
+ #
115
+ # @return [Fiber,NilClass]
116
+ #
117
+ # @api private
118
+ # @since 1.24.0
119
+ attr_accessor :timed_fiber
120
+
121
+ # The thread of the `timed` block of code of a blocking fiber: it is interrupted with
122
+ # `Thread#raise` when a lock is lost or the hold limit is reached (`nil`: see `#fiber_scheduler`).
123
+ #
124
+ # @return [Thread,NilClass]
125
+ #
126
+ # @api private
127
+ # @since 1.24.0
128
+ attr_accessor :interrupted_thread
129
+
130
+ # The fiber scheduler of the `timed` block of code of a non-blocking fiber: the block is
131
+ # interrupted through its `Fiber::Scheduler#fiber_interrupt` (`nil`: a blocking fiber or a
132
+ # scheduler without this hook: the keeper does not interrupt the block, its hold limit is a
133
+ # `Timeout`).
134
+ #
135
+ # @return [#fiber_interrupt,NilClass]
136
+ #
137
+ # @api private
138
+ # @since 1.24.0
139
+ attr_accessor :fiber_scheduler
140
+
141
+ # @return [::Logger,#debug]
142
+ #
143
+ # @api private
144
+ # @since 1.24.0
145
+ attr_reader :logger
146
+
147
+ # @return [Boolean]
148
+ #
149
+ # @api private
150
+ # @since 1.24.0
151
+ attr_reader :log_sampled
152
+
153
+ # @return [#notify]
154
+ #
155
+ # @api private
156
+ # @since 1.24.0
157
+ attr_reader :instrumenter
158
+
159
+ # @return [Boolean]
160
+ #
161
+ # @api private
162
+ # @since 1.24.0
163
+ attr_reader :instr_sampled
164
+
165
+ # @return [NilClass,Any]
166
+ #
167
+ # @api private
168
+ # @since 1.24.0
169
+ attr_reader :instrument
170
+
171
+ # @param lock_keys [Array<String>]
172
+ # @param lock_readers_keys [Array<String>]
173
+ # @param read_lock_keys [Array<String,NilClass>]
174
+ # @param rw_modes [Array<Symbol>]
175
+ # @param lock_names [Array<String>]
176
+ # @param acquirer_id [String]
177
+ # @param host_id [String]
178
+ # @param started_at [Integer] The start of the attempt that obtained the locks.
179
+ # @param ttl [Integer]
180
+ # The time the locks live from the attempt (in milliseconds), `0`: unknown (see
181
+ # `RedisQueuedLocks::LockKeeper#keep`).
182
+ # @param extension_ttl [Integer] How long the lock lives after an extension (in milliseconds).
183
+ # @param max_hold_time_during_auto_extend [Integer,NilClass]
184
+ # The hold limit (in milliseconds), `nil`: no limit.
185
+ # @param lock_index_enabled [Boolean]
186
+ # @param lock_index_grace_period [Integer]
187
+ # @param logger [::Logger,#debug]
188
+ # @param log_sampled [Boolean]
189
+ # @param instrumenter [#notify]
190
+ # @param instr_sampled [Boolean]
191
+ # @param instrument [NilClass,Any]
192
+ # @return [void]
193
+ #
194
+ # @api private
195
+ # @since 1.24.0
196
+ # rubocop:disable Metrics/MethodLength
197
+ def initialize(
198
+ lock_keys,
199
+ lock_readers_keys,
200
+ read_lock_keys,
201
+ rw_modes,
202
+ lock_names,
203
+ acquirer_id,
204
+ host_id,
205
+ started_at,
206
+ ttl,
207
+ extension_ttl,
208
+ max_hold_time_during_auto_extend,
209
+ lock_index_enabled,
210
+ lock_index_grace_period,
211
+ logger,
212
+ log_sampled,
213
+ instrumenter,
214
+ instr_sampled,
215
+ instrument
216
+ )
217
+ @lock_keys = lock_keys
218
+ @lock_readers_keys = lock_readers_keys
219
+ @read_lock_keys = read_lock_keys
220
+ @rw_modes = rw_modes
221
+ @lock_names = lock_names
222
+ @acquirer_id = acquirer_id
223
+ @host_id = host_id
224
+ @extension_ttl = extension_ttl
225
+ @extension_interval = [extension_ttl / 3, 1].max
226
+ @max_hold_time_during_auto_extend = max_hold_time_during_auto_extend
227
+ @started_at = started_at
228
+ @hold_deadline =
229
+ max_hold_time_during_auto_extend ? (started_at + max_hold_time_during_auto_extend) : nil
230
+ @lock_index_enabled = lock_index_enabled
231
+ @lock_index_grace_period = lock_index_grace_period
232
+ @timed_fiber = nil
233
+ @interrupted_thread = nil
234
+ @fiber_scheduler = nil
235
+ # NOTE: the interruption that is sent through the fiber scheduler and has not come yet;
236
+ @pending_interruption = nil
237
+ @logger = logger
238
+ @log_sampled = log_sampled
239
+ @instrumenter = instrumenter
240
+ @instr_sampled = instr_sampled
241
+ @instrument = instrument
242
+ @lost_locks = Array.new(lock_keys.size, false)
243
+ @lost = false
244
+ @hold_stopped = false
245
+ @hold_moved = false
246
+ # NOTE: the first extension:
247
+ # - `ttl / 3`: the lock has to be extended while it is alive (the ttl can be shorter than the
248
+ # extension ttl);
249
+ # - `ttl - extension_ttl * 2 / 3`: an earlier extension of a long ttl changes nothing (it
250
+ # never shortens the lock): the same remaining time as before all other extensions;
251
+ # - `0` ttl (a reentrant lock that is not extended by the attempt: its remaining ttl is
252
+ # unknown): at once;
253
+ @next_extension_at = 0
254
+ schedule_extension(started_at + [ttl / 3, ttl - (extension_ttl * 2 / 3)].max)
255
+ end
256
+ # rubocop:enable Metrics/MethodLength
257
+
258
+ # @return [Boolean] At least one lock of the lease is lost.
259
+ #
260
+ # @api private
261
+ # @since 1.24.0
262
+ def lost?
263
+ @lost
264
+ end
265
+
266
+ # @param lock_index [Integer]
267
+ # @return [Boolean]
268
+ #
269
+ # @api private
270
+ # @since 1.24.0
271
+ def lock_lost?(lock_index)
272
+ @lost_locks[lock_index]
273
+ end
274
+
275
+ # @param lock_index [Integer]
276
+ # @return [void]
277
+ #
278
+ # @api private
279
+ # @since 1.24.0
280
+ def lose_lock(lock_index)
281
+ @lost_locks[lock_index] = true
282
+ @lost = true
283
+ end
284
+
285
+ # @return [Boolean]
286
+ #
287
+ # @api private
288
+ # @since 1.24.0
289
+ def all_locks_lost?
290
+ @lost_locks.all?
291
+ end
292
+
293
+ # @return [Boolean]
294
+ # The keeper can interrupt the `timed` block of code: its hold limit needs no `Timeout`.
295
+ #
296
+ # @api private
297
+ # @since 1.24.0
298
+ def interruptible?
299
+ (@interrupted_thread || @fiber_scheduler) ? true : false
300
+ end
301
+
302
+ # Interrupts the `timed` block of code from another thread or fiber with
303
+ # `RedisQueuedLocks::TimedLockIntermediateTimeoutError` (under the lock of the keeper: see
304
+ # `RedisQueuedLocks::LockKeeper#forget`; nothing happens when the block can not be interrupted):
305
+ # - a blocking fiber: `Thread#raise`;
306
+ # - a non-blocking fiber: the fiber scheduler raises the error in the fiber at its next
307
+ # suspension point (`Fiber::Scheduler#fiber_interrupt`, it may be called by another thread);
308
+ # a sent interruption can not be cancelled: it is pending till the block of code takes it
309
+ # (`#take_pending_interruption`);
310
+ #
311
+ # @param message [String]
312
+ # @return [void]
313
+ #
314
+ # @api private
315
+ # @since 1.24.0
316
+ def interrupt(message)
317
+ if (interrupted_thread = @interrupted_thread)
318
+ interrupted_thread.raise(RedisQueuedLocks::TimedLockIntermediateTimeoutError, message)
319
+ elsif (fiber_scheduler = @fiber_scheduler) && (timed_fiber = @timed_fiber)
320
+ error = RedisQueuedLocks::TimedLockIntermediateTimeoutError.new(message)
321
+ @pending_interruption = error
322
+ begin
323
+ fiber_scheduler.fiber_interrupt(timed_fiber, error)
324
+ rescue StandardError
325
+ # NOTE: the scheduler can not interrupt the fiber: the loss is only reported (like in a
326
+ # scheduler without the hook), nothing is pending;
327
+ @pending_interruption = nil
328
+ end
329
+ end
330
+ end
331
+
332
+ # The interruption sent through the fiber scheduler that has not come to the block of code yet
333
+ # (it is taken once: the next call returns `nil`). Under the lock of the keeper.
334
+ #
335
+ # @param raised_error [Exception,NilClass] The error that has stopped the block of code.
336
+ # @return [RedisQueuedLocks::TimedLockIntermediateTimeoutError,NilClass]
337
+ #
338
+ # @api private
339
+ # @since 1.24.0
340
+ def take_pending_interruption(raised_error)
341
+ pending_interruption = @pending_interruption
342
+ @pending_interruption = nil
343
+ pending_interruption.equal?(raised_error) ? nil : pending_interruption
344
+ end
345
+
346
+ # @param time [Integer]
347
+ # @return [Boolean]
348
+ #
349
+ # @api private
350
+ # @since 1.24.0
351
+ def hold_limit_reached?(time)
352
+ hold_deadline = @hold_deadline
353
+ hold_deadline ? time >= hold_deadline : false
354
+ end
355
+
356
+ # Moves the hold limit to the given moment, never back (no limit stays no limit). Under the lock
357
+ # of the keeper: see `RedisQueuedLocks::LockKeeper#hold_for`.
358
+ #
359
+ # @param hold_deadline [Integer]
360
+ # @return [void]
361
+ #
362
+ # @api private
363
+ # @since 1.24.0
364
+ def hold_until(hold_deadline)
365
+ current_hold_deadline = @hold_deadline
366
+ return unless current_hold_deadline && hold_deadline > current_hold_deadline
367
+
368
+ @hold_deadline = hold_deadline
369
+ @hold_moved = true
370
+ end
371
+
372
+ # @return [Boolean]
373
+ # The hold limit is moved by the block of code (it is not `max_hold_time_during_auto_extend`).
374
+ #
375
+ # @api private
376
+ # @since 1.24.0
377
+ def hold_moved?
378
+ @hold_moved
379
+ end
380
+
381
+ # @param time [Integer]
382
+ # @return [Integer,NilClass] The time left till the hold limit (in milliseconds), `nil`: no limit.
383
+ #
384
+ # @api private
385
+ # @since 1.24.0
386
+ def hold_time_left(time)
387
+ hold_deadline = @hold_deadline
388
+ hold_deadline ? [hold_deadline - time, 0].max : nil
389
+ end
390
+
391
+ # The hold limit is reached and the locks are not extended anymore (under the lock of the keeper).
392
+ #
393
+ # @return [void]
394
+ #
395
+ # @api private
396
+ # @since 1.24.0
397
+ def stop_hold
398
+ @hold_stopped = true
399
+ end
400
+
401
+ # @return [Boolean]
402
+ #
403
+ # @api private
404
+ # @since 1.24.0
405
+ def hold_stopped?
406
+ @hold_stopped
407
+ end
408
+
409
+ # How long the lock lives after an extension at the given time: the extension ttl without
410
+ # exceeding the hold limit.
411
+ #
412
+ # @param time [Integer]
413
+ # @return [Integer] In milliseconds.
414
+ #
415
+ # @api private
416
+ # @since 1.24.0
417
+ def extension_ttl_at(time)
418
+ hold_deadline = @hold_deadline
419
+ return extension_ttl unless hold_deadline
420
+
421
+ [hold_deadline - time, extension_ttl].min #: Integer
422
+ end
423
+
424
+ # @param time [Integer]
425
+ # @return [void]
426
+ #
427
+ # @api private
428
+ # @since 1.24.0
429
+ def schedule_extension(time)
430
+ hold_deadline = @hold_deadline
431
+ @next_extension_at = (hold_deadline && hold_deadline < time) ? hold_deadline : time
432
+ end
433
+ end
434
+ # rubocop:enable Metrics/ClassLength
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The lease of an auto extended block of code (`auto_extend: true` of `#lock`, `#lock!`,
4
+ # `#lock_series`, `#lock_series!`): the block of code gets it as its argument when it takes one.
5
+ #
6
+ # rql.lock(
7
+ # 'report', ttl: 10_000, auto_extend: true, max_hold_time_during_auto_extend: 600_000
8
+ # ) do |lease|
9
+ # batches.each do |batch|
10
+ # process(batch)
11
+ # lease.hold_for(120_000) # the progress is made: keep the lock two more minutes from now
12
+ # end
13
+ # save_report unless lease.lost?
14
+ # end
15
+ #
16
+ # - the hold limit (`max_hold_time_during_auto_extend`) is the limit of a stuck block of code:
17
+ # a block that makes progress moves it (`#hold_for`), a stuck one stops moving it and its lock
18
+ # expires at the last limit (a dead man's switch);
19
+ # - one lease per block of code: a lock series has one lease for all its locks, a nested block
20
+ # with `auto_extend: true` has its own one;
21
+ # - a block of code without arguments (a lambda without parameters too) gets no lease: nothing is
22
+ # allocated for it;
23
+ # - the lease belongs to its block of code: it is useless after the block (`:released`);
24
+ #
25
+ # @api public
26
+ # @since 1.24.0
27
+ class RedisQueuedLocks::LockKeeper::LockLease
28
+ # @param lock_keeper [RedisQueuedLocks::LockKeeper]
29
+ # @param lease [RedisQueuedLocks::LockKeeper::Lease]
30
+ # @param redis [RedisClient] The connection of the client.
31
+ # @return [void]
32
+ #
33
+ # @api private
34
+ # @since 1.24.0
35
+ def initialize(lock_keeper, lease, redis)
36
+ @lock_keeper = lock_keeper
37
+ @lease = lease
38
+ @redis = redis
39
+ end
40
+
41
+ # Moves the hold limit of the block of code: the locks are kept at least `hold_time`
42
+ # milliseconds from now, and they are extended at once (one round trip with the connection of
43
+ # the client).
44
+ #
45
+ # - the time is counted from now, it is not a sum: the calls in a loop do not accumulate it,
46
+ # and the limit never moves back (the latest limit wins);
47
+ # - it can move the limit beyond `max_hold_time_during_auto_extend`: the block that makes
48
+ # progress decides; with `max_hold_time_during_auto_extend: nil` (no limit) it only extends
49
+ # the locks and checks their owner;
50
+ # - each extension of the keeper is still `auto_extend_ttl` at most: the crash of the process
51
+ # releases the lock in `auto_extend_ttl`, whatever the limit is;
52
+ # - `timed: true`: the limit of the block of code is moved too (the block is interrupted at the
53
+ # moved limit; a non-blocking fiber whose fiber scheduler has no
54
+ # `Fiber::Scheduler#fiber_interrupt` keeps the `Timeout` of
55
+ # `max_hold_time_during_auto_extend`);
56
+ # a lost lock interrupts such block right here (`RedisQueuedLocks::TimedBlockLostLockError`),
57
+ # in any fiber;
58
+ # - call it per a unit of progress (a batch), not per item: each call is a round trip;
59
+ # - from another thread: only with a connection pool (the connection of the client is used,
60
+ # like by any method of the client);
61
+ #
62
+ # @param hold_time [Integer] In milliseconds, a positive Integer.
63
+ # @return [Hash<Symbol,Boolean|Symbol>]
64
+ # Format: { ok: true, result: :extended } or { ok: false, result: Symbol }:
65
+ # - `:extended` - the hold limit is moved, all locks are owned and extended;
66
+ # - `:lock_lost` - a lock of the block of code is lost (`auto_extended_lock_lost` is
67
+ # reported); the next calls do not move the limit;
68
+ # - `:hold_limit_reached` - the hold limit is already reached: the locks are not extended
69
+ # anymore (`lock_hold_limit_reached` is reported), the limit can not be moved back;
70
+ # - `:released` - the block of code is completed and its locks are released;
71
+ #
72
+ # @raise [RedisQueuedLocks::ArgumentError]
73
+ #
74
+ # @api public
75
+ # @since 1.24.0
76
+ def hold_for(hold_time)
77
+ unless hold_time.is_a?(::Integer) && hold_time > 0
78
+ raise(
79
+ RedisQueuedLocks::ArgumentError,
80
+ "`hold_time` argument should be a positive Integer, got #{hold_time.inspect}."
81
+ )
82
+ end
83
+
84
+ @lock_keeper.hold_for(@redis, @lease, hold_time)
85
+ end
86
+
87
+ # A local check without Redis: a lost lock is found by an extension (each `auto_extend_ttl / 3`)
88
+ # or by `#hold_for`, so `false` does not prove that the lock is still owned right now.
89
+ #
90
+ # @return [Boolean] A lock of the block of code is lost.
91
+ #
92
+ # @api public
93
+ # @since 1.24.0
94
+ def lost?
95
+ @lease.lost?
96
+ end
97
+
98
+ # @return [Integer,NilClass]
99
+ # The time left till the hold limit (in milliseconds, `0` when it is reached), `nil`: no limit.
100
+ #
101
+ # @api public
102
+ # @since 1.24.0
103
+ def hold_time_left
104
+ @lock_keeper.hold_time_left(@lease)
105
+ end
106
+ end