123456789_123456789_123456789_123456789_123456789_

Class: DRb::TimerIdConv::TimerHolder2

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Exceptions:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, MonitorMixin
Inherits: Object
Defined in: lib/drb/timeridconv.rb

Class Method Summary

Instance Method Summary

Constructor Details

.new(keeping = 600) ⇒ TimerHolder2

[ GitHub ]

  
# File 'lib/drb/timeridconv.rb', line 21

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

Instance Method Details

#add(obj)

[ GitHub ]

  
# File 'lib/drb/timeridconv.rb', line 30

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

#fetch(key)

[ GitHub ]

  
# File 'lib/drb/timeridconv.rb', line 40

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

#invoke_keeper (private)

[ GitHub ]

  
# File 'lib/drb/timeridconv.rb', line 55

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

#on_gc (private)

[ GitHub ]

  
# File 'lib/drb/timeridconv.rb', line 61

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

#peek(key) (private)

[ GitHub ]

  
# File 'lib/drb/timeridconv.rb', line 51

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

#rotate (private)

[ GitHub ]

  
# File 'lib/drb/timeridconv.rb', line 68

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