lib/em-synchrony/em-remcached.rb in em-synchrony-0.3.0.beta.1 vs lib/em-synchrony/em-remcached.rb in em-synchrony-1.0.0
- old
+ new
@@ -9,18 +9,22 @@
def connect(servers)
Memcached.servers = servers
f = Fiber.current
+ @w = EM::Timer.new(10.0) { f.resume :error }
@t = EM::PeriodicTimer.new(0.01) do
if Memcached.usable?
+ @w.cancel
@t.cancel
f.resume(self)
end
end
- Fiber.yield
+ r = Fiber.yield
+
+ (r == :error) ? (raise Exception.new('Cannot connect to memcached server')) : r
end
%w[add get set delete].each do |type|
class_eval %[
def a#{type}(contents, &callback)
@@ -28,44 +32,64 @@
df.callback &callback
cb = Proc.new { |res| df.succeed(res) }
operation Request::#{type.capitalize}, contents, &cb
- df
+ df
end
def #{type}(contents, &callback)
fiber = Fiber.current
+ paused = false
- df = a#{type}(contents, &Proc.new { |res| fiber.resume(res) })
+ cb = Proc.new do |res|
+ if paused
+ fiber.resume(res)
+ else
+ return res
+ end
+ end
+
+ df = a#{type}(contents, &cb)
df.callback &callback
+ paused = true
Fiber.yield
end
]
end
%w[add get set delete].each do |type|
- class_eval %[
- def amulti_#{type}(contents, &callback)
- df = EventMachine::DefaultDeferrable.new
- df.callback &callback
+ class_eval %[
+ def amulti_#{type}(contents, &callback)
+ df = EventMachine::DefaultDeferrable.new
+ df.callback &callback
- cb = Proc.new { |res| df.succeed(res) }
- multi_operation Request::#{type.capitalize}, contents, &cb
+ cb = Proc.new { |res| df.succeed(res) }
+ multi_operation Request::#{type.capitalize}, contents, &cb
- df
- end
+ df
+ end
- def multi_#{type}(contents, &callback)
- fiber = Fiber.current
+ def multi_#{type}(contents, &callback)
+ fiber = Fiber.current
+ paused = false
- df = amulti_#{type}(contents, &Proc.new { |res| fiber.resume(res) })
- df.callback &callback
+ cb = Proc.new do |res|
+ if paused
+ fiber.resume(res)
+ else
+ return res
+ end
+ end
- Fiber.yield
- end
- ]
- end
+ df = amulti_#{type}(contents, &cb)
+ df.callback &callback
+
+ paused = true
+ Fiber.yield
+ end
+ ]
+ end
end
end