lib/and-son/client.rb in and-son-0.3.0 vs lib/and-son/client.rb in and-son-0.3.1

- old
+ new

@@ -84,11 +84,20 @@ benchmark = Benchmark.measure do client_response = self.responses.find(name, params) if ENV['ANDSON_TEST_MODE'] client_response ||= self.call!(name, params) end - self.logger_value.info("[AndSon] #{summary_line(name, params, benchmark, client_response)}") + summary_line = SummaryLine.new({ + 'time' => RoundedTime.new(benchmark.real), + 'status' => client_response.protocol_response.code, + 'host' => "#{self.host}:#{self.port}", + 'version' => self.version, + 'service' => name, + 'params' => params + }) + self.logger_value.info("[AndSon] #{summary_line}") + if block_given? yield client_response.protocol_response else client_response.data end @@ -96,66 +105,44 @@ def call!(name, params) call_params = self.params_value.merge(params) AndSon::Connection.new(host, port).open do |connection| connection.write(Sanford::Protocol::Request.new(version, name, call_params).to_hash) + connection.close_write if !connection.peek(timeout_value).empty? AndSon::Response.parse(connection.read(timeout_value)) else raise AndSon::ConnectionClosedError.new end end end - protected - - def summary_line(name, params, benchmark, client_response) - response = client_response.protocol_response - SummaryLine.new.tap do |line| - line.add 'host', "#{self.host}:#{self.port}" - line.add 'version', self.version - line.add 'service', name - line.add 'params', params - line.add 'status', response.code - line.add 'duration', self.round_time(benchmark.real) - end - end - - ROUND_PRECISION = 2 - ROUND_MODIFIER = 10 ** ROUND_PRECISION - def round_time(time_in_seconds) - (time_in_seconds * 1000 * ROUND_MODIFIER).to_i / ROUND_MODIFIER.to_f - end - end - class SummaryLine - - def initialize - @hash = {} - end - - def add(key, value) - @hash[key] = value.inspect if value - end - - def to_s - [ 'host', 'version', 'service', 'status', 'duration', 'params' ].map do |key| - "#{key}=#{@hash[key]}" if @hash[key] - end.compact.join(" ") - end - - end - class ConnectionClosedError < RuntimeError def initialize super "The server closed the connection, no response was written." end end class NullLogger ::Logger::Severity.constants.each do |name| define_method(name.downcase){|*args| } # no-op + end + end + + module SummaryLine + def self.new(line_attrs) + attr_keys = %w{time status host version service params} + attr_keys.map{ |k| "#{k}=#{line_attrs[k].inspect}" }.join(' ') + end + end + + module RoundedTime + ROUND_PRECISION = 2 + ROUND_MODIFIER = 10 ** ROUND_PRECISION + def self.new(time_in_seconds) + (time_in_seconds * 1000 * ROUND_MODIFIER).to_i / ROUND_MODIFIER.to_f end end end