|
1 | 1 | require 'manticore'
|
| 2 | +require "elasticsearch/transport/transport/http/manticore/pool" |
2 | 3 |
|
3 | 4 | module Elasticsearch
|
4 | 5 | module Transport
|
@@ -43,97 +44,73 @@ module HTTP
|
43 | 44 | # @see Transport::Base
|
44 | 45 | #
|
45 | 46 | class Manticore
|
| 47 | + attr_reader :pool, :adapter |
46 | 48 | include Base
|
47 | 49 |
|
48 |
| - def initialize(arguments={}, &block) |
49 |
| - @manticore = build_client(arguments[:options] || {}) |
50 |
| - super(arguments, &block) |
51 |
| - end |
| 50 | + class Adapter |
| 51 | + attr_reader :manticore |
52 | 52 |
|
53 |
| - # Should just be run once at startup |
54 |
| - def build_client(options={}) |
55 |
| - client_options = options[:transport_options] || {} |
56 |
| - client_options[:ssl] = options[:ssl] || {} |
| 53 | + def initialize(options) |
| 54 | + build_client(options || {}) |
| 55 | + end |
57 | 56 |
|
58 |
| - @manticore = ::Manticore::Client.new(client_options) |
59 |
| - end |
| 57 | + # Should just be run once at startup |
| 58 | + def build_client(options={}) |
| 59 | + client_options = options[:transport_options] || {} |
| 60 | + client_options[:ssl] = options[:ssl] || {} |
60 | 61 |
|
61 |
| - # Performs the request by invoking {Transport::Base#perform_request} with a block. |
62 |
| - # |
63 |
| - # @return [Response] |
64 |
| - # @see Transport::Base#perform_request |
65 |
| - # |
66 |
| - def perform_request(method, path, params={}, body=nil) |
67 |
| - super do |connection, url| |
68 |
| - params[:body] = __convert_to_json(body) if body |
| 62 | + @request_options = options[:headers] ? {:headers => options[:headers]} : {} |
| 63 | + @manticore = ::Manticore::Client.new(client_options) |
| 64 | + end |
| 65 | + |
| 66 | + # Performs the request by invoking {Transport::Base#perform_request} with a block. |
| 67 | + # |
| 68 | + # @return [Response] |
| 69 | + # @see Transport::Base#perform_request |
| 70 | + # |
| 71 | + def perform_request(url, method, path, params={}, body=nil) |
69 | 72 | params = params.merge @request_options
|
| 73 | + params[:body] = body if body |
| 74 | + url_and_path = url + path |
70 | 75 | case method
|
71 |
| - when "GET" |
72 |
| - resp = connection.connection.get(url, params) |
73 |
| - when "HEAD" |
74 |
| - resp = connection.connection.head(url, params) |
75 |
| - when "PUT" |
76 |
| - resp = connection.connection.put(url, params) |
77 |
| - when "POST" |
78 |
| - resp = connection.connection.post(url, params) |
79 |
| - when "DELETE" |
80 |
| - resp = connection.connection.delete(url, params) |
81 |
| - else |
82 |
| - raise ArgumentError.new "Method #{method} not supported" |
| 76 | + when "GET" |
| 77 | + resp = @manticore.get(url_and_path, params) |
| 78 | + when "HEAD" |
| 79 | + resp = @manticore.head(url_and_path, params) |
| 80 | + when "PUT" |
| 81 | + resp = @manticore.put(url_and_path, params) |
| 82 | + when "POST" |
| 83 | + resp = @manticore.post(url_and_path, params) |
| 84 | + when "DELETE" |
| 85 | + resp = @manticore.delete(url_and_path, params) |
| 86 | + else |
| 87 | + raise ArgumentError.new "Method #{method} not supported" |
83 | 88 | end
|
84 | 89 | Response.new resp.code, resp.read_body, resp.headers
|
85 | 90 | end
|
86 |
| - end |
87 |
| - |
88 |
| - # Builds and returns a collection of connections. |
89 |
| - # Each connection is a Manticore::Client |
90 |
| - # |
91 |
| - # @return [Connections::Collection] |
92 |
| - # |
93 |
| - def __build_connections |
94 |
| - @request_options = {} |
95 | 91 |
|
96 |
| - if options.key?(:headers) |
97 |
| - @request_options[:headers] = options[:headers] |
| 92 | + def close |
| 93 | + @manticore.close |
98 | 94 | end
|
99 | 95 |
|
100 |
| - Connections::Collection.new \ |
101 |
| - :connections => hosts.map { |host| |
102 |
| - host[:protocol] = host[:scheme] || DEFAULT_PROTOCOL |
103 |
| - host[:port] ||= DEFAULT_PORT |
104 |
| - |
105 |
| - host.delete(:user) # auth is not supported here. |
106 |
| - host.delete(:password) # use the headers |
| 96 | + def add_url(url) |
| 97 | + end |
107 | 98 |
|
108 |
| - Connections::Connection.new \ |
109 |
| - :host => host, |
110 |
| - :connection => @manticore |
111 |
| - }, |
112 |
| - :selector_class => options[:selector_class], |
113 |
| - :selector => options[:selector] |
| 99 | + def remove_url(url) |
| 100 | + end |
114 | 101 | end
|
115 | 102 |
|
116 |
| - # Closes all connections by marking them as dead |
117 |
| - # and closing the underlying HttpClient instances |
118 |
| - # |
119 |
| - # @return [Connections::Collection] |
120 |
| - # |
121 |
| - def __close_connections |
122 |
| - @connections.each {|c| c.dead! } |
123 |
| - @connections.all.each {|c| c.connection.close } |
| 103 | + def initialize(arguments={}, &block) |
| 104 | + @adapter = Adapter.new(arguments[:options]) |
| 105 | + # TODO handle HTTPS |
| 106 | + @pool = Manticore::Pool.new(@adapter, arguments[:hosts].map {|h| URI::HTTP.build(h).to_s}) |
| 107 | + options = arguments[:options] || {} |
| 108 | + @serializer = options[:serializer] || ( options[:serializer_class] ? options[:serializer_class].new(self) : DEFAULT_SERIALIZER_CLASS.new(self) ) |
124 | 109 | end
|
125 | 110 |
|
126 |
| - # Returns an array of implementation specific connection errors. |
127 |
| - # |
128 |
| - # @return [Array] |
129 |
| - # |
130 |
| - def host_unreachable_exceptions |
131 |
| - [ |
132 |
| - ::Manticore::Timeout, |
133 |
| - ::Manticore::SocketException, |
134 |
| - ::Manticore::ClientProtocolException, |
135 |
| - ::Manticore::ResolutionFailure |
136 |
| - ] |
| 111 | + def perform_request(method, path, params={}, body=nil) |
| 112 | + body = __convert_to_json(body) if body |
| 113 | + @pool.perform_request(method, path, params, body) |
137 | 114 | end
|
138 | 115 | end
|
139 | 116 | end
|
|
0 commit comments