Class: Net::SSH::Connection::Keepalive

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/net/ssh/connection/keepalive.rb

Instance Attribute Summary

Attributes included from Loggable

#logger

Instance Method Summary collapse

Methods included from Loggable

#debug, #error, #fatal, #info, #lwarn

Constructor Details

#initialize(session) ⇒ Keepalive

Returns a new instance of Keepalive.



8
9
10
11
12
13
# File 'lib/net/ssh/connection/keepalive.rb', line 8

def initialize(session)
  @last_keepalive_sent_at = nil
  @unresponded_keepalive_count = 0
  @session = session
  self.logger = session.logger
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/net/ssh/connection/keepalive.rb', line 19

def enabled?
  options[:keepalive]
end

#intervalObject



23
24
25
# File 'lib/net/ssh/connection/keepalive.rb', line 23

def interval
  options[:keepalive_interval] || Session::DEFAULT_IO_SELECT_TIMEOUT
end

#keepalive_maxcountObject



34
35
36
# File 'lib/net/ssh/connection/keepalive.rb', line 34

def keepalive_maxcount
  (options[:keepalive_maxcount] || 3).to_i
end

#optionsObject



15
16
17
# File 'lib/net/ssh/connection/keepalive.rb', line 15

def options
  @session.options
end

#send_as_needed(was_events) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/net/ssh/connection/keepalive.rb', line 38

def send_as_needed(was_events)
  return if was_events
  return unless should_send?

  info { "sending keepalive #{@unresponded_keepalive_count}" }

  @unresponded_keepalive_count += 1
  @session.send_global_request("[email protected]") { |success, response|
    debug { "keepalive response successful. Missed #{@unresponded_keepalive_count - 1} keepalives" }
    @unresponded_keepalive_count = 0
  }
  @last_keepalive_sent_at = Time.now
  if keepalive_maxcount > 0 && @unresponded_keepalive_count > keepalive_maxcount
    error { "Timeout, server #{@session.host} not responding. Missed #{@unresponded_keepalive_count - 1} timeouts." }
    @unresponded_keepalive_count = 0
    raise Net::SSH::Timeout, "Timeout, server #{@session.host} not responding."
  end
end

#should_send?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
# File 'lib/net/ssh/connection/keepalive.rb', line 27

def should_send?
  return false unless enabled?
  return true unless @last_keepalive_sent_at

  Time.now - @last_keepalive_sent_at >= interval
end