Ruby latest stable (v2_5_5) - 0 notes - Superclass: Object
  • 1_8_6_287
  • 1_8_7_72
  • 1_8_7_330
  • 1_9_1_378
  • 1_9_2_180
  • 1_9_3_125
  • 1_9_3_392
  • 2_1_10
  • 2_2_9
  • 2_4_6
  • 2_5_5
  • 2_6_3 (0)
  • What's this?

Class not available on this version

This class is only available on newer versions. The first available version of the class is shown here.

Persistent connections for Net::HTTP

Bundler::Persistent::Net::HTTP::Persistent maintains persistent connections across all the servers you wish to talk to. For each host:port you communicate with a single persistent connection is created.

Multiple Bundler::Persistent::Net::HTTP::Persistent objects will share the same set of connections.

For each thread you start a new connection will be created. A Bundler::Persistent::Net::HTTP::Persistent connection will not be shared across threads.

You can shut down the HTTP connections when done by calling #shutdown. You should name your Bundler::Persistent::Net::HTTP::Persistent object if you intend to call this method.


require 'bundler/vendor/net-http-persistent/lib/net/http/persistent'

uri = URI 'http://example.com/awesome/web/service'

http = Bundler::Persistent::Net::HTTP::Persistent.new 'my_app_name'

# perform a GET
response = http.request uri

# or

get = Net::HTTP::Get.new uri.request_uri
response = http.request get

# create a POST
post_uri = uri + 'create'
post = Net::HTTP::Post.new post_uri.path
post.set_form_data 'some' => 'cool data'

# perform the POST, the URI is always required
response http.request post_uri, post

Note that for GET, HEAD and other requests that do not have a body you want to use URI#request_uri not URI#path. The request_uri contains the query params which are sent in the body for other requests.


SSL connections are automatically created depending upon the scheme of the URI. SSL connections are automatically verified against the default certificate store for your computer. You can override this by changing verify_mode or by specifying an alternate cert_store.

Here are the SSL settings, see the individual methods for documentation:


This client’s certificate


The certificate-authority


An SSL certificate store


The client’s SSL private key


Reuse a previously opened SSL session for a new connection


Which specific SSL version to use


For server certificate verification


How connections should be verified


A proxy can be set through #proxy= or at initialization time by providing a second argument to ::new. The proxy may be the URI of the proxy server or :ENV which will consult environment variables.

See #proxy= and #proxy_from_env for details.


Headers may be specified for use in every request. #headers are appended to any headers on the request. #override_headers replace existing headers on the request.

The difference between the two can be seen in setting the User-Agent. Using http.headers[‘User-Agent’] = ‘MyUserAgent’ will send “Ruby, MyUserAgent” while http.override_headers[‘User-Agent’] = ‘MyUserAgent’ will send “MyUserAgent”.



By providing an application name to ::new you can separate your connections from the connections of other applications.

Idle Timeout

If a connection hasn’t been used for this number of seconds it will automatically be reset upon the next use to avoid attempting to send to a closed connection. The default value is 5 seconds. nil means no timeout. Set through #idle_timeout.

Reducing this value may help avoid the “too many connection resets” error when sending non-idempotent requests while increasing this value will cause fewer round-trips.

Read Timeout

The amount of time allowed between reading two chunks from the socket. Set through #read_timeout

Max Requests

The number of requests that should be made before opening a new connection. Typically many keep-alive capable servers tune this to 100 or less, so the 101st request will fail with ECONNRESET. If unset (default), this value has no effect, if set, connections will be reset on the request after max_requests.

Open Timeout

The amount of time to wait for a connection to be opened. Set through #open_timeout.

Socket Options

Socket options may be set on newly-created connections. See #socket_options for details.

Non-Idempotent Requests

By default non-idempotent requests will not be retried per RFC 2616. By setting retry_change_requests to true requests will automatically be retried once.

Only do this when you know that retrying a POST or other non-idempotent request is safe for your application and will not create duplicate resources.

The recommended way to handle non-idempotent requests is the following:

require 'bundler/vendor/net-http-persistent/lib/net/http/persistent'

uri = URI 'http://example.com/awesome/web/service'
post_uri = uri + 'create'

http = Bundler::Persistent::Net::HTTP::Persistent.new 'my_app_name'

post = Net::HTTP::Post.new post_uri.path
# ... fill in POST request

  response = http.request post_uri, post
