Refactor how Redis locks are created (#18400)
* Refactor how Redis locks are created * Fix autorelease duration on account deletion lock
This commit is contained in:
		@@ -3,6 +3,7 @@
 | 
			
		||||
class ActivityPub::Activity
 | 
			
		||||
  include JsonLdHelper
 | 
			
		||||
  include Redisable
 | 
			
		||||
  include Lockable
 | 
			
		||||
 | 
			
		||||
  SUPPORTED_TYPES = %w(Note Question).freeze
 | 
			
		||||
  CONVERTED_TYPES = %w(Image Audio Video Article Page Event).freeze
 | 
			
		||||
@@ -157,22 +158,6 @@ class ActivityPub::Activity
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def lock_or_return(key, expire_after = 2.hours.seconds)
 | 
			
		||||
    yield if redis.set(key, true, nx: true, ex: expire_after)
 | 
			
		||||
  ensure
 | 
			
		||||
    redis.del(key)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def lock_or_fail(key, expire_after = 15.minutes.seconds)
 | 
			
		||||
    RedisLock.acquire({ redis: redis, key: key, autorelease: expire_after }) do |lock|
 | 
			
		||||
      if lock.acquired?
 | 
			
		||||
        yield
 | 
			
		||||
      else
 | 
			
		||||
        raise Mastodon::RaceConditionError
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def fetch?
 | 
			
		||||
    !@options[:delivery]
 | 
			
		||||
  end
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user