# frozen_string_literal: false
# 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.
# DRb.install_id_conv TimerIdConv.new 60 # one minute
class TimerIdConv < DRbIdConv
class TimerHolder2 # :nodoc:
class InvalidIndexError < RuntimeError; end
def initialize(keeping=600)
raise InvalidIndexError if obj == @sentinel
@renew[key] = obj # KeepIt
return @renew.fetch(key) { @gc.fetch(key, @sentinel) }
@expires = Time.now + @keeping
return unless Thread.main.alive?
Thread.new { rotate } if @expires < Time.now
ObjectSpace.define_finalizer(Object.new) {on_gc}
@expires = @gc.empty? ? nil : Time.now + @keeping
# Creates a new TimerIdConv which will hold objects for +keeping+ seconds.
def initialize(keeping=600)
@holder = TimerHolder2.new(keeping)
def to_obj(ref) # :nodoc:
rescue TimerHolder2::InvalidIndexError
raise "invalid reference"
# DRb.install_id_conv(TimerIdConv.new)