Kouhei Sutou
null+****@clear*****
Fri May 2 18:13:14 JST 2014
Kouhei Sutou 2014-05-02 18:13:14 +0900 (Fri, 02 May 2014) New Revision: 26e70940f2ecb598420281e21a7fa611c98f1137 https://github.com/droonga/droonga-client-ruby/commit/26e70940f2ecb598420281e21a7fa611c98f1137 Message: coolio: support connection failed detection Modified files: lib/droonga/client/connection/droonga-protocol/coolio.rb lib/droonga/client/connection/error.rb Modified: lib/droonga/client/connection/droonga-protocol/coolio.rb (+30 -7) =================================================================== --- lib/droonga/client/connection/droonga-protocol/coolio.rb 2014-05-02 17:00:49 +0900 (3266a3d) +++ lib/droonga/client/connection/droonga-protocol/coolio.rb 2014-05-02 18:13:14 +0900 (2e439f5) @@ -50,23 +50,36 @@ module Droonga def initialize(*args) super @connected = false + @failed_to_connect = false @buffer = [] end - def send(tag, data) + def send(tag, data, &on_error) + if @failed_to_connect + on_error.call(data) + end fluent_message = [tag, Time.now.to_i, data] packed_fluent_message = MessagePackPacker.pack(fluent_message) if @connected write(packed_fluent_message) else - @buffer << packed_fluent_message + @buffer << [packed_fluent_message, on_error] end end def on_connect @connected = true - @buffer.each do |message| - write(message) + @buffer.each do |packed_message,| + write(packed_message) + end + @buffer.clear + end + + def on_connect_failed + @failed_to_connect = true + @buffer.each do |packed_message, on_error| + _, _, message = MessagePack.unpack(packed_message) + on_error.call(message) end @buffer.clear end @@ -168,7 +181,9 @@ module Droonga id = message["id"] || generate_id message = message.merge("id" => id, "replyTo" => @receiver.droonga_name) - send(message, options) + send(message, options) do |error| + yield(error) + end sync = block.nil? if sync @@ -195,7 +210,9 @@ module Droonga message = message.merge("id" => id, "replyTo" => @receiver.droonga_name, "from" => @receiver.droonga_name) - send(message, options) + send(message, options) do |error| + yield(error) + end request = InfiniteRequest.new(@loop) sync = block.nil? @@ -228,7 +245,13 @@ module Droonga date = message["date"] || Time.now message = message.merge("id" => id, "date" => date) end - @sender.send("#{@tag}.message", message) + @sender.send("#{@tag}.message", message) do + host =****@sende*****[3] + port =****@sende*****[1] + detail = message + error = ConnectionError.new(host, port, detail) + yield(error) + end end def close Modified: lib/droonga/client/connection/error.rb (+12 -0) =================================================================== --- lib/droonga/client/connection/error.rb 2014-05-02 17:00:49 +0900 (a33ec7c) +++ lib/droonga/client/connection/error.rb 2014-05-02 18:13:14 +0900 (aa0d524) @@ -33,6 +33,18 @@ module Droonga super("Unknown #{@protocol} backend: <#{backend}>: #{detail}") end end + + class ConnectionError < Error + attr_reader :host + attr_reader :port + attr_reader :detail + def initialize(host, port, detail) + @host = host + @port = port + @detail = detail + super("Failed to connect: <#{@host}:#{@port}>: #{@detail}") + end + end end end end -------------- next part -------------- HTML����������������������������... Download