-
-
Notifications
You must be signed in to change notification settings - Fork 62
Connection Reuse
By default, Isahc makes use of a feature called connection reuse to improve performance and throughput for subsequent HTTP requests to the same host. After HTTP requests are completed, if the underlying connection is reusable, it is added to the connection cache to be reused to reduce latency for future requests. Requests for the same host will use an open connection from the cache if one is available.
You can customize how the connection cache behaves when creating a custom client instance using HttpClientBuilder
. The relevant builder methods are:
-
connection_cache_size
: Set a limit on how many connections can be kept in the cache. -
connection_cache_ttl
: Control how long unused connections are kept in the cache before being closed.
If you don't want connection reuse, you can disable the connection cache entirely by calling connection_cache_size(0)
on your client builder.
If a response stream is dropped before it has been completely read from, then that HTTP connection will be terminated. For HTTP/1.1 this requires closing the socket connection entirely since there's no way in HTTP/1.1 to ask the server to stop sending the response body once it has already started doing so. This means that we can't put such connections in the connection cache and we lose the ability to reuse that connection.
If you are downloading a file on behalf of a user and have been requested to cancel the operation, then this is probably OK behavior. But if you are making many small API calls to a known server, then you may want to call consume
instead of dropping a response not fully read. This method will read any remaining data from the response stream and immediately discard it, and makes sure the connection can be reused for future requests. In most situations, reading a couple megabytes off a socket is usually more efficient in the long run than taking a hit on connection reuse, and opening new TCP connections can be expensive.
If you are only interested in the response headers of a GET
request and not in the response body, you can try issuing a HEAD
request instead of a GET
request. If the web server supports it, this avoids the need to close the connection early.
Since not reading the entirety of an HTTP response can reduce performance and can be indicative of a mistake by the programmer, Isahc will emit a log message when this happens that looks like this:
response dropped without fully consuming the response body, connection won't be reused
Aborting a response without fully consuming the response body can result in sub-optimal performance.
See https://github.com/sagebind/isahc/wiki/Connection-Reuse#closing-connections-early.
In HTTP/2 and newer there is a mechanism to signal the server to stop sending its response, so there is no reuse penalty for terminating a response stream early.
The connection cache is provided by libcurl. Isahc does little to change how connection reuse works in libcurl, aside from making it easy to customize its behavior, because it already works great!