class Routemaster::RedisBroker

Constants

DEFAULT_NAMESPACE

Public Class Methods

new() click to toggle source
# File lib/routemaster/redis_broker.rb, line 12
def initialize
  @_connections = {}
  _cleanup
end

Public Instance Methods

cleanup() click to toggle source
# File lib/routemaster/redis_broker.rb, line 26
def cleanup
  _cleanup
end
get(name, urls: []) click to toggle source
# File lib/routemaster/redis_broker.rb, line 17
def get(name, urls: [])
  _check_for_fork
  @_connections[name] ||= begin
                            parsed_url = URI.parse(urls.first)
                            namespace = parsed_url.path.split('/')[2] || DEFAULT_NAMESPACE
                            Redis::Namespace.new(namespace, redis: Redis::Distributed.new(urls))
                          end
end
inject(clients={}) click to toggle source

Allow to inject pre-built Redis clients

Before storing a new connection, ensures that any previously set client is properly closed.

# File lib/routemaster/redis_broker.rb, line 35
def inject(clients={})
  @_injected_clients = true
  clients.each_pair do |name, client|
    _close_if_present(@_connections[name])
    @_connections[name] = Redis::Namespace.new(DEFAULT_NAMESPACE, redis: client)
  end
end

Private Instance Methods

_check_for_fork() click to toggle source

Do not clean up if the clients are injected by the host application. In that case connections should be managed the server or worker processes.

# File lib/routemaster/redis_broker.rb, line 48
def _check_for_fork
  return if @_injected_clients
  return if Process.pid == @_pid
  _cleanup
end
_cleanup() click to toggle source
# File lib/routemaster/redis_broker.rb, line 54
def _cleanup
  @_pid = Process.pid
  @_connections.each_value { |conn| _close_if_present(conn) }
  @_connections = {}
end
_close_if_present(connection) click to toggle source
# File lib/routemaster/redis_broker.rb, line 60
def _close_if_present(connection)
  if connection.respond_to?(:redis)
    connection.redis.quit
  end
end