1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-21 15:46:48 +00:00

optimize Request handling

This commit is contained in:
Nikolay Kim 2018-07-04 22:52:49 +06:00
parent f559f23e1c
commit 5d79114239
7 changed files with 134 additions and 79 deletions

View file

@ -28,7 +28,7 @@ struct Cookies(Vec<Cookie<'static>>);
/// An HTTP Request
pub struct HttpRequest<S = ()> {
req: Rc<Request>,
req: Option<Request>,
state: Rc<S>,
route: RouteInfo,
}
@ -38,12 +38,12 @@ impl<S> HttpMessage for HttpRequest<S> {
#[inline]
fn headers(&self) -> &HeaderMap {
self.req.headers()
self.request().headers()
}
#[inline]
fn payload(&self) -> Payload {
if let Some(payload) = self.req.inner.payload.borrow_mut().take() {
if let Some(payload) = self.request().inner.payload.borrow_mut().take() {
payload
} else {
Payload::empty()
@ -55,7 +55,7 @@ impl<S> Deref for HttpRequest<S> {
type Target = Request;
fn deref(&self) -> &Request {
self.req.as_ref()
self.request()
}
}
@ -65,7 +65,7 @@ impl<S> HttpRequest<S> {
HttpRequest {
state,
route,
req: Rc::new(req),
req: Some(req),
}
}
@ -98,38 +98,40 @@ impl<S> HttpRequest<S> {
#[inline]
/// Server request
pub fn request(&self) -> &Request {
&self.req
self.req.as_ref().unwrap()
}
/// Request extensions
#[inline]
pub fn extensions(&self) -> Ref<Extensions> {
self.req.extensions()
self.request().extensions()
}
/// Mutable reference to a the request's extensions
#[inline]
pub fn extensions_mut(&self) -> RefMut<Extensions> {
self.req.extensions_mut()
self.request().extensions_mut()
}
/// Default `CpuPool`
#[inline]
#[doc(hidden)]
pub fn cpu_pool(&self) -> &CpuPool {
self.req.server_settings().cpu_pool()
self.request().server_settings().cpu_pool()
}
#[inline]
/// Create http response
pub fn response(&self, status: StatusCode, body: Body) -> HttpResponse {
self.req.server_settings().get_response(status, body)
self.request().server_settings().get_response(status, body)
}
#[inline]
/// Create http response builder
pub fn build_response(&self, status: StatusCode) -> HttpResponseBuilder {
self.req.server_settings().get_response_builder(status)
self.request()
.server_settings()
.get_response_builder(status)
}
/// Read the Request Uri.
@ -314,6 +316,14 @@ impl<S> HttpRequest<S> {
}
}
impl<S> Drop for HttpRequest<S> {
fn drop(&mut self) {
if let Some(req) = self.req.take() {
req.release();
}
}
}
impl<S> Clone for HttpRequest<S> {
fn clone(&self) -> HttpRequest<S> {
HttpRequest {

View file

@ -365,7 +365,7 @@ where
}
// set remote addr
msg.inner.addr = self.addr;
msg.inner_mut().addr = self.addr;
// stop keepalive timer
self.keepalive_timer.take();

View file

@ -118,9 +118,9 @@ impl H1Decoder {
let slice = buf.split_to(len).freeze();
// convert headers
let mut msg = settings.get_request_context();
let mut msg = settings.get_request();
{
let inner = &mut msg.inner;
let inner = msg.inner_mut();
inner
.flags
.get_mut()

View file

@ -329,13 +329,16 @@ impl<H: HttpHandler + 'static> Entry<H> {
// Payload and Content-Encoding
let (psender, payload) = Payload::new(false);
let mut msg = settings.get_request_context();
msg.inner.url = Url::new(parts.uri);
msg.inner.method = parts.method;
msg.inner.version = parts.version;
msg.inner.headers = parts.headers;
*msg.inner.payload.borrow_mut() = Some(payload);
msg.inner.addr = addr;
let mut msg = settings.get_request();
{
let inner = msg.inner_mut();
inner.url = Url::new(parts.uri);
inner.method = parts.method;
inner.version = parts.version;
inner.headers = parts.headers;
*inner.payload.borrow_mut() = Some(payload);
inner.addr = addr;
}
// Payload sender
let psender = PayloadType::new(msg.headers(), psender);

View file

@ -1,6 +1,7 @@
use std::cell::{Cell, Ref, RefCell, RefMut};
use std::collections::VecDeque;
use std::net::SocketAddr;
use std::rc::Rc;
use http::{header, HeaderMap, Method, Uri, Version};
@ -19,8 +20,9 @@ bitflags! {
}
/// Request's context
#[derive(Clone)]
pub struct Request {
pub(crate) inner: Box<InnerRequest>,
pub(crate) inner: Rc<InnerRequest>,
}
pub(crate) struct InnerRequest {
@ -34,6 +36,18 @@ pub(crate) struct InnerRequest {
pub(crate) info: RefCell<ConnectionInfo>,
pub(crate) payload: RefCell<Option<Payload>>,
pub(crate) settings: ServerSettings,
pool: &'static RequestPool,
}
impl InnerRequest {
#[inline]
/// Reset request instance
pub fn reset(&mut self) {
self.headers.clear();
self.extensions.borrow_mut().clear();
self.flags.set(MessageFlags::empty());
*self.payload.borrow_mut() = None;
}
}
impl HttpMessage for Request {
@ -55,9 +69,10 @@ impl HttpMessage for Request {
impl Request {
/// Create new RequestContext instance
pub fn new(settings: ServerSettings) -> Request {
pub(crate) fn new(pool: &'static RequestPool, settings: ServerSettings) -> Request {
Request {
inner: Box::new(InnerRequest {
inner: Rc::new(InnerRequest {
pool,
settings,
method: Method::GET,
url: InnerUrl::default(),
@ -72,45 +87,55 @@ impl Request {
}
}
#[inline]
pub(crate) fn inner(&self) -> &InnerRequest {
self.inner.as_ref()
}
#[inline]
pub(crate) fn inner_mut(&mut self) -> &mut InnerRequest {
Rc::get_mut(&mut self.inner).expect("Multiple copies exist")
}
#[inline]
pub(crate) fn url(&self) -> &InnerUrl {
&self.inner.url
&self.inner().url
}
/// Read the Request Uri.
#[inline]
pub fn uri(&self) -> &Uri {
self.inner.url.uri()
self.inner().url.uri()
}
/// Read the Request method.
#[inline]
pub fn method(&self) -> &Method {
&self.inner.method
&self.inner().method
}
/// Read the Request Version.
#[inline]
pub fn version(&self) -> Version {
self.inner.version
self.inner().version
}
/// The target path of this Request.
#[inline]
pub fn path(&self) -> &str {
self.inner.url.path()
self.inner().url.path()
}
#[inline]
/// Returns Request's headers.
pub fn headers(&self) -> &HeaderMap {
&self.inner.headers
&self.inner().headers
}
#[inline]
/// Returns mutable Request's headers.
pub fn headers_mut(&mut self) -> &mut HeaderMap {
&mut self.inner.headers
&mut self.inner_mut().headers
}
/// Peer socket address
@ -121,67 +146,71 @@ impl Request {
/// To get client connection information `connection_info()` method should
/// be used.
pub fn peer_addr(&self) -> Option<SocketAddr> {
self.inner.addr
self.inner().addr
}
/// Checks if a connection should be kept alive.
#[inline]
pub fn keep_alive(&self) -> bool {
self.inner.flags.get().contains(MessageFlags::KEEPALIVE)
self.inner().flags.get().contains(MessageFlags::KEEPALIVE)
}
/// Request extensions
#[inline]
pub fn extensions(&self) -> Ref<Extensions> {
self.inner.extensions.borrow()
self.inner().extensions.borrow()
}
/// Mutable reference to a the request's extensions
#[inline]
pub fn extensions_mut(&self) -> RefMut<Extensions> {
self.inner.extensions.borrow_mut()
self.inner().extensions.borrow_mut()
}
/// Check if request requires connection upgrade
pub fn upgrade(&self) -> bool {
if let Some(conn) = self.inner.headers.get(header::CONNECTION) {
if let Some(conn) = self.inner().headers.get(header::CONNECTION) {
if let Ok(s) = conn.to_str() {
return s.to_lowercase().contains("upgrade");
}
}
self.inner.method == Method::CONNECT
self.inner().method == Method::CONNECT
}
/// Get *ConnectionInfo* for the correct request.
pub fn connection_info(&self) -> Ref<ConnectionInfo> {
if self.inner.flags.get().contains(MessageFlags::CONN_INFO) {
self.inner.info.borrow()
if self.inner().flags.get().contains(MessageFlags::CONN_INFO) {
self.inner().info.borrow()
} else {
let mut flags = self.inner.flags.get();
let mut flags = self.inner().flags.get();
flags.insert(MessageFlags::CONN_INFO);
self.inner.flags.set(flags);
self.inner.info.borrow_mut().update(self);
self.inner.info.borrow()
self.inner().flags.set(flags);
self.inner().info.borrow_mut().update(self);
self.inner().info.borrow()
}
}
/// Server settings
#[inline]
pub fn server_settings(&self) -> &ServerSettings {
&self.inner.settings
&self.inner().settings
}
#[inline]
/// Reset request instance
pub fn reset(&mut self) {
self.inner.headers.clear();
self.inner.extensions.borrow_mut().clear();
self.inner.flags.set(MessageFlags::empty());
*self.inner.payload.borrow_mut() = None;
pub(crate) fn release(self) {
let mut inner = self.inner;
if let Some(r) = Rc::get_mut(&mut inner) {
r.reset();
} else {
return;
}
inner.pool.release(inner);
}
}
pub(crate) struct RequestPool(RefCell<VecDeque<Request>>, RefCell<ServerSettings>);
pub(crate) struct RequestPool(
RefCell<VecDeque<Rc<InnerRequest>>>,
RefCell<ServerSettings>,
);
thread_local!(static POOL: &'static RequestPool = RequestPool::create());
@ -202,20 +231,19 @@ impl RequestPool {
}
#[inline]
pub fn get(&self) -> Request {
if let Some(msg) = self.0.borrow_mut().pop_front() {
msg
pub fn get(pool: &'static RequestPool) -> Request {
if let Some(msg) = pool.0.borrow_mut().pop_front() {
Request { inner: msg }
} else {
Request::new(self.1.borrow().clone())
Request::new(pool, pool.1.borrow().clone())
}
}
#[inline]
/// Release request instance
pub fn release(&self, mut msg: Request) {
pub fn release(&self, msg: Rc<InnerRequest>) {
let v = &mut self.0.borrow_mut();
if v.len() < 128 {
msg.reset();
v.push_front(msg);
}
}

View file

@ -212,8 +212,8 @@ impl<H> WorkerSettings<H> {
self.bytes.release_bytes(bytes)
}
pub fn get_request_context(&self) -> Request {
self.messages.get()
pub fn get_request(&self) -> Request {
RequestPool::get(self.messages)
}
pub fn add_channel(&self) {

View file

@ -29,7 +29,8 @@ use param::Params;
use payload::Payload;
use resource::ResourceHandler;
use router::Router;
use server::{HttpServer, IntoHttpHandler, Request, ServerSettings};
use server::message::{Request, RequestPool};
use server::{HttpServer, IntoHttpHandler, ServerSettings};
use uri::Url as InnerUrl;
use ws;
@ -540,12 +541,16 @@ impl<S: 'static> TestRequest<S> {
} = self;
let (router, _) = Router::new::<S>("/", Vec::new());
let mut req = Request::new(ServerSettings::default());
req.inner.method = method;
req.inner.url = InnerUrl::new(uri);
req.inner.version = version;
req.inner.headers = headers;
*req.inner.payload.borrow_mut() = payload;
let pool = RequestPool::pool(ServerSettings::default());
let mut req = RequestPool::get(pool);
{
let inner = req.inner_mut();
inner.method = method;
inner.url = InnerUrl::new(uri);
inner.version = version;
inner.headers = headers;
*inner.payload.borrow_mut() = payload;
}
let mut req =
HttpRequest::new(req, Rc::new(state), router.route_info_params(params));
@ -567,12 +572,16 @@ impl<S: 'static> TestRequest<S> {
payload,
} = self;
let mut req = Request::new(ServerSettings::default());
req.inner.method = method;
req.inner.url = InnerUrl::new(uri);
req.inner.version = version;
req.inner.headers = headers;
*req.inner.payload.borrow_mut() = payload;
let pool = RequestPool::pool(ServerSettings::default());
let mut req = RequestPool::get(pool);
{
let inner = req.inner_mut();
inner.method = method;
inner.url = InnerUrl::new(uri);
inner.version = version;
inner.headers = headers;
*inner.payload.borrow_mut() = payload;
}
let mut req =
HttpRequest::new(req, Rc::new(state), router.route_info_params(params));
req.set_cookies(cookies);
@ -589,12 +598,17 @@ impl<S: 'static> TestRequest<S> {
payload,
..
} = self;
let mut req = Request::new(ServerSettings::default());
req.inner.method = method;
req.inner.url = InnerUrl::new(uri);
req.inner.version = version;
req.inner.headers = headers;
*req.inner.payload.borrow_mut() = payload;
let pool = RequestPool::pool(ServerSettings::default());
let mut req = RequestPool::get(pool);
{
let inner = req.inner_mut();
inner.method = method;
inner.url = InnerUrl::new(uri);
inner.version = version;
inner.headers = headers;
*inner.payload.borrow_mut() = payload;
}
req
}