Module: TingYun::TingYunService::Request

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

Instance Method Summary collapse

Instance Method Details

#check_post_size(post) ⇒ Object



57
58
59
60
61
62
# File 'lib/ting_yun/ting_yun_service/request.rb', line 57

def check_post_size(post)
  size = post.size
  return if size < TingYun::Agent.config[:post_size_limit]
  TingYun::Agent.logger.debug "Tried to send too much data: #{size} bytes"
  raise TingYun::Support::Exception::UnrecoverableServerException.new('413 Request Entity Too Large')
end

#compress_request_if_needed(data) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/ting_yun/ting_yun_service/request.rb', line 47

def compress_request_if_needed(data)
  encoding = 'identity'
  if data.size > 64*1024
    data = TingYun::Support::Serialize::Encoders::Compressed.encode(data)
    encoding = 'deflate'
  end
  check_post_size(data)
  [data, encoding]
end

#send_request(opts) ⇒ Object

Posts to the specified server, retry every minute if ServerConnectionException



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ting_yun/ting_yun_service/request.rb', line 10

def send_request(opts)
  request = Net::HTTP::Post.new(opts[:uri], 'CONTENT-ENCODING' => opts[:encoding], 'HOST' => opts[:collector].name)
  request['user-agent'] = user_agent
  request.content_type = "application/octet-stream"
  request.body = opts[:data]
  response = nil
  max_attempts = 2
  attempts = 0
  begin
    attempts += 1
    conn = http_connection
    TingYun::Agent.logger.debug "Sending request to #{opts[:collector]}#{opts[:uri]}"
    TingYun::Support::TimerLib.timeout(@request_timeout) do
      response = conn.request(request)
    end
  rescue *CONNECTION_ERRORS => e
    close_shared_connection
    if attempts < max_attempts
      TingYun::Agent.logger.debug("Retrying request to #{opts[:collector]}#{opts[:uri]} after #{e}")
      retry
    else
      raise TingYun::Support::Exception::ServerConnectionException, "Recoverable error talking to #{@collector} after #{attempts} attempts: #{e}"
    end
  end
  TingYun::Agent.logger.debug "Received response, status: #{response.code}, encoding: '#{response['content-encoding']}'"

  case response
    when Net::HTTPSuccess
      true # do nothing
    else
      raise TingYun::Support::Exception::ServerConnectionException, "Unexpected response from server (#{response.code}): #{response.message}"
  end
  response
end

#user_agentObject



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

def user_agent
  ruby_description = ''
  # note the trailing space!
  ruby_description << "(ruby #{::RUBY_VERSION} #{::RUBY_PLATFORM}) " if defined?(::RUBY_VERSION) && defined?(::RUBY_PLATFORM)
  zlib_version = ''
  zlib_version << "zlib/#{Zlib.zlib_version}" if defined?(::Zlib) && Zlib.respond_to?(:zlib_version)
  "NBS Newlens Agent/#{TingYun::VERSION::STRING} #{ruby_description}#{zlib_version}"
end

#valid_to_marshal?(data) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# File 'lib/ting_yun/ting_yun_service/request.rb', line 73

def valid_to_marshal?(data)
  @marshaller.dump(data)
  true
rescue StandardError, SystemStackError => error
  TingYun::Agent.logger.warn("Unable to marshal environment report on connect.", error)
  false
end