Module: TingYun::TingYunService::Request
- Included in:
- Http
- Defined in:
- lib/ting_yun/ting_yun_service/request.rb
Instance Method Summary collapse
- #check_post_size(post) ⇒ Object
- #compress_request_if_needed(data) ⇒ Object
-
#send_request(opts) ⇒ Object
Posts to the specified server, retry every minute if ServerConnectionException.
- #user_agent ⇒ Object
- #valid_to_marshal?(data) ⇒ Boolean
Instance Method Details
#check_post_size(post) ⇒ Object
66 67 68 69 70 |
# File 'lib/ting_yun/ting_yun_service/request.rb', line 66 def check_post_size(post) return if post.size < TingYun::Agent.config[:post_size_limit] TingYun::Agent.logger.debug "Tried to send too much data: #{post.size} bytes" raise TingYun::Support::Exception::UnrecoverableServerException.new('413 Request Entity Too Large') end |
#compress_request_if_needed(data) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/ting_yun/ting_yun_service/request.rb', line 56 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 44 45 46 47 48 49 50 51 52 |
# 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 when Net::HTTPServiceUnavailable raise TingYun::Support::Exception::ServerConnectionException, "Service unavailable (#{response.code}): #{response.message}" when Net::HTTPGatewayTimeOut raise TingYun::Support::Exception::ServerConnectionException, "Gateway timeout (#{response.code}): #{response.message}" when Net::HTTPRequestEntityTooLarge raise TingYun::Support::Exception::UnrecoverableServerException, '413 Request Entity Too Large' when Net::HTTPUnsupportedMediaType raise TingYun::Support::Exception::UnsupportedMediaType, '415 Unsupported Media Type' else raise TingYun::Support::Exception::ServerConnectionException, "Unexpected response from server (#{response.code}): #{response.message}" end response end |
#user_agent ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/ting_yun/ting_yun_service/request.rb', line 72 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
81 82 83 84 85 86 87 |
# File 'lib/ting_yun/ting_yun_service/request.rb', line 81 def valid_to_marshal?(data) @marshaller.dump(data) true rescue StandardError, SystemStackError => e TingYun::Agent.logger.warn("Unable to marshal environment report on connect.", e) false end |