Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

allow custom db/auth for em-hiredis on connect, closes #103

  • Loading branch information...
commit 4755c48e4e34286326bc64054c349bb150915bfb 1 parent a30e8e9
@igrigorik authored
View
82 lib/em-synchrony/em-hiredis.rb
@@ -6,14 +6,94 @@
module EventMachine
module Hiredis
- class Client
+ def self.connect(uri = nil)
+ client = setup(uri)
+ EM::Synchrony.sync client.connect
+ client
+ end
+
+ class Client
def self.connect(host = 'localhost', port = 6379)
conn = new(host, port)
EM::Synchrony.sync conn.connect
conn
end
+ def connect
+ @connection = EM.connect(@host, @port, Connection, @host, @port)
+
+ @connection.on(:closed) do
+ if @connected
+ @defs.each { |d| d.fail("Redis disconnected") }
+ @defs = []
+ @deferred_status = nil
+ @connected = false
+ unless @closing_connection
+ @reconnecting = true
+ reconnect
+ end
+ else
+ unless @closing_connection
+ EM.add_timer(1) { reconnect }
+ end
+ end
+ end
+
+ @connection.on(:connected) do
+ Fiber.new do
+ @connected = true
+
+ auth(@password) if @password
+ select(@db) if @db
+
+ @subs.each { |s| method_missing(:subscribe, s) }
+ @psubs.each { |s| method_missing(:psubscribe, s) }
+ succeed
+
+ if @reconnecting
+ @reconnecting = false
+ emit(:reconnected)
+ end
+ end.resume
+ end
+
+ @connection.on(:message) do |reply|
+ if RuntimeError === reply
+ raise "Replies out of sync: #{reply.inspect}" if @defs.empty?
+ deferred = @defs.shift
+ deferred.fail(reply) if deferred
+ else
+ if reply && PUBSUB_MESSAGES.include?(reply[0]) # reply can be nil
+ kind, subscription, d1, d2 = *reply
+
+ case kind.to_sym
+ when :message
+ emit(:message, subscription, d1)
+ when :pmessage
+ emit(:pmessage, subscription, d1, d2)
+ end
+ else
+ if @defs.empty?
+ if @monitoring
+ emit(:monitor, reply)
+ else
+ raise "Replies out of sync: #{reply.inspect}"
+ end
+ else
+ deferred = @defs.shift
+ deferred.succeed(reply) if deferred
+ end
+ end
+ end
+ end
+
+ @connected = false
+ @reconnecting = false
+
+ return self
+ end
+
alias :old_method_missing :method_missing
def method_missing(sym, *args)
EM::Synchrony.sync old_method_missing(sym, *args)
View
1  spec/activerecord_spec.rb
@@ -16,7 +16,6 @@ class Widget < ActiveRecord::Base; end;
DELAY = 0.25
QUERY = "SELECT sleep(#{DELAY})"
-
def establish_connection
ActiveRecord::Base.establish_connection(
:adapter => 'em_mysql2',
View
23 spec/hiredis_spec.rb
@@ -11,6 +11,29 @@
end
end
+ it "should work with compact connect syntax" do
+ EventMachine.synchrony do
+ redis = EM::Hiredis.connect
+
+ redis.set('a', 'bar')
+ redis.get('a').should == 'bar'
+
+ EM.stop
+ end
+ end
+
+ it "should work with manual db select" do
+ EventMachine.synchrony do
+ redis = EM::Hiredis.connect 'redis://127.0.0.1:6379'
+ redis.select(0)
+
+ redis.set('a', 'baz')
+ redis.get('a').should == 'baz'
+
+ EM.stop
+ end
+ end
+
it "should get/set records synchronously" do
EventMachine.synchrony do
redis = EM::Hiredis::Client.connect

0 comments on commit 4755c48

Please sign in to comment.
Something went wrong with that request. Please try again.