spawn less

This commit is contained in:
Bryan Stitt 2022-12-27 19:43:02 -08:00
parent 6a201e1e47
commit 15c5ebf3bc
2 changed files with 26 additions and 29 deletions

@ -331,12 +331,15 @@ pub async fn ip_is_authorized(
{ {
RateLimitResult::Allowed(authorization, semaphore) => (authorization, semaphore), RateLimitResult::Allowed(authorization, semaphore) => (authorization, semaphore),
RateLimitResult::RateLimited(authorization, retry_at) => { 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)); return Err(FrontendErrorResponse::RateLimited(authorization, retry_at));
} }
// TODO: don't panic. give the user an error // TODO: don't panic. give the user an error
x => unimplemented!("rate_limit_by_ip shouldn't ever see these: {:?}", x), 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)) Ok((authorization, semaphore))
} }

@ -22,7 +22,7 @@ pub async fn proxy_web3_rpc(
origin: Option<TypedHeader<Origin>>, origin: Option<TypedHeader<Origin>>,
Json(payload): Json<JsonRpcRequestEnum>, Json(payload): Json<JsonRpcRequestEnum>,
) -> FrontendResult { ) -> FrontendResult {
let f = tokio::spawn(async move { // TODO: benchmark spawning this
// TODO: do we care about keeping the TypedHeader wrapper? // TODO: do we care about keeping the TypedHeader wrapper?
let origin = origin.map(|x| x.0); let origin = origin.map(|x| x.0);
@ -30,17 +30,15 @@ pub async fn proxy_web3_rpc(
let authorization = Arc::new(authorization); let authorization = Arc::new(authorization);
app.proxy_web3_rpc(authorization, payload) let (response, rpcs, _semaphore) = app.proxy_web3_rpc(authorization, payload)
.await .await
.map(|(x, y)| (x, y, semaphore)) .map(|(x, y)| (x, y, semaphore))?;
});
let (response, rpcs, _semaphore) = f.await??;
let mut response = Json(&response).into_response(); let mut response = Json(&response).into_response();
let headers = response.headers_mut(); let headers = response.headers_mut();
// TODO: this might be slow. think about this more
// TODO: special string if no rpcs were used (cache hit)? // TODO: special string if no rpcs were used (cache hit)?
let rpcs: String = rpcs.into_iter().map(|x| x.name.clone()).join(","); let rpcs: String = rpcs.into_iter().map(|x| x.name.clone()).join(",");
@ -68,7 +66,6 @@ pub async fn proxy_web3_rpc_with_key(
) -> FrontendResult { ) -> FrontendResult {
// TODO: DRY w/ proxy_web3_rpc // TODO: DRY w/ proxy_web3_rpc
// the request can take a while, so we spawn so that we can start serving another request // 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( let (authorization, semaphore) = key_is_authorized(
@ -83,12 +80,9 @@ pub async fn proxy_web3_rpc_with_key(
let authorization = Arc::new(authorization); let authorization = Arc::new(authorization);
app.proxy_web3_rpc(authorization, payload) let (response, rpcs, _semaphore) = app.proxy_web3_rpc(authorization, payload)
.await .await
.map(|(x, y)| (x, y, semaphore)) .map(|(x, y)| (x, y, semaphore))?;
});
let (response, rpcs, _semaphore) = f.await??;
let mut response = Json(&response).into_response(); let mut response = Json(&response).into_response();