connection pooling

This commit is contained in:
Martin Algesten
2018-06-30 16:52:54 +02:00
parent c5fb12a1fe
commit 4a5944443f
8 changed files with 205 additions and 81 deletions

View File

@@ -1,10 +1,59 @@
//
use agent::Unit;
use std::collections::HashMap;
use std::io::{Read, Result as IoResult};
use stream::Stream;
use url::Url;
#[derive(Debug, Default, Clone)]
pub struct ConnectionPool {}
#[derive(Default, Debug)]
pub struct ConnectionPool {
recycle: HashMap<Url, Stream>,
}
impl ConnectionPool {
pub fn new() -> Self {
ConnectionPool {}
ConnectionPool {
..Default::default()
}
}
pub fn try_get_connection(&mut self, url: &Url) -> Option<Stream> {
self.recycle.remove(url)
}
}
pub struct PoolReturnRead<R: Read + Sized> {
unit: Option<Unit>,
reader: Option<R>,
}
impl<R: Read + Sized> PoolReturnRead<R> {
pub fn new(unit: Option<Unit>, reader: R) -> Self {
PoolReturnRead {
unit,
reader: Some(reader),
}
}
fn return_connection(&mut self) {
if let Some(_unit) = self.unit.take() {}
}
fn do_read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
match self.reader.as_mut() {
None => return Ok(0),
Some(reader) => reader.read(buf),
}
}
}
impl<R: Read + Sized> Read for PoolReturnRead<R> {
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
let amount = self.do_read(buf)?;
// only if the underlying reader is exhausted can we send a new
// request to the same socket. hence, we only return it now.
if amount == 0 {
self.return_connection();
}
Ok(amount)
}
}