mirror of
https://github.com/denoland/deno.git
synced 2024-11-26 16:09:27 -05:00
perf(ext/http): simplify op_http_accept (#15067)
This commit is contained in:
parent
0bcc1175be
commit
34301e5a47
1 changed files with 31 additions and 43 deletions
|
@ -164,7 +164,7 @@ impl HttpConnResource {
|
||||||
// Accepts a new incoming HTTP request.
|
// Accepts a new incoming HTTP request.
|
||||||
async fn accept(
|
async fn accept(
|
||||||
self: &Rc<Self>,
|
self: &Rc<Self>,
|
||||||
) -> Result<Option<HttpStreamResource>, AnyError> {
|
) -> Result<Option<(HttpStreamResource, String, String)>, AnyError> {
|
||||||
let fut = async {
|
let fut = async {
|
||||||
let (request_tx, request_rx) = oneshot::channel();
|
let (request_tx, request_rx) = oneshot::channel();
|
||||||
let (response_tx, response_rx) = oneshot::channel();
|
let (response_tx, response_rx) = oneshot::channel();
|
||||||
|
@ -173,8 +173,24 @@ impl HttpConnResource {
|
||||||
self.acceptors_tx.unbounded_send(acceptor).ok()?;
|
self.acceptors_tx.unbounded_send(acceptor).ok()?;
|
||||||
|
|
||||||
let request = request_rx.await.ok()?;
|
let request = request_rx.await.ok()?;
|
||||||
let stream = HttpStreamResource::new(self, request, response_tx);
|
|
||||||
Some(stream)
|
let accept_encoding = {
|
||||||
|
let encodings = fly_accept_encoding::encodings_iter(request.headers())
|
||||||
|
.filter(|r| {
|
||||||
|
matches!(r, Ok((Some(Encoding::Brotli | Encoding::Gzip), _)))
|
||||||
|
});
|
||||||
|
|
||||||
|
fly_accept_encoding::preferred(encodings)
|
||||||
|
.ok()
|
||||||
|
.flatten()
|
||||||
|
.unwrap_or(Encoding::Identity)
|
||||||
|
};
|
||||||
|
|
||||||
|
let method = request.method().to_string();
|
||||||
|
let url = req_url(&request, self.scheme, &self.addr);
|
||||||
|
let stream =
|
||||||
|
HttpStreamResource::new(self, request, response_tx, accept_encoding);
|
||||||
|
Some((stream, method, url))
|
||||||
};
|
};
|
||||||
|
|
||||||
async {
|
async {
|
||||||
|
@ -192,14 +208,6 @@ impl HttpConnResource {
|
||||||
async fn closed(&self) -> Result<(), AnyError> {
|
async fn closed(&self) -> Result<(), AnyError> {
|
||||||
self.closed_fut.clone().map_err(AnyError::from).await
|
self.closed_fut.clone().map_err(AnyError::from).await
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scheme(&self) -> &'static str {
|
|
||||||
self.scheme
|
|
||||||
}
|
|
||||||
|
|
||||||
fn addr(&self) -> &HttpSocketAddr {
|
|
||||||
&self.addr
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Resource for HttpConnResource {
|
impl Resource for HttpConnResource {
|
||||||
|
@ -308,12 +316,13 @@ impl HttpStreamResource {
|
||||||
conn: &Rc<HttpConnResource>,
|
conn: &Rc<HttpConnResource>,
|
||||||
request: Request<Body>,
|
request: Request<Body>,
|
||||||
response_tx: oneshot::Sender<Response<Body>>,
|
response_tx: oneshot::Sender<Response<Body>>,
|
||||||
|
accept_encoding: Encoding,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
conn: conn.clone(),
|
conn: conn.clone(),
|
||||||
rd: HttpRequestReader::Headers(request).into(),
|
rd: HttpRequestReader::Headers(request).into(),
|
||||||
wr: HttpResponseWriter::Headers(response_tx).into(),
|
wr: HttpResponseWriter::Headers(response_tx).into(),
|
||||||
accept_encoding: RefCell::new(Encoding::Identity),
|
accept_encoding: RefCell::new(accept_encoding),
|
||||||
cancel_handle: CancelHandle::new(),
|
cancel_handle: CancelHandle::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -377,38 +386,17 @@ async fn op_http_accept(
|
||||||
) -> Result<Option<NextRequestResponse>, AnyError> {
|
) -> Result<Option<NextRequestResponse>, AnyError> {
|
||||||
let conn = state.borrow().resource_table.get::<HttpConnResource>(rid)?;
|
let conn = state.borrow().resource_table.get::<HttpConnResource>(rid)?;
|
||||||
|
|
||||||
let stream = match conn.accept().await {
|
match conn.accept().await {
|
||||||
Ok(Some(stream)) => Rc::new(stream),
|
Ok(Some((stream, method, url))) => {
|
||||||
Ok(None) => return Ok(None),
|
let stream_rid =
|
||||||
Err(err) => return Err(err),
|
state.borrow_mut().resource_table.add_rc(Rc::new(stream));
|
||||||
};
|
|
||||||
|
|
||||||
let rd = RcRef::map(&stream, |r| &r.rd).borrow().await;
|
|
||||||
let request = match &*rd {
|
|
||||||
HttpRequestReader::Headers(request) => request,
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
stream.accept_encoding.replace({
|
|
||||||
let encodings = fly_accept_encoding::encodings_iter(request.headers())
|
|
||||||
.filter(|r| {
|
|
||||||
matches!(r, Ok((Some(Encoding::Brotli | Encoding::Gzip), _)))
|
|
||||||
});
|
|
||||||
|
|
||||||
fly_accept_encoding::preferred(encodings)
|
|
||||||
.ok()
|
|
||||||
.flatten()
|
|
||||||
.unwrap_or(Encoding::Identity)
|
|
||||||
});
|
|
||||||
|
|
||||||
let method = request.method().to_string();
|
|
||||||
let url = req_url(request, conn.scheme(), conn.addr());
|
|
||||||
|
|
||||||
let stream_rid = state.borrow_mut().resource_table.add_rc(stream);
|
|
||||||
|
|
||||||
let r = NextRequestResponse(stream_rid, method, url);
|
let r = NextRequestResponse(stream_rid, method, url);
|
||||||
Ok(Some(r))
|
Ok(Some(r))
|
||||||
}
|
}
|
||||||
|
Ok(None) => Ok(None),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn req_url(
|
fn req_url(
|
||||||
req: &hyper::Request<hyper::Body>,
|
req: &hyper::Request<hyper::Body>,
|
||||||
|
|
Loading…
Reference in a new issue