FIX: Off-by-one error setting the distributed mutex key to expire

Accounting for fractional seconds, a distributed mutex can be held for
almost a full second longer than its validity.

For example: if we grab the lock at 10.5 seconds passed the epoch with a
validity of 5 seconds, the lock would be released at 16 seconds passed
the epoch. However, in this case assuming that all other processing
takes a negligible amount of time, the key would be expired at 15.5
seconds passed the epoch.

Using expireat, the key is now expired exactly when the lock is released.
This commit is contained in:
Daniel Waterworth 2020-02-03 12:23:02 +00:00
parent e7a511fc7f
commit 7c7098c700

View File

@ -95,7 +95,7 @@ class DistributedMutex
result =
redis.multi do
redis.set key, expire_time.to_s
redis.expire key, validity
redis.expireat key, expire_time + 1
end
got_lock = !result.nil?