Class: UniversalRenderer::Client::HttpPool
- Inherits:
-
Object
- Object
- UniversalRenderer::Client::HttpPool
- Defined in:
- lib/universal_renderer/client/http_pool.rb
Overview
Thread-safe pool of persistent Net::HTTP connections keyed by SSR origin.
Net::HTTP instances are not thread-safe, so each pooled connection is checked out exclusively. Keeping them started allows Net::HTTP to reuse the underlying TCP connection for sequential SSR requests.
Defined Under Namespace
Classes: ClientProxy
Class Method Summary collapse
- .client(uri, timeout) ⇒ Object
- .close(http) ⇒ Object
- .request(uri, timeout, http_request) ⇒ Object
- .reset! ⇒ Object
Class Method Details
.client(uri, timeout) ⇒ Object
32 33 34 |
# File 'lib/universal_renderer/client/http_pool.rb', line 32 def client(uri, timeout) ClientProxy.new(uri, timeout) end |
.close(http) ⇒ Object
53 54 55 56 57 |
# File 'lib/universal_renderer/client/http_pool.rb', line 53 def close(http) http.finish if http.started? rescue IOError # Already closed by the peer or by another cleanup path. end |
.request(uri, timeout, http_request) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/universal_renderer/client/http_pool.rb', line 36 def request(uri, timeout, http_request) with_connection(uri, timeout) do |http| if block_given? http.request(http_request) { |response| yield(response, http) } else http.request(http_request) end end end |
.reset! ⇒ Object
46 47 48 49 50 51 |
# File 'lib/universal_renderer/client/http_pool.rb', line 46 def reset! mutex.synchronize do pools.each_value { |pool| pool.shutdown { |http| close(http) } } pools.clear end end |