class DRb::TimerIdConv::TimerHolder2

:nodoc:

def add(obj)

def add(obj)
  synchronize do
    rotate
    key = obj.__id__
    @renew[key] = obj
    invoke_keeper
    return key
  end
end

def fetch(key)

def fetch(key)
  synchronize do
    rotate
    obj = peek(key)
    raise InvalidIndexError if obj == @sentinel
    @renew[key] = obj # KeepIt
    return obj
  end
end

def initialize(keeping=600)

def initialize(keeping=600)
  super()
  @sentinel = Object.new
  @gc = {}
  @renew = {}
  @keeping = keeping
  @expires = nil
end

def invoke_keeper

def invoke_keeper
  return if @expires
  @expires = Time.now + @keeping
  on_gc
end

def on_gc

def on_gc
  return unless Thread.main.alive?
  return if @expires.nil?
  Thread.new { rotate } if @expires < Time.now
  ObjectSpace.define_finalizer(Object.new) {on_gc}
end

def peek(key)

def peek(key)
  return @renew.fetch(key) { @gc.fetch(key, @sentinel) }
end

def rotate

def rotate
  synchronize do
    if @expires &.< Time.now
      @gc = @renew      # GCed
      @renew = {}
      @expires = @gc.empty? ? nil : Time.now + @keeping
    end
  end
end