redis_queued_locks 1.2.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,72 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # @api private
4
- # @since 1.0.0
5
- module RedisQueuedLocks::Acquier::AcquireLock::YieldWithExpire
6
- # @since 1.0.0
7
- extend RedisQueuedLocks::Utilities
8
-
9
- # @param redis [RedisClient] Redis connection manager.
10
- # @param logger [::Logger,#debug] Logger object.
11
- # @param lock_key [String] Lock key to be expired.
12
- # @param acquier_id [String] Acquier identifier.
13
- # @param timed [Boolean] Should the lock be wrapped by Tiemlout with with lock's ttl
14
- # @param ttl_shift [Float] Lock's TTL shifting. Should affect block's ttl. In millisecodns.
15
- # @param ttl [Integer,NilClass] Lock's time to live (in ms). Nil means "without timeout".
16
- # @param queue_ttl [Integer] Lock request lifetime.
17
- # @param block [Block] Custom logic that should be invoked unter the obtained lock.
18
- # @return [Any,NilClass] nil is returned no block parametr is provided.
19
- #
20
- # @api private
21
- # @since 1.0.0
22
- def yield_with_expire(
23
- redis,
24
- logger,
25
- lock_key,
26
- acquier_id,
27
- timed,
28
- ttl_shift,
29
- ttl,
30
- queue_ttl,
31
- &block
32
- )
33
- if block_given?
34
- if timed && ttl != nil
35
- timeout = ((ttl - ttl_shift) / 1000.0).yield_self { |time| (time < 0) ? 0.0 : time }
36
- yield_with_timeout(timeout, lock_key, ttl, &block)
37
- else
38
- yield
39
- end
40
- end
41
- ensure
42
- run_non_critical do
43
- logger.debug do
44
- "[redis_queued_locks.expire_lock] " \
45
- "lock_key => '#{lock_key}' " \
46
- "queue_ttl => #{queue_ttl} " \
47
- "acq_id => '#{acquier_id}'"
48
- end
49
- end
50
- redis.call('EXPIRE', lock_key, '0')
51
- end
52
-
53
- private
54
-
55
- # @param timeout [Float]
56
- # @parma lock_key [String]
57
- # @param lock_ttl [Integer,NilClass]
58
- # @param block [Blcok]
59
- # @return [Any]
60
- #
61
- # @api private
62
- # @since 1.0.0
63
- def yield_with_timeout(timeout, lock_key, lock_ttl, &block)
64
- ::Timeout.timeout(timeout, &block)
65
- rescue ::Timeout::Error
66
- raise(
67
- RedisQueuedLocks::TimedLockTimeoutError,
68
- "Passed <timed> block of code exceeded " \
69
- "the lock TTL (lock: \"#{lock_key}\", ttl: #{lock_ttl})"
70
- )
71
- end
72
- end