lib/dcell/rpc.rb in dcell-0.9.0 vs lib/dcell/rpc.rb in dcell-0.10.0

- old
+ new

@@ -1,10 +1,8 @@ require 'weakref' module DCell - EPOCH = Time.gm(2012) # All things begin in 2012 - class RPC < Celluloid::SyncCall def initialize(id, caller, method, arguments, block) @id, @caller, @method, @arguments, @block = id, caller, method, arguments, block end @@ -15,52 +13,54 @@ end # Loader for custom marshal format def self._load(string) id = string.slice!(0, string.index(":") + 1) - match = id.match(/^([0-9a-fA-F]+)@(.+?):$/) + match = id.match(/^([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})@(.+?):$/) raise ArgumentError, "couldn't parse call ID" unless match - call_id, node_id = match[1], match[2] + uuid, node_id = match[1], match[2] if DCell.id == node_id - Manager.claim Integer("0x#{call_id}") + Manager.claim uuid else caller, method, arguments, block = Marshal.load(string) - RPC.new("#{call_id}@#{node_id}", caller, method, arguments, block) + RPC.new("#{uuid}@#{node_id}", caller, method, arguments, block) end end # Tracks calls-in-flight class Manager @mutex = Mutex.new @ids = {} @calls = {} - @serial = Integer(Time.now - EPOCH) * 0x100000 def self.register(call) @mutex.lock - call_id = @ids[call.object_id] - unless call_id - call_id = @serial - @serial += 1 - @ids[call.object_id] = call_id - end + begin + call_id = @ids[call.object_id] + unless call_id + call_id = Celluloid.uuid + @ids[call.object_id] = call_id + end - @calls[call_id] = WeakRef.new(call) - call_id - ensure - @mutex.unlock + @calls[call_id] = WeakRef.new(call) + call_id + ensure + @mutex.unlock rescue nil + end end def self.claim(call_id) @mutex.lock - ref = @calls.delete(call_id) - ref.__getobj__ if ref - rescue WeakRef::RefError - # Nothing to see here, folks - ensure - @mutex.unlock + begin + ref = @calls.delete(call_id) + ref.__getobj__ if ref + rescue WeakRef::RefError + # Nothing to see here, folks + ensure + @mutex.unlock rescue nil + end end end end end