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
Constant Summary collapse
- SINGLETON =
Application wide pool of ports
Pool.new
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
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/random-port/pool.rb', line 41 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
57 58 59 60 61 |
# File 'lib/random-port/pool.rb', line 57 def release(port) @mutex.synchronize do @ports.delete(port) end end |