redis_queued_locks 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/README.md +206 -47
- data/lib/redis_queued_locks/acquier/acquire_lock/try_to_lock.rb +235 -8
- data/lib/redis_queued_locks/acquier/acquire_lock/yield_expire.rb +115 -0
- data/lib/redis_queued_locks/acquier/acquire_lock.rb +194 -83
- data/lib/redis_queued_locks/acquier/lock_info.rb +17 -1
- data/lib/redis_queued_locks/acquier/locks.rb +9 -0
- data/lib/redis_queued_locks/client.rb +29 -2
- data/lib/redis_queued_locks/errors.rb +4 -0
- data/lib/redis_queued_locks/logging.rb +1 -1
- data/lib/redis_queued_locks/resource.rb +6 -0
- data/lib/redis_queued_locks/version.rb +2 -2
- metadata +4 -4
- data/lib/redis_queued_locks/acquier/acquire_lock/yield_with_expire.rb +0 -72
@@ -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
|