rescue Bundler::Persistent::Net::HTTP::Persistent::Error

  # POST failed, make a new request to verify the server did not process
  # the request
  exists_uri = uri + '...'
  response = http.get exists_uri

  # Retry if it failed
  retry if response.code == '404'

The method of determining if the resource was created or not is unique to the particular service you are using. Of course, you will want to add protection from infinite looping.

Connection Termination

If you are done using the Bundler::Persistent::Net::HTTP::Persistent instance you may shut down all the connections in the current thread with #shutdown. This is not recommended for normal use, it should only be used when it will be several minutes before you make another HTTP request.

If you are using multiple threads, call #shutdown in each thread when the thread is done making requests. If you don’t call shutdown, that’s OK. Ruby will automatically garbage collect and shutdown your HTTP connections when the thread terminates.


RETRIED_EXCEPTIONS = [ # :nodoc: (Net::ReadTimeout if Net.const_defined? :ReadTimeout), IOError, EOFError, Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPIPE, (OpenSSL::SSL::SSLError if HAVE_OPENSSL), Timeout::Error, ].compact

VERSION = '2.9.4'

HAVE_OPENSSL = defined? OpenSSL::SSL # :nodoc:

EPOCH = Time.at 0


[RW] retry_change_requests

Enable retries of non-idempotent requests that change data (e.g. POST requests) when the server has disconnected.

This will in the worst case lead to multiple requests with the same data, but it may be useful for some applications. Take care when enabling this option to ensure it is safe to POST or perform other non-idempotent requests to the server.

[R] verify_mode

HTTPS verify mode. Defaults to OpenSSL::SSL::VERIFY_PEER which verifies the server certificate.

If no ca_file or cert_store is set the default system certificate store is used.

You can use verify_mode to override any default values.

[R] verify_callback

SSL verification callback. Used when ca_file is set.

[R] timeout_key

Where this instance’s last-use times live in the thread local variables

[R] ssl_version

SSL version to use.

By default, the version will be negotiated automatically between client and server. Ruby 1.9 and newer only.

[R] ssl_generation_key

Where this instance’s SSL connections live in the thread local variables

[R] ssl_generation

Current SSL connection generation

[R] socket_options

An array of options for Socket#setsockopt.

By default the TCP_NODELAY option is set on sockets.

To set additional options append them to this array:

http.socket_options << [Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1]
[RW] reuse_ssl_sessions

By default SSL sessions are reused to avoid extra SSL handshakes. Set this to false if you have problems communicating with an HTTPS server like:

SSL_connect [...] read finished A: unexpected message (OpenSSL::SSL::SSLError)
[R] request_key

Where this instance’s request counts live in the thread local variables

[RW] read_timeout

Seconds to wait until reading one block. See Net::HTTP#read_timeout

[R] no_proxy

List of host suffixes which will not be proxied

[R] proxy_uri

The URL through which requests will be proxied

[R] key

This client’s SSL private key

[R] private_key

This client’s SSL private key

[R] override_headers

Headers that are added to every request using Net::HTTP#[]=

[RW] open_timeout

Seconds to wait until a connection is opened. See Net::HTTP#open_timeout

[R] name

A name for this connection. Allows you to keep your connections apart from everybody else’s.

[RW] keep_alive

The value sent in the Keep-Alive header. Defaults to 30. Not needed for HTTP/1.1 servers.

This may not work correctly for HTTP/1.0 servers

This method may be removed in a future version as RFC 2616 does not require this header.

[RW] max_requests

Maximum number of requests on a connection before it is considered expired and automatically closed.

[RW] idle_timeout

Maximum time an unused connection can remain idle before being automatically closed.

[R] http_versions

Maps host:port to an HTTP version. This allows us to enable version specific features.

[R] headers

Headers that are added to every request using Net::HTTP#add_field

[R] generation_key

Where this instance’s connections live in the thread local variables

[R] generation

Current connection generation

[RW] debug_output

Sends debug_output to this IO via Net::HTTP#set_debug_output.

Never use this method in production code, it causes a serious security hole.

[R] cert_store

An SSL certificate store. Setting this will override the default certificate store. See verify_mode for more information.

[R] ca_file

An SSL certificate authority. Setting this will set verify_mode to VERIFY_PEER.

[R] cert

This client’s OpenSSL::X509::Certificate

[R] certificate

This client’s OpenSSL::X509::Certificate

Show files where this class is defined (1 file)
Register or log in to add new notes.