2003-06-18 19:45:12 +04:00
|
|
|
require 'drb/drb'
|
|
|
|
require 'monitor'
|
|
|
|
|
|
|
|
module DRb
|
2013-01-25 07:25:39 +04:00
|
|
|
|
|
|
|
# Timer id conversion keeps objects alive for a certain amount of time after
|
|
|
|
# their last access. The default time period is 600 seconds and can be
|
|
|
|
# changed upon initialization.
|
|
|
|
#
|
|
|
|
# To use TimerIdConv:
|
|
|
|
#
|
|
|
|
# DRb.install_id_conv TimerIdConv.new 60 # one minute
|
|
|
|
|
2003-06-18 19:45:12 +04:00
|
|
|
class TimerIdConv < DRbIdConv
|
2013-01-25 07:25:39 +04:00
|
|
|
class TimerHolder2 # :nodoc:
|
2003-06-18 19:45:12 +04:00
|
|
|
include MonitorMixin
|
|
|
|
|
|
|
|
class InvalidIndexError < RuntimeError; end
|
|
|
|
|
|
|
|
def initialize(timeout=600)
|
2011-05-19 01:19:18 +04:00
|
|
|
super()
|
|
|
|
@sentinel = Object.new
|
|
|
|
@gc = {}
|
|
|
|
@curr = {}
|
|
|
|
@renew = {}
|
|
|
|
@timeout = timeout
|
|
|
|
@keeper = keeper
|
2003-06-18 19:45:12 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def add(obj)
|
2011-05-19 01:19:18 +04:00
|
|
|
synchronize do
|
|
|
|
key = obj.__id__
|
|
|
|
@curr[key] = obj
|
|
|
|
return key
|
|
|
|
end
|
2003-06-18 19:45:12 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch(key, dv=@sentinel)
|
2011-05-19 01:19:18 +04:00
|
|
|
synchronize do
|
|
|
|
obj = peek(key)
|
|
|
|
if obj == @sentinel
|
|
|
|
return dv unless dv == @sentinel
|
|
|
|
raise InvalidIndexError
|
|
|
|
end
|
|
|
|
@renew[key] = obj # KeepIt
|
|
|
|
return obj
|
|
|
|
end
|
2003-06-18 19:45:12 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def include?(key)
|
2011-05-19 01:19:18 +04:00
|
|
|
synchronize do
|
|
|
|
obj = peek(key)
|
|
|
|
return false if obj == @sentinel
|
|
|
|
true
|
|
|
|
end
|
2003-06-18 19:45:12 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def peek(key)
|
2011-05-19 01:19:18 +04:00
|
|
|
synchronize do
|
|
|
|
return @curr.fetch(key, @renew.fetch(key, @gc.fetch(key, @sentinel)))
|
|
|
|
end
|
2003-06-18 19:45:12 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def alternate
|
2011-05-19 01:19:18 +04:00
|
|
|
synchronize do
|
|
|
|
@gc = @curr # GCed
|
|
|
|
@curr = @renew
|
|
|
|
@renew = {}
|
|
|
|
end
|
2003-06-18 19:45:12 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def keeper
|
2011-05-19 01:19:18 +04:00
|
|
|
Thread.new do
|
|
|
|
loop do
|
|
|
|
alternate
|
|
|
|
sleep(@timeout)
|
|
|
|
end
|
|
|
|
end
|
2003-06-18 19:45:12 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-25 07:25:39 +04:00
|
|
|
# Creates a new TimerIdConv which will hold objects for +timeout+ seconds.
|
2003-06-18 19:45:12 +04:00
|
|
|
def initialize(timeout=600)
|
|
|
|
@holder = TimerHolder2.new(timeout)
|
|
|
|
end
|
|
|
|
|
2013-01-25 07:25:39 +04:00
|
|
|
def to_obj(ref) # :nodoc:
|
2003-06-18 19:45:12 +04:00
|
|
|
return super if ref.nil?
|
|
|
|
@holder.fetch(ref)
|
|
|
|
rescue TimerHolder2::InvalidIndexError
|
|
|
|
raise "invalid reference"
|
|
|
|
end
|
|
|
|
|
2013-01-25 07:25:39 +04:00
|
|
|
def to_id(obj) # :nodoc:
|
2003-06-18 19:45:12 +04:00
|
|
|
return @holder.add(obj)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# DRb.install_id_conv(TimerIdConv.new)
|