lib/fulmar/infrastructure/service/tunnel_service.rb in fulmar-1.0.0 vs lib/fulmar/infrastructure/service/tunnel_service.rb in fulmar-1.1.0
- old
+ new
@@ -1,10 +1,11 @@
require 'socket'
module Fulmar
module Infrastructure
module Service
+ # Opens an ssh tunnel to a remote host so other services can access mysql for example
class TunnelService
attr_reader :host, :remote_port, :local_port
def initialize(host, port, remote_host = 'localhost')
@host = host
@@ -29,22 +30,20 @@
def open?
@tunnel_pid > 0
end
def free_port
- port = 60000
- begin
- 1000.times do
+ (60_000..61_000).each do |port|
+ begin
socket = TCPSocket.new('localhost', port)
socket.close
- port += 1
+ rescue Errno::ECONNREFUSED
+ return port
end
- rescue Errno::ECONNREFUSED
- return port
end
+
fail 'Cannot find an open local port'
- 0
end
end
end
end
-end
\ No newline at end of file
+end