Module: TingYun::TingYunService::Connection

Included in:
Http
Defined in:
lib/ting_yun/ting_yun_service/connection.rb

Instance Method Summary collapse

Instance Method Details

#close_shared_connectionObject



65
66
67
68
69
70
71
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 65

def close_shared_connection
  if @shared_tcp_connection
    TingYun::Agent.logger.debug("Closing shared TCP connection to #{@shared_tcp_connection.address}:#{@shared_tcp_connection.port}")
    @shared_tcp_connection.finish if @shared_tcp_connection.started?
    @shared_tcp_connection = nil
  end
end

#create_and_start_http_connectionObject



30
31
32
33
34
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 30

def create_and_start_http_connection
  conn = create_http_connection
  start_connection(conn)
  conn
end

#create_http_connectionObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 43

def create_http_connection
  if TingYun::Agent.config[:proxy_host]
    TingYun::Agent.logger.debug("Using proxy server #{TingYun::Agent.config[:proxy_host]}:#{TingYun::Agent.config[:proxy_port]}")

    proxy = Net::HTTP::Proxy(
        TingYun::Agent.config[:proxy_host],
        TingYun::Agent.config[:proxy_port],
        TingYun::Agent.config[:proxy_user],
        TingYun::Agent.config[:proxy_pass]
    )
    conn = proxy.new(@collector.name, @collector.port)
  else
    conn = Net::HTTP.new(@collector.name, @collector.port)
  end

  setup_connection_for_ssl(conn) if TingYun::Agent.config[:ssl]
  setup_connection_timeouts(conn)
  TingYun::Agent.logger.debug("Created net/http handle to #{conn.address}:#{conn.port}")

  conn
end

#establish_shared_connectionObject



22
23
24
25
26
27
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 22

def establish_shared_connection
  unless @shared_tcp_connection
    @shared_tcp_connection = create_and_start_http_connection
  end
  @shared_tcp_connection
end

#http_connectionObject



14
15
16
17
18
19
20
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 14

def http_connection
  if @in_session
    establish_shared_connection
  else
    create_http_connection
  end
end

#session(&block) ⇒ Object

One session with the service’s endpoint. In this case the session represents 1 tcp connection which may transmit multiple HTTP requests via keep-alive.

Raises:

  • (ArgumentError)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 86

def session(&block)
  raise ArgumentError, "#{self.class}#shared_connection must be passed a block" unless block_given?

  begin
    t0 = Time.now
    @in_session = true
    if TingYun::Agent.config[:aggressive_keepalive]
      session_with_keepalive(&block)
    else
      session_without_keepalive(&block)
    end
  rescue *CONNECTION_ERRORS => e
    elapsed = Time.now - t0
    raise TingYun::Support::Exception::ServerConnectionException, "Recoverable error connecting to #{@collector} after #{elapsed} seconds: #{e}"
  ensure
    @in_session = false
  end
end

#session_with_keepalive(&block) ⇒ Object



105
106
107
108
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 105

def session_with_keepalive(&block)
  establish_shared_connection
  block.call
end

#session_without_keepalive(&block) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 110

def session_without_keepalive(&block)
  begin
    establish_shared_connection
    block.call
  ensure
    close_shared_connection
  end
end

#setup_connection_timeouts(conn) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 74

def setup_connection_timeouts(conn)
  # We use Timeout explicitly instead of this
  conn.read_timeout = nil

  if conn.respond_to?(:keep_alive_timeout) && TingYun::Agent.config[:aggressive_keepalive]
    conn.keep_alive_timeout = TingYun::Agent.config[:keep_alive_timeout]
  end
end

#start_connection(conn) ⇒ Object



36
37
38
39
40
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 36

def start_connection(conn)
  TingYun::Agent.logger.debug("Opening TCP connection to #{conn.address}:#{conn.port}")
  TingYun::Support::TimerLib.timeout(@request_timeout) { conn.start }
  conn
end