cache api keys that are not in the database

This commit is contained in:
Bryan Stitt 2022-08-16 21:10:09 +00:00
parent 4c66bc32d7
commit a61af53117
4 changed files with 20 additions and 4 deletions

View File

@ -89,6 +89,7 @@
- [x] I'm hitting infura rate limits very quickly. I feel like that means something is very inefficient
- whenever blocks were slow, we started checking as fast as possible
- [x] create user script should allow setting requests per minute
- [x] cache api keys that are not in the database
- [-] basic request method stats (using the user_id and other fields that are in the tracing frame)
- [ ] use siwe messages and signatures for sign up and login
- [ ] "chain is forked" message is wrong. it includes nodes just being on different heights of the same chain. need a smarter check
@ -254,7 +255,6 @@ in another repo: event subscriber
eth_1 | 2022-08-10T23:26:10.195014Z WARN web3_proxy::connections: chain is forked! 262 possible heads. 1/2/5/5 rpcs have 0x0538…bfff
eth_1 | 2022-08-10T23:26:10.195658Z WARN web3_proxy::connections: chain is forked! 262 possible heads. 2/3/5/5 rpcs have 0x0538…bfff
- [ ] fix ip detection when running in dev
- [ ] cache api keys that are not in the database?
- [ ] double check weight sorting code
- [ ] sea-orm brings in async-std, but we are using tokio. benchmark switching
- [ ] this query always times out, but erigon can serve it quickly: `curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"debug_traceBlockByNumber","params":["latest"],"id":1}' 127.0.0.1:8544' 127.0.0.1:8544`

View File

@ -130,7 +130,12 @@ impl Web3ProxyApp {
))
}
None => {
return Err(anyhow::anyhow!("unknown api key"));
UserCacheValue::from((
// TODO: how long should this cache last? get this from config
Instant::now() + Duration::from_secs(60),
0,
0,
))
}
};
@ -147,6 +152,10 @@ impl Web3ProxyApp {
user_data.unwrap()
};
if user_data.user_id == 0 {
return Err(anyhow::anyhow!("unknown key!"));
}
// user key is valid. now check rate limits
if let Some(rate_limiter) = &self.rate_limiter {
if rate_limiter

View File

@ -10,7 +10,7 @@
use super::{errors::anyhow_error_into_response, rate_limit::RateLimitResult};
use crate::app::Web3ProxyApp;
use axum::{
response::{IntoResponse, Response},
response::{ErrorResponse, IntoResponse, Response},
Extension, Json,
};
use axum_client_ip::ClientIp;
@ -22,8 +22,11 @@ use sea_orm::ActiveModelTrait;
use serde::Deserialize;
use std::sync::Arc;
// TODO: how do we customize axum's error response? I think we probably want an enum that implements IntoResponse instead
#[debug_handler]
pub async fn get_login(Extension(app): Extension<Arc<Web3ProxyApp>>) -> Result<Response, Response> {
pub async fn get_login(
Extension(app): Extension<Arc<Web3ProxyApp>>,
) -> Result<Response, ErrorResponse> {
// let redis: RedisPool = app...;
let redis_pool = app.redis_pool.as_ref().unwrap();
@ -32,6 +35,7 @@ pub async fn get_login(Extension(app): Extension<Arc<Web3ProxyApp>>) -> Result<R
todo!("how should this work? probably keep stuff in redis ")
}
#[debug_handler]
pub async fn create_user(
// this argument tells axum to parse the request body
// as JSON into a `CreateUser` type

View File

@ -5,6 +5,7 @@ use axum::{
Extension,
};
use axum_client_ip::ClientIp;
use axum_macros::debug_handler;
use futures::SinkExt;
use futures::{
future::AbortHandle,
@ -26,6 +27,7 @@ use crate::{
use super::{errors::anyhow_error_into_response, rate_limit::RateLimitResult};
#[debug_handler]
pub async fn public_websocket_handler(
Extension(app): Extension<Arc<Web3ProxyApp>>,
ClientIp(ip): ClientIp,
@ -56,6 +58,7 @@ pub async fn public_websocket_handler(
}
}
#[debug_handler]
pub async fn user_websocket_handler(
Extension(app): Extension<Arc<Web3ProxyApp>>,
Path(user_key): Path<Uuid>,