Class: Rumai::IXP::Agent::RangedPool

Inherits:
Object
  • Object
show all
Defined in:
lib/rumai/ixp/transport.rb

Overview

A finite, thread-safe pool of range members.

Constant Summary

FILL_RATE =

how many new members should be added to the pool when the pool is empty?

10

Instance Method Summary

Constructor Details

- (RangedPool) initialize(range)

A new instance of RangedPool



61
62
63
64
65
66
67
# File 'lib/rumai/ixp/transport.rb', line 61

def initialize range
  @pos = range.first
  @lim = range.last
  @lim = @lim.succ unless range.exclude_end?

  @pool = Queue.new
end

Instance Method Details

- (Object) obtain

Returns an unoccupied range member from the pool.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rumai/ixp/transport.rb', line 72

def obtain
  begin
    @pool.deq true

  rescue ThreadError
    # pool is empty, so fill it
    FILL_RATE.times do
      if @pos != @lim
        @pool << @pos
        @pos = @pos.succ
      else
        # range is exhausted, so give other threads
        # a chance to fill the pool before retrying
        Thread.pass
        break
      end
    end

    retry
  end
end

- (Object) release(member)

Marks the given member as being unoccupied so that it may be occupied again in the future.



98
99
100
# File 'lib/rumai/ixp/transport.rb', line 98

def release member
  @pool << member
end