dedicated error for lagged servers

This commit is contained in:
Bryan Stitt 2023-10-23 18:46:53 -07:00
parent 85928b299d
commit 6cb2accd0d
2 changed files with 22 additions and 2 deletions

View File

@ -3,6 +3,8 @@
use crate::frontend::authorization::Authorization;
use crate::jsonrpc::{self, JsonRpcErrorData, ParsedResponse, StreamResponse};
use crate::response_cache::ForwardedResponse;
use crate::rpcs::blockchain::Web3ProxyBlock;
use crate::rpcs::one::Web3Rpc;
use crate::rpcs::provider::EthersHttpProvider;
use axum::extract::rejection::JsonRejection;
use axum::extract::ws::Message;
@ -135,6 +137,10 @@ pub enum Web3ProxyError {
#[from(ignore)]
MethodNotFound(Cow<'static, str>),
NoVolatileRedisDatabase,
#[error(ignore)]
#[from(ignore)]
#[display(fmt = "{} @ {}", _0, _1)]
OldHead(Arc<Web3Rpc>, Web3ProxyBlock),
OriginRequired,
#[error(ignore)]
#[from(ignore)]
@ -820,6 +826,20 @@ impl Web3ProxyError {
},
)
}
Self::OldHead(rpc, old_head) => {
warn!(?old_head, "{} is lagged", rpc);
(
StatusCode::BAD_GATEWAY,
JsonRpcErrorData {
message: "RPC is lagged".into(),
code: StatusCode::BAD_REQUEST.as_u16().into(),
data: Some(json!({
"rpc": rpc.name,
"head": old_head,
})),
},
)
}
Self::OriginRequired => {
trace!("OriginRequired");
(

View File

@ -642,9 +642,9 @@ impl Web3Rpc {
let head_block = self.head_block_sender.as_ref().unwrap().borrow().clone();
if let Some(head_block) = head_block {
// TODO: if head block is very old and not expected to be syncing, emit warning
if head_block.age() > self.max_head_block_age {
return Err(anyhow::anyhow!("head_block is too old!").into());
// TODO: if the server is expected to be syncing, make a way to quiet this error
return Err(Web3ProxyError::OldHead(self.clone(), head_block));
}
if detailed_healthcheck {