Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace use of _id2ref in DRb. #2102

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions lib/drb/drb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

require 'socket'
require 'io/wait'
require 'weakref'
require_relative 'eq'

#
Expand Down Expand Up @@ -355,30 +356,68 @@ class DRbConnError < DRbError; end

# Class responsible for converting between an object and its id.
#
# This, the default implementation, uses an object's local ObjectSpace
# This, the default implementation, uses an object's runtime-assigned
# __id__ as its id. This means that an object's identification over
# drb remains valid only while that object instance remains alive
# within the server runtime.
#
# For alternative mechanisms, see DRb::TimerIdConv in drb/timeridconv.rb
# and DRbNameIdConv in sample/name.rb in the full drb distribution.
#
class DRbIdConv
def initialize
@id2ref = {}
@mutex = Mutex.new
end

# Convert an object reference id to an object.
#
# This implementation looks up the reference id in the local object
# space and returns the object it refers to.
def to_obj(ref)
ObjectSpace._id2ref(ref)
_get(ref)
end

# Convert an object into a reference id.
#
# This implementation returns the object's __id__ in the local
# object space.
def to_id(obj)
obj.nil? ? nil : obj.__id__
obj.nil? ? nil : _put(obj)
end

def _clean
fail unless @mutex.owned?
dead = []
@id2ref.each {|id,weakref| dead << id unless weakref.weakref_alive?}
dead.each {|id| @id2ref.delete(id)}
end

def _put(obj)
id = obj.__id__
@mutex.lock
begin
_clean
@id2ref[id] = WeakRef.new(obj)
ensure
@mutex.unlock
end
id
end

def _get(id)
@mutex.lock
begin
weakref = @id2ref[id]
return weakref.__getobj__ if weakref
rescue WeakRef::RefError
@id2ref.delete id
ensure
@mutex.unlock
end
nil
end
private :_clean, :_put, :_get
end

# Mixin module making an object undumpable or unmarshallable.
Expand Down