Class: Gitlab::TcpChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/tcp_checker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remote_host, remote_port, local_host = nil, local_port = nil) ⇒ TcpChecker

Returns a new instance of TcpChecker.


7
8
9
10
11
12
# File 'lib/gitlab/tcp_checker.rb', line 7

def initialize(remote_host, remote_port, local_host = nil, local_port = nil)
  @remote_host = remote_host
  @remote_port = remote_port
  @local_host = local_host
  @local_port = local_port
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.


5
6
7
# File 'lib/gitlab/tcp_checker.rb', line 5

def error
  @error
end

#local_hostObject (readonly)

Returns the value of attribute local_host.


5
6
7
# File 'lib/gitlab/tcp_checker.rb', line 5

def local_host
  @local_host
end

#local_portObject (readonly)

Returns the value of attribute local_port.


5
6
7
# File 'lib/gitlab/tcp_checker.rb', line 5

def local_port
  @local_port
end

#remote_hostObject (readonly)

Returns the value of attribute remote_host.


5
6
7
# File 'lib/gitlab/tcp_checker.rb', line 5

def remote_host
  @remote_host
end

#remote_portObject (readonly)

Returns the value of attribute remote_port.


5
6
7
# File 'lib/gitlab/tcp_checker.rb', line 5

def remote_port
  @remote_port
end

Instance Method Details

#check(timeout: 10) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gitlab/tcp_checker.rb', line 22

def check(timeout: 10)
  Socket.tcp(
    remote_host, remote_port,
    local_host, local_port,
    connect_timeout: timeout
  ) do |sock|
    @local_port, @local_host = Socket.unpack_sockaddr_in(sock.local_address)
    @remote_port, @remote_host = Socket.unpack_sockaddr_in(sock.remote_address)
  end

  true
rescue StandardError => err
  @error = err

  false
end

#localObject


14
15
16
# File 'lib/gitlab/tcp_checker.rb', line 14

def local
  join_host_port(local_host, local_port)
end

#remoteObject


18
19
20
# File 'lib/gitlab/tcp_checker.rb', line 18

def remote
  join_host_port(remote_host, remote_port)
end