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



62
63
64
65
66
67
68
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 62

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



27
28
29
30
31
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 27

def create_and_start_http_connection
  conn = create_http_connection
  start_connection(conn)
  conn
end

#create_http_connectionObject



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

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



19
20
21
22
23
24
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 19

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

#http_connectionObject

Return a Net::HTTP connection object to make a call to the collector. We’ll reuse the same handle for cases where we’re using keep-alive, or otherwise create a new one.



11
12
13
14
15
16
17
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 11

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)


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

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



102
103
104
105
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 102

def session_with_keepalive(&block)
  establish_shared_connection
  block.call
end

#session_without_keepalive(&block) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 107

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

#setup_connection_timeouts(conn) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 71

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



33
34
35
36
37
# File 'lib/ting_yun/ting_yun_service/connection.rb', line 33

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