Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding a mechanism to manually decide when fibers are ready to be use…

…d and extracting send_query_using_fibers
  • Loading branch information...
commit 14d0fd3b843815f13323149f3e4396f6ce66d083 1 parent 7cba707
Ruben Nine authored
View
1  lib/active_record/connection_adapters/em_postgresql_adapter.rb
@@ -21,6 +21,7 @@ def connect
PostgreSQLColumn.money_precision = (postgresql_version >= 80300) ? 19 : 10
configure_connection
+ @connection.use_fibers!
end
# Close then reopen the connection.
View
49 lib/em-postgresql-adapter/fibered_postgresql_connection.rb
@@ -24,24 +24,43 @@ def notify_readable
end
end
end
+
+ attr_accessor :use_fibers # decide whether fibers should be used
+
+ def initialize(*args)
+ self.use_fibers = false # default use_fibers to false
+ super(*args)
+ end
+
+ def use_fibers! # convenience method
+ self.use_fibers = true
+ end
+
+ def can_use_fibers?
+ ::EM.reactor_running? && self.use_fibers
+ end
+
+ def send_query_using_fibers(sql, *opts)
+ send_query(sql, *opts)
+ deferrable = ::EM::DefaultDeferrable.new
+ ::EM.watch(self.socket, Watcher, self, deferrable).notify_readable = true
+ fiber = Fiber.current
+ deferrable.callback do |result|
+ fiber.resume(result)
+ end
+ deferrable.errback do |err|
+ fiber.resume(err)
+ end
+ Fiber.yield.tap do |result|
+ raise result if result.is_a?(Exception)
+ end
+ end
def async_exec(sql, *opts)
- if ::EM.reactor_running?
- send_query(sql, *opts)
- deferrable = ::EM::DefaultDeferrable.new
- ::EM.watch(self.socket, Watcher, self, deferrable).notify_readable = true
- fiber = Fiber.current
- deferrable.callback do |result|
- fiber.resume(result)
- end
- deferrable.errback do |err|
- fiber.resume(err)
- end
- Fiber.yield.tap do |result|
- raise result if result.is_a?(Exception)
- end
+ if can_use_fibers?
+ send_query_using_fibers(sql, *opts)
else
- super(sql)
+ super(sql, *opts)
end
end
alias_method :async_query, :async_exec
Please sign in to comment.
Something went wrong with that request. Please try again.