class MailRoom::Arbitration::Redis

Constants

EXPIRATION

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

Options

Attributes

options[RW]

Public Class Methods

new(options) click to toggle source
# File lib/mail_room/arbitration/redis.rb, line 21
def initialize(options)
  @options = options
end

Public Instance Methods

deliver?(uid) click to toggle source
# File lib/mail_room/arbitration/redis.rb, line 25
def deliver?(uid)
  key = "delivered:#{uid}"

  incr = nil
  client.multi do |c|
    # At this point, `incr` is a future, which will get its value after
    # the MULTI command returns.
    incr = c.incr(key)

    c.expire(key, EXPIRATION)
  end

  # If INCR returns 1, that means the key didn't exist before, which means
  # we are the first mail_room to try to deliver this message, so we get to.
  # If we get any other value, another mail_room already (tried to) deliver
  # the message, so we don't have to anymore.
  incr.value == 1
end

Private Instance Methods

client() click to toggle source
# File lib/mail_room/arbitration/redis.rb, line 46
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