feat(client): add a Connection Pool

This adds a connection pool to the Client that is used by default. It
accepts any other NetworkConnector, and simply acts as a
NetworkConnector itself. Other Pools can exist by simply providing a
custom NetworkConnector. This Pool is only used by default if you also
use the default connector, which is `HttpConnector`. If you wish to use
the Pool with a custom connector, you'll need to create the Pool with
your custom connector, and then pass that pool to the
Client::with_connector.

This also adds a method to `NetworkStream`, `close`, which can be used
to know when the Stream should be put down, because a server requested
that the connection close instead of be kept alive.

Closes #363
Closes #41
This commit is contained in:
Sean McArthur
2015-01-23 13:15:15 -08:00
parent 362044c32c
commit 1e72a8ab3a
7 changed files with 281 additions and 15 deletions

View File

@@ -46,15 +46,17 @@ use status::StatusClass::Redirection;
use {Url, HttpResult};
use HttpError::HttpUriError;
pub use self::pool::Pool;
pub use self::request::Request;
pub use self::response::Response;
pub mod pool;
pub mod request;
pub mod response;
/// A Client to use additional features with Requests.
///
/// Clients can handle things such as: redirect policy.
/// Clients can handle things such as: redirect policy, connection pooling.
pub struct Client {
connector: Connector,
redirect_policy: RedirectPolicy,
@@ -64,7 +66,12 @@ impl Client {
/// Create a new Client.
pub fn new() -> Client {
Client::with_connector(HttpConnector(None))
Client::with_pool_config(Default::default())
}
/// Create a new Client with a configured Pool Config.
pub fn with_pool_config(config: pool::Config) -> Client {
Client::with_connector(Pool::new(config))
}
/// Create a new client with a specific connector.
@@ -78,7 +85,10 @@ impl Client {
/// Set the SSL verifier callback for use with OpenSSL.
pub fn set_ssl_verifier(&mut self, verifier: ContextVerifier) {
self.connector = with_connector(HttpConnector(Some(verifier)));
self.connector = with_connector(Pool::with_connector(
Default::default(),
HttpConnector(Some(verifier))
));
}
/// Set the RedirectPolicy.