Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TLS connect support #60

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions lib/em-hiredis/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class BaseClient

attr_reader :host, :port, :password, :db

def initialize(host = 'localhost', port = 6379, password = nil, db = nil)
@host, @port, @password, @db = host, port, password, db
def initialize(host = 'localhost', port = 6379, password = nil, db = nil, args={})
@host, @port, @password, @db, @args = host, port, password, db, args
@defs = []
@command_queue = []

Expand All @@ -41,18 +41,26 @@ def initialize(host = 'localhost', port = 6379, password = nil, db = nil)
# In usual operation, the uri should be passed to initialize. This method
# is useful for example when failing over to a slave connection at runtime
#
def configure(uri_string)
uri = URI(uri_string)
def configure(options)
if options.is_a?(String)
uri = URI(options)

if uri.scheme == "unix"
@host = uri.path
@port = nil
else
@host = uri.host
@port = uri.port
@password = uri.password
path = uri.path[1..-1]
@db = path.to_i # Empty path => 0
if uri.scheme == "unix"
@host = uri.path
@port = nil
else
@host = uri.host
@port = uri.port
@password = uri.password
path = uri.path[1..-1]
@db = path.to_i # Empty path => 0
end
elsif options.is_a?(Hash)
@host = options[:host]
@port = options[:port]
@password = options[:password]
@db = options[:db].to_i # Empty path => 0
@args = options.except(:host, :password, :port, :path, :db)
end
end

Expand All @@ -71,7 +79,7 @@ def reconnect!(new_uri = nil)

def connect
@auto_reconnect = true
@connection = EM.connect(@host, @port, Connection, @host, @port)
@connection = EM.connect(@host, @port, Connection, @host, @port, @args)

@connection.on(:closed) do
cancel_inactivity_checks
Expand Down
6 changes: 3 additions & 3 deletions lib/em-hiredis/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module EventMachine::Hiredis
class Client < BaseClient
def self.connect(host = 'localhost', port = 6379)
new(host, port).connect
def self.connect(host = 'localhost', port = 6379, args = {})
new(host, port, args).connect
end

def self.load_scripts_from(dir)
Expand Down Expand Up @@ -87,7 +87,7 @@ def info_commandstats(&blk)
#
def pubsub
@pubsub ||= begin
PubsubClient.new(@host, @port, @password, @db).connect
PubsubClient.new(@host, @port, @password, @db, @args).connect
end
end

Expand Down
12 changes: 10 additions & 2 deletions lib/em-hiredis/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ module EventMachine::Hiredis
class Connection < EM::Connection
include EventMachine::Hiredis::EventEmitter

def initialize(host, port)
def initialize(host, port, args={})
super
@host, @port = host, port
@name = "[em-hiredis #{@host}:#{@port}]"
tls = args[:ssl].present?
@name = "[em-hiredis #{@host}:#{@port} #{tls}]"
@args = args
end

def post_init
if @args[:ssl]
start_tls(@args[:ssl_params])
end
end

def reconnect(host, port)
Expand Down
3 changes: 2 additions & 1 deletion lib/em-hiredis/pubsub_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ class PubsubClient < BaseClient

PING_CHANNEL = '__em-hiredis-ping'

def initialize(host='localhost', port='6379', password=nil, db=nil)
def initialize(host='localhost', port='6379', password=nil, db=nil, args={})
@subs, @psubs = [], []
@args = args
@pubsub_defs = Hash.new { |h,k| h[k] = [] }
super
end
Expand Down