Class: RandomPort::Pool
- Inherits:
-
Object
- Object
- RandomPort::Pool
- Defined in:
- lib/random-port/pool.rb
Overview
Pool of TPC ports.
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2018 Yegor Bugayenko
- License
-
MIT
Instance Method Summary collapse
- #acquire ⇒ Object
-
#initialize ⇒ Pool
constructor
A new instance of Pool.
- #release(port) ⇒ Object
Constructor Details
#initialize ⇒ Pool
Returns a new instance of Pool.
33 34 35 36 |
# File 'lib/random-port/pool.rb', line 33 def initialize @ports = [] @mutex = Mutex.new end |
Instance Method Details
#acquire ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/random-port/pool.rb', line 38 def acquire @mutex.synchronize do loop do server = TCPServer.new('127.0.0.1', 0) port = server.addr[1] server.close next if @ports.include?(port) @ports << port return port unless block_given? yield port @ports.delete(port) break end end end |
#release(port) ⇒ Object
54 55 56 57 58 |
# File 'lib/random-port/pool.rb', line 54 def release(port) @mutex.synchronize do @ports.delete(port) end end |