Change soft-fail to use the config, rather than env
[rbx.git] / lib / drb / timeridconv.rb
blobbb2c48d52872a122c9d14e9890259bfb5f18fe55
1 require 'drb/drb'
2 require 'monitor'
4 module DRb
5   class TimerIdConv < DRbIdConv
6     class TimerHolder2
7       include MonitorMixin
9       class InvalidIndexError < RuntimeError; end
11       def initialize(timeout=600)
12         super()
13         @sentinel = Object.new
14         @gc = {}
15         @curr = {}
16         @renew = {}
17         @timeout = timeout
18         @keeper = keeper
19       end
21       def add(obj)
22         synchronize do 
23           key = obj.__id__
24           @curr[key] = obj
25           return key
26         end
27       end
29       def fetch(key, dv=@sentinel)
30         synchronize do 
31           obj = peek(key)
32           if obj == @sentinel
33             return dv unless dv == @sentinel
34             raise InvalidIndexError
35           end
36           @renew[key] = obj # KeepIt
37           return obj
38         end
39       end
41       def include?(key)
42         synchronize do 
43           obj = peek(key)
44           return false if obj == @sentinel
45           true
46         end
47       end
49       def peek(key)
50         synchronize do 
51           return @curr.fetch(key, @renew.fetch(key, @gc.fetch(key, @sentinel)))
52         end
53       end
55       private
56       def alternate
57         synchronize do
58           @gc = @curr       # GCed
59           @curr = @renew
60           @renew = {}
61         end
62       end
64       def keeper
65         Thread.new do
66           loop do
67             size = alternate
68             sleep(@timeout)
69           end
70         end
71       end
72     end
74     def initialize(timeout=600)
75       @holder = TimerHolder2.new(timeout)
76     end
78     def to_obj(ref)
79       return super if ref.nil?
80       @holder.fetch(ref)
81     rescue TimerHolder2::InvalidIndexError
82       raise "invalid reference"
83     end
85     def to_id(obj)
86       return @holder.add(obj)
87     end
88   end
89 end
91 # DRb.install_id_conv(TimerIdConv.new)