Kouhei Sutou
null+****@clear*****
Tue Sep 30 19:38:05 JST 2014
Kouhei Sutou 2014-09-30 19:38:05 +0900 (Tue, 30 Sep 2014) New Revision: 80ea11c3a6a0aeea57a43f23c91287255bbb60ed https://github.com/ranguba/groonga-client/commit/80ea11c3a6a0aeea57a43f23c91287255bbb60ed Message: http: add synchronous backend "thread" backend is implemented by synchronous backend. Copied files: lib/groonga/client/protocol/http/synchronous.rb (from lib/groonga/client/protocol/http/thread.rb) Modified files: lib/groonga/client/protocol/http/thread.rb Copied: lib/groonga/client/protocol/http/synchronous.rb (+15 -27) 77% =================================================================== --- lib/groonga/client/protocol/http/thread.rb 2014-09-30 19:29:02 +0900 (4a0a22c) +++ lib/groonga/client/protocol/http/synchronous.rb 2014-09-30 19:38:05 +0900 (95d6e22) @@ -24,17 +24,7 @@ module Groonga class Client module Protocol class HTTP - class Thread - class Request - def initialize(thread) - @thread = thread - end - - def wait - @thread.join - end - end - + class Synchronous def initialize(host, port, options) @host = host @port = port @@ -42,28 +32,26 @@ module Groonga end def send(command) - thread = ::Thread.new do - begin - Net::HTTP.start(@host, @port) do |http| - response = send_request(http, command) - case response - when Net::HTTPSuccess, Net::HTTPBadRequest + begin + Net::HTTP.start(@host, @port) do |http| + response = send_request(http, command) + case response + when Net::HTTPSuccess, Net::HTTPBadRequest + yield(response.body) + else + if response.body.start_with?("[[") yield(response.body) else - if response.body.start_with?("[[") - yield(response.body) - else - message = - "#{response.code} #{response.message}: #{response.body}" - raise Error.new(message) - end + message = + "#{response.code} #{response.message}: #{response.body}" + raise Error.new(message) end end - rescue SystemCallError, Timeout::Error - raise WrappedError.new($!) end + rescue SystemCallError, Timeout::Error + raise WrappedError.new($!) end - Request.new(thread) + EmptyRequest.new end # @return [false] Always returns false because the current Modified: lib/groonga/client/protocol/http/thread.rb (+4 -78) =================================================================== --- lib/groonga/client/protocol/http/thread.rb 2014-09-30 19:29:02 +0900 (4a0a22c) +++ lib/groonga/client/protocol/http/thread.rb 2014-09-30 19:38:05 +0900 (e915d80) @@ -15,16 +15,15 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -require "net/http" +require "thread" -require "groonga/client/empty-request" -require "groonga/client/protocol/error" +require "groonga/client/protocol/http/synchronous" module Groonga class Client module Protocol class HTTP - class Thread + class Thread < Synchronous class Request def initialize(thread) @thread = thread @@ -35,85 +34,12 @@ module Groonga end end - def initialize(host, port, options) - @host = host - @port = port - @options = options - end - def send(command) thread = ::Thread.new do - begin - Net::HTTP.start(@host, @port) do |http| - response = send_request(http, command) - case response - when Net::HTTPSuccess, Net::HTTPBadRequest - yield(response.body) - else - if response.body.start_with?("[[") - yield(response.body) - else - message = - "#{response.code} #{response.message}: #{response.body}" - raise Error.new(message) - end - end - end - rescue SystemCallError, Timeout::Error - raise WrappedError.new($!) - end + super end Request.new(thread) end - - # @return [false] Always returns false because the current - # implementation doesn't support keep-alive. - def connected? - false - end - - # Does nothing because the current implementation doesn't - # support keep-alive. If the implementation supports - # keep-alive, it close the opend connection. - # - # @overload close - # Closes synchronously. - # - # @return [false] It always returns false because there is always - # no connection. - # - # @overload close {} - # Closes asynchronously. - # - # @yield [] Calls the block when the opened connection is closed. - # @return [#wait] The request object. If you want to wait until - # the request is processed. You can send #wait message to the - # request. - def close(&block) - sync = !block_given? - if sync - false - else - yield - EmptyRequest.new - end - end - - private - def send_request(http, command) - if command.name == "load" - raw_values = command[:values] - command[:values] = nil - path = command.to_uri_format - command[:values] = raw_values - request = Net::HTTP::Post.new(path) - request.content_type = "application/json" - request.body = raw_values - http.request(request) - else - http.get(command.to_uri_format) - end - end end end end -------------- next part -------------- HTML����������������������������...Download