Sha256: fc7f4ea7e88297b61e18d7051eb2e8043fe669bee22ef327de28b0cf953224e0

Contents?: true

Size: 1.7 KB

Versions: 10

Compression:

Stored size: 1.7 KB

Contents

require "redis"

module MailRoom
  module Arbitration
    class Redis
      Options = Struct.new(:redis_url, :namespace, :sentinels) do
        def initialize(mailbox)
          redis_url = mailbox.arbitration_options[:redis_url] || "redis://localhost:6379"
          namespace = mailbox.arbitration_options[:namespace]
          sentinels = mailbox.arbitration_options[:sentinels]

          super(redis_url, namespace, sentinels)
        end
      end

      # Expire after 10 minutes so Redis doesn't get filled up with outdated data.
      EXPIRATION = 600

      attr_accessor :options

      def initialize(options)
        @options = options
      end

      def deliver?(uid, expiration = EXPIRATION)
        key = "delivered:#{uid}"

        # Set the key, but only if it doesn't already exist;
        # the return value is true if successful, false if the key was already set,
        # which is conveniently the correct return value for this method
        # Any subsequent failure in the instance which gets the lock will be dealt
        # with by the expiration, at which time another instance can pick up the
        # message and try again.
        client.set(key, 1, {:nx => true, :ex => expiration})
      end

      private

      def client
        @client ||= begin
          sentinels = options.sentinels
          redis_options = { url: options.redis_url }
          redis_options[:sentinels] = sentinels if sentinels

          redis = ::Redis.new(redis_options)

          namespace = options.namespace
          if namespace
            require 'redis/namespace'
            ::Redis::Namespace.new(namespace, redis: redis)
          else
            redis
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
gitlab-mail_room-0.0.11 lib/mail_room/arbitration/redis.rb
gitlab-mail_room-0.0.10 lib/mail_room/arbitration/redis.rb
gitlab-mail_room-0.0.9 lib/mail_room/arbitration/redis.rb
gitlab-mail_room-0.0.8 lib/mail_room/arbitration/redis.rb
gitlab-mail_room-0.0.7 lib/mail_room/arbitration/redis.rb
gitlab-mail_room-0.0.6 lib/mail_room/arbitration/redis.rb
gitlab-mail_room-0.0.4 lib/mail_room/arbitration/redis.rb
gitlab-mail_room-0.0.3 lib/mail_room/arbitration/redis.rb
gitlab-mail_room-0.0.2 lib/mail_room/arbitration/redis.rb
mail_room-0.10.0 lib/mail_room/arbitration/redis.rb