1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-25 08:52:42 +01:00
actix-web/src/client/connection.rs

103 lines
2.6 KiB
Rust
Raw Normal View History

2018-11-12 08:12:54 +01:00
use std::{fmt, io, time};
use futures::Poll;
use tokio_io::{AsyncRead, AsyncWrite};
use super::pool::Acquired;
2018-11-15 20:10:23 +01:00
pub trait Connection: AsyncRead + AsyncWrite + 'static {
/// Close connection
fn close(&mut self);
/// Release connection to the connection pool
fn release(&mut self);
}
#[doc(hidden)]
2018-11-12 08:12:54 +01:00
/// HTTP client connection
2018-11-15 20:10:23 +01:00
pub struct IoConnection<T> {
io: Option<T>,
2018-11-12 08:12:54 +01:00
created: time::Instant,
pool: Option<Acquired<T>>,
}
2018-11-15 20:10:23 +01:00
impl<T> fmt::Debug for IoConnection<T>
2018-11-12 08:12:54 +01:00
where
2018-11-14 07:53:30 +01:00
T: fmt::Debug,
2018-11-12 08:12:54 +01:00
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Connection {:?}", self.io)
}
}
2018-11-15 20:10:23 +01:00
impl<T: AsyncRead + AsyncWrite + 'static> IoConnection<T> {
2018-11-12 08:12:54 +01:00
pub(crate) fn new(io: T, created: time::Instant, pool: Acquired<T>) -> Self {
2018-11-15 20:10:23 +01:00
IoConnection {
2018-11-12 08:12:54 +01:00
created,
2018-11-15 20:10:23 +01:00
io: Some(io),
2018-11-12 08:12:54 +01:00
pool: Some(pool),
}
}
/// Raw IO stream
pub fn get_mut(&mut self) -> &mut T {
2018-11-15 20:10:23 +01:00
self.io.as_mut().unwrap()
2018-11-12 08:12:54 +01:00
}
2018-11-15 20:10:23 +01:00
pub(crate) fn into_inner(self) -> (T, time::Instant) {
(self.io.unwrap(), self.created)
}
}
impl<T: AsyncRead + AsyncWrite + 'static> Connection for IoConnection<T> {
2018-11-12 08:12:54 +01:00
/// Close connection
2018-11-15 20:10:23 +01:00
fn close(&mut self) {
2018-11-12 08:12:54 +01:00
if let Some(mut pool) = self.pool.take() {
2018-11-15 20:10:23 +01:00
if let Some(io) = self.io.take() {
pool.close(IoConnection {
io: Some(io),
created: self.created,
pool: None,
})
}
2018-11-12 08:12:54 +01:00
}
}
/// Release this connection to the connection pool
2018-11-15 20:10:23 +01:00
fn release(&mut self) {
2018-11-12 08:12:54 +01:00
if let Some(mut pool) = self.pool.take() {
2018-11-15 20:10:23 +01:00
if let Some(io) = self.io.take() {
pool.release(IoConnection {
io: Some(io),
created: self.created,
pool: None,
})
}
2018-11-12 08:12:54 +01:00
}
}
}
2018-11-15 20:10:23 +01:00
impl<T: AsyncRead + AsyncWrite + 'static> io::Read for IoConnection<T> {
2018-11-12 08:12:54 +01:00
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
2018-11-15 20:10:23 +01:00
self.io.as_mut().unwrap().read(buf)
2018-11-12 08:12:54 +01:00
}
}
2018-11-15 20:10:23 +01:00
impl<T: AsyncRead + AsyncWrite + 'static> AsyncRead for IoConnection<T> {}
2018-11-12 08:12:54 +01:00
2018-11-15 20:10:23 +01:00
impl<T: AsyncRead + AsyncWrite + 'static> io::Write for IoConnection<T> {
2018-11-12 08:12:54 +01:00
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
2018-11-15 20:10:23 +01:00
self.io.as_mut().unwrap().write(buf)
2018-11-12 08:12:54 +01:00
}
fn flush(&mut self) -> io::Result<()> {
2018-11-15 20:10:23 +01:00
self.io.as_mut().unwrap().flush()
2018-11-12 08:12:54 +01:00
}
}
2018-11-15 20:10:23 +01:00
impl<T: AsyncRead + AsyncWrite + 'static> AsyncWrite for IoConnection<T> {
2018-11-12 08:12:54 +01:00
fn shutdown(&mut self) -> Poll<(), io::Error> {
2018-11-15 20:10:23 +01:00
self.io.as_mut().unwrap().shutdown()
2018-11-12 08:12:54 +01:00
}
}