Permalink
Browse files

properly determine if we're wrapped in a fiber or not. and if we're not

wrapped inside a fiber nor a thread, then we can't use em-http-request,
but rest-client with threads instead
  • Loading branch information...
1 parent 0edd96d commit 9d02de25410998fe1659122fb89092ae171b413a @godfat godfat committed Sep 8, 2012
Showing with 9 additions and 2 deletions.
  1. +2 −0 lib/rest-core.rb
  2. +4 −1 lib/rest-core/engine/auto.rb
  3. +3 −1 lib/rest-core/engine/future/future.rb
View
@@ -18,6 +18,8 @@ module RestCore
TIMER = 'async.timer'
FUTURE = 'async.future'
+ RootFiber = Fiber.respond_to?(:current) && Fiber.current
+
# core utilities
autoload :Builder , 'rest-core/builder'
autoload :Client , 'rest-core/client'
@@ -11,7 +11,10 @@ def call env, &k
def http_client
if Object.const_defined?(:EventMachine) &&
::EventMachine.const_defined?(:HttpRequest) &&
- ::EventMachine.reactor_running?
+ ::EventMachine.reactor_running? &&
+ # it should be either wrapped around a thread or a fiber
+ ((Thread.main != Thread.current) ||
+ (Fiber.respond_to?(:current) && RootFiber != Fiber.current))
@emhttprequest ||= RestCore::EmHttpRequest.new
@@ -15,7 +15,9 @@ def method_missing msg, *args, &block
end
def self.create *args, &block
- if Thread.current == Thread.main
+ if Fiber.respond_to?(:current) && RootFiber != Fiber.current &&
+ # because under a thread, Fiber.current won't return the root fiber
+ Thread.main == Thread.current
FutureFiber .new(*args, &block)
else
FutureThread.new(*args, &block)

0 comments on commit 9d02de2

Please sign in to comment.