mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 16:51:03 +00:00
Alert on frequent network errors (#7410)
* Introduce is_frequent util * Add dirty warn_if_frequent! implementation * Add freq * Fix order in condition * Update * Update docs * Fix * Remove old impl * Fix errors * Add wif to av-distr * Add wif to col prot * Rename * Add wif to state-distr * Address review comments * Change Freq implementation * Remove the zero division check * Make rate explicit * Fix typo * Update rate constant * Introduce explicit rates * Update docs * Split errors freq * Downgrade coarsetime
This commit is contained in:
@@ -43,6 +43,27 @@ pub fn warn(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
gum(item, Level::Warn)
|
||||
}
|
||||
|
||||
/// Print a warning or debug level message depending on their frequency
|
||||
#[proc_macro]
|
||||
pub fn warn_if_frequent(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let ArgsIfFrequent { freq, max_rate, rest } = parse2(item.into()).unwrap();
|
||||
|
||||
let freq_expr = freq.expr;
|
||||
let max_rate_expr = max_rate.expr;
|
||||
let debug: proc_macro2::TokenStream = gum(rest.clone().into(), Level::Debug).into();
|
||||
let warn: proc_macro2::TokenStream = gum(rest.into(), Level::Warn).into();
|
||||
|
||||
let stream = quote! {
|
||||
if #freq_expr .is_frequent(#max_rate_expr) {
|
||||
#warn
|
||||
} else {
|
||||
#debug
|
||||
}
|
||||
};
|
||||
|
||||
stream.into()
|
||||
}
|
||||
|
||||
/// Print a info level message.
|
||||
#[proc_macro]
|
||||
pub fn info(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
|
||||
@@ -23,6 +23,8 @@ use syn::{
|
||||
|
||||
pub(crate) mod kw {
|
||||
syn::custom_keyword!(target);
|
||||
syn::custom_keyword!(freq);
|
||||
syn::custom_keyword!(max_rate);
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -248,6 +250,50 @@ impl ToTokens for FmtGroup {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(crate) struct Freq {
|
||||
kw: kw::freq,
|
||||
colon: Token![:],
|
||||
pub expr: syn::Expr,
|
||||
}
|
||||
|
||||
impl Parse for Freq {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
Ok(Self { kw: input.parse()?, colon: input.parse()?, expr: input.parse()? })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(crate) struct MaxRate {
|
||||
kw: kw::max_rate,
|
||||
colon: Token![:],
|
||||
pub expr: syn::Expr,
|
||||
}
|
||||
|
||||
impl Parse for MaxRate {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
Ok(Self { kw: input.parse()?, colon: input.parse()?, expr: input.parse()? })
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct ArgsIfFrequent {
|
||||
pub freq: Freq,
|
||||
pub max_rate: MaxRate,
|
||||
pub rest: TokenStream,
|
||||
}
|
||||
|
||||
impl Parse for ArgsIfFrequent {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
let freq = input.parse()?;
|
||||
let _: Token![,] = input.parse()?;
|
||||
let max_rate = input.parse()?;
|
||||
let _: Token![,] = input.parse()?;
|
||||
let rest = input.parse()?;
|
||||
|
||||
Ok(Self { freq, max_rate, rest })
|
||||
}
|
||||
}
|
||||
|
||||
/// Full set of arguments as provided to the `gum::warn!` call.
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct Args {
|
||||
|
||||
Reference in New Issue
Block a user