From 15c5ebf3bc59ae841398eaef65b2b6987e916a52 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Tue, 27 Dec 2022 19:43:02 -0800 Subject: [PATCH] spawn less --- web3_proxy/src/frontend/authorization.rs | 3 ++ web3_proxy/src/frontend/rpc_proxy_http.rs | 52 ++++++++++------------- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/web3_proxy/src/frontend/authorization.rs b/web3_proxy/src/frontend/authorization.rs index 22f1c9dc..b05ec64e 100644 --- a/web3_proxy/src/frontend/authorization.rs +++ b/web3_proxy/src/frontend/authorization.rs @@ -331,12 +331,15 @@ pub async fn ip_is_authorized( { RateLimitResult::Allowed(authorization, semaphore) => (authorization, semaphore), RateLimitResult::RateLimited(authorization, retry_at) => { + // TODO: in the background, emit a stat (maybe simplest to use a channel?) return Err(FrontendErrorResponse::RateLimited(authorization, retry_at)); } // TODO: don't panic. give the user an error x => unimplemented!("rate_limit_by_ip shouldn't ever see these: {:?}", x), }; + // TODO: in the background, add the ip to a recent_users map + Ok((authorization, semaphore)) } diff --git a/web3_proxy/src/frontend/rpc_proxy_http.rs b/web3_proxy/src/frontend/rpc_proxy_http.rs index ba224827..cf590fe1 100644 --- a/web3_proxy/src/frontend/rpc_proxy_http.rs +++ b/web3_proxy/src/frontend/rpc_proxy_http.rs @@ -22,25 +22,23 @@ pub async fn proxy_web3_rpc( origin: Option>, Json(payload): Json, ) -> FrontendResult { - let f = tokio::spawn(async move { - // TODO: do we care about keeping the TypedHeader wrapper? - let origin = origin.map(|x| x.0); + // TODO: benchmark spawning this + // TODO: do we care about keeping the TypedHeader wrapper? + let origin = origin.map(|x| x.0); - let (authorization, semaphore) = ip_is_authorized(&app, ip, origin).await?; + let (authorization, semaphore) = ip_is_authorized(&app, ip, origin).await?; - let authorization = Arc::new(authorization); + let authorization = Arc::new(authorization); - app.proxy_web3_rpc(authorization, payload) - .await - .map(|(x, y)| (x, y, semaphore)) - }); - - let (response, rpcs, _semaphore) = f.await??; + let (response, rpcs, _semaphore) = app.proxy_web3_rpc(authorization, payload) + .await + .map(|(x, y)| (x, y, semaphore))?; let mut response = Json(&response).into_response(); let headers = response.headers_mut(); + // TODO: this might be slow. think about this more // TODO: special string if no rpcs were used (cache hit)? let rpcs: String = rpcs.into_iter().map(|x| x.name.clone()).join(","); @@ -68,27 +66,23 @@ pub async fn proxy_web3_rpc_with_key( ) -> FrontendResult { // TODO: DRY w/ proxy_web3_rpc // the request can take a while, so we spawn so that we can start serving another request - let f = tokio::spawn(async move { - let rpc_key = rpc_key.parse()?; + let rpc_key = rpc_key.parse()?; - let (authorization, semaphore) = key_is_authorized( - &app, - rpc_key, - ip, - origin.map(|x| x.0), - referer.map(|x| x.0), - user_agent.map(|x| x.0), - ) - .await?; + let (authorization, semaphore) = key_is_authorized( + &app, + rpc_key, + ip, + origin.map(|x| x.0), + referer.map(|x| x.0), + user_agent.map(|x| x.0), + ) + .await?; - let authorization = Arc::new(authorization); + let authorization = Arc::new(authorization); - app.proxy_web3_rpc(authorization, payload) - .await - .map(|(x, y)| (x, y, semaphore)) - }); - - let (response, rpcs, _semaphore) = f.await??; + let (response, rpcs, _semaphore) = app.proxy_web3_rpc(authorization, payload) + .await + .map(|(x, y)| (x, y, semaphore))?; let mut response = Json(&response).into_response();