Class: Manticore::Client
- Inherits:
-
Object
- Object
- Manticore::Client
- Includes:
- ProxiesInterface
- Defined in:
- lib/manticore/client.rb,
lib/manticore/client/proxies.rb,
lib/manticore/client/trust_strategies.rb
Overview
Core Manticore client, with a backing PoolingHttpClientConnectionManager
Defined Under Namespace
Modules: ProxiesInterface, TrustStrategies Classes: AsyncProxy, BackgroundProxy, BaseProxy, CombinedTrustStrategy, CustomTrustStrategy, StubProxy
Constant Summary collapse
- DEFAULT_MAX_POOL_SIZE =
The default maximum pool size for requests
50
- DEFAULT_REQUEST_TIMEOUT =
60
- DEFAULT_SOCKET_TIMEOUT =
10
- DEFAULT_CONNECT_TIMEOUT =
10
- DEFAULT_MAX_REDIRECTS =
5
- DEFAULT_EXPECT_CONTINUE =
false
- DEFAULT_STALE_CHECK =
false
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Class Method Summary collapse
Instance Method Summary collapse
-
#clear_pending ⇒ Object
Remove all pending asynchronous requests.
-
#clear_stubs! ⇒ Object
Wipe all currently-set stubs.
-
#close(await: nil) ⇒ Object
Release resources held by this client, namely: - close the internal http client - shutdown the connection pool - stops accepting async requests in the executor.
-
#delete(url, options = {}, &block) ⇒ Response
Perform a HTTP DELETE request.
-
#execute! ⇒ Array
Execute all queued async requests.
-
#executor ⇒ Object
Get at the underlying ExecutorService used to invoke asynchronous calls.
-
#get(url, options = {}, &block) ⇒ Response
Perform a HTTP GET request.
-
#head(url, options = {}, &block) ⇒ Response
Perform a HTTP HEAD request.
-
#http(method, url, options = {}, &block) ⇒ Response
Perform an HTTP request, passing the method as a parameter.
-
#initialize(options = {}) {|builder, request_config| ... } ⇒ Client
constructor
Create a new HTTP client with a backing request pool.
-
#options(url, options = {}, &block) ⇒ Response
Perform a HTTP OPTIONS request.
-
#patch(url, options = {}, &block) ⇒ Response
Perform a HTTP PATCH request.
-
#pool_stats ⇒ Object
Return a hash of statistics about this client’s HTTP connection pool.
-
#post(url, options = {}, &block) ⇒ Response
Perform a HTTP POST request.
-
#put(url, options = {}, &block) ⇒ Response
Perform a HTTP PUT request.
-
#stub(url, stubs) ⇒ Object
Cause this client to return a stubbed response for this URL.
-
#unstub(url) ⇒ Object
Cause this client to unstubbed previously-stubbed URL.
Methods included from ProxiesInterface
#async, #background, #respond_with
Constructor Details
#initialize(options = {}) {|builder, request_config| ... } ⇒ Client
Create a new HTTP client with a backing request pool. if you pass a block to the initializer, the underlying HttpClientBuilder and RequestConfig.Builder will be yielded so that you can operate on them directly.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/manticore/client.rb', line 195 def initialize( = {}) @finalizer = Finalizer.new self.class.shutdown_on_finalize self, @finalizer builder = client_builder builder.set_user_agent .fetch(:user_agent, "Manticore #{VERSION}") @options = @use_cookies = .fetch(:cookies, false) builder. unless @use_cookies builder.disable_content_compression if .fetch(:compression, true) == false builder.set_proxy get_proxy_host([:proxy]) if .key?(:proxy) builder.set_retry_handler LoggingStandardRetryHandler.new .fetch(:automatic_retries, 3), .fetch(:retry_non_idempotent, false) # http://hc.apache.org/httpcomponents-client-ga/tutorial/html/advanced.html#stateful_conn # By default this is used to prevent different contexts from accessing SSL data # Since we're running this for JRuby which does not have context separation within the JVM # We can disable this for connection reuse. builder.disable_connection_state unless .fetch(:ssl, {}).fetch(:track_state, false) @keepalive = .fetch(:keepalive, true) if @keepalive == false builder.set_connection_reuse_strategy { |response, context| false } else builder.set_connection_reuse_strategy DefaultConnectionReuseStrategy.new end builder.set_default_socket_config () builder.set_connection_manager pool() request_config = RequestConfig.custom request_config.set_connection_request_timeout .fetch(:request_timeout, DEFAULT_REQUEST_TIMEOUT) * 1000 request_config.set_connect_timeout .fetch(:connect_timeout, DEFAULT_CONNECT_TIMEOUT) * 1000 request_config.set_socket_timeout .fetch(:socket_timeout, DEFAULT_SOCKET_TIMEOUT) * 1000 request_config.set_max_redirects .fetch(:max_redirects, DEFAULT_MAX_REDIRECTS) request_config.set_expect_continue_enabled .fetch(:expect_continue, DEFAULT_EXPECT_CONTINUE) request_config.set_stale_connection_check_enabled .fetch(:stale_check, DEFAULT_STALE_CHECK) request_config.set_circular_redirects_allowed false yield builder, request_config if block_given? builder.set_default_request_config request_config.build @client = builder.build finalize @client, :close @options = @async_requests = Queue.new @stubs = {} end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
134 135 136 |
# File 'lib/manticore/client.rb', line 134 def client @client end |
Class Method Details
.shutdown_on_finalize(client, finalizer) ⇒ Object
413 414 415 |
# File 'lib/manticore/client.rb', line 413 def self.shutdown_on_finalize(client, finalizer) ObjectSpace.define_finalizer(client, finalizer) end |
Instance Method Details
#clear_pending ⇒ Object
Remove all pending asynchronous requests.
341 342 343 344 |
# File 'lib/manticore/client.rb', line 341 def clear_pending @async_requests.clear nil end |
#clear_stubs! ⇒ Object
Wipe all currently-set stubs.
334 335 336 |
# File 'lib/manticore/client.rb', line 334 def clear_stubs! @stubs.clear end |
#close(await: nil) ⇒ Object
In versions before 0.9 this method only closed the underlying ‘CloseableHttpClient`
Release resources held by this client, namely:
- close the internal http client
- shutdown the connection pool
- stops accepting async requests in the executor
After this call the client is no longer usable.
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/manticore/client.rb', line 370 def close(await: nil) ObjectSpace.undefine_finalizer(self) @finalizer.call # which does ~ : # @executor&.shutdown rescue nil # @client&.close # @pool&.shutdown rescue nil @async_requests.close case await when false, nil @executor&.shutdown_now rescue nil when Numeric # NOTE: the concept of awaiting gracefully might/should also be used with the pool/client closing millis = java.util.concurrent.TimeUnit::MILLISECONDS @executor&.await_termination(await * 1000, millis) rescue nil else nil end end |
#delete(url, options = {}, &block) ⇒ Response
Perform a HTTP DELETE request
285 286 287 288 |
# File 'lib/manticore/client.rb', line 285 def delete(url, = {}, &block) = treat_params_as_query() request HttpDeleteWithEntity, url, , &block end |
#execute! ⇒ Array
Execute all queued async requests
349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/manticore/client.rb', line 349 def execute! method = executor.java_method(:submit, [java.util.concurrent.Callable.java_class]) result = [] result << method.call(@async_requests.pop) until @async_requests.empty? result.map do |future| begin future.get rescue Java::JavaUtilConcurrent::ExecutionException => e # These exceptions should be handled in on_failure blocks. end end end |
#executor ⇒ Object
Get at the underlying ExecutorService used to invoke asynchronous calls.
408 409 410 |
# File 'lib/manticore/client.rb', line 408 def executor @executor ||= create_executor end |
#get(url, options = {}, &block) ⇒ Response
Perform a HTTP GET request
259 260 261 262 |
# File 'lib/manticore/client.rb', line 259 def get(url, = {}, &block) = treat_params_as_query() request HttpGetWithEntity, url, , &block end |
#head(url, options = {}, &block) ⇒ Response
Perform a HTTP HEAD request
272 273 274 275 |
# File 'lib/manticore/client.rb', line 272 def head(url, = {}, &block) = treat_params_as_query() request HttpHead, url, , &block end |
#http(method, url, options = {}, &block) ⇒ Response
Perform an HTTP request, passing the method as a parameter
312 313 314 315 316 317 318 319 |
# File 'lib/manticore/client.rb', line 312 def http(method, url, = {}, &block) case method.to_s.downcase when *%w(get put head post delete options patch) send method, url, , &block else raise "Invalid method: #{method}" end end |
#options(url, options = {}, &block) ⇒ Response
Perform a HTTP OPTIONS request
292 293 294 |
# File 'lib/manticore/client.rb', line 292 def (url, = {}, &block) request HttpOptions, url, , &block end |
#patch(url, options = {}, &block) ⇒ Response
Perform a HTTP PATCH request
298 299 300 |
# File 'lib/manticore/client.rb', line 298 def patch(url, = {}, &block) request HttpPatch, url, , &block end |
#pool_stats ⇒ Object
Return a hash of statistics about this client’s HTTP connection pool
245 246 247 248 249 250 251 252 253 |
# File 'lib/manticore/client.rb', line 245 def pool_stats stats = @pool.get_total_stats { max: stats.get_max, leased: stats.get_leased, pending: stats.get_pending, available: stats.get_available, } end |
#post(url, options = {}, &block) ⇒ Response
Perform a HTTP POST request
279 280 281 |
# File 'lib/manticore/client.rb', line 279 def post(url, = {}, &block) request HttpPost, url, , &block end |
#put(url, options = {}, &block) ⇒ Response
Perform a HTTP PUT request
266 267 268 |
# File 'lib/manticore/client.rb', line 266 def put(url, = {}, &block) request HttpPut, url, , &block end |
#stub(url, stubs) ⇒ Object
Cause this client to return a stubbed response for this URL
324 325 326 |
# File 'lib/manticore/client.rb', line 324 def stub(url, stubs) @stubs[url_as_regex(url)] = stubs end |
#unstub(url) ⇒ Object
Cause this client to unstubbed previously-stubbed URL
329 330 331 |
# File 'lib/manticore/client.rb', line 329 def unstub(url) @stubs.delete(url_as_regex(url)) end |