Skip to content

Commit

Permalink
feat: make MethodWeights cheap clone (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
yjhmelody authored Jun 24, 2024
1 parent c93f208 commit ac19fef
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/extensions/rate_limit/weight.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
use crate::config::RpcMethod;
use std::collections::BTreeMap;
use std::sync::Arc;

#[derive(Clone, Debug, Default)]
pub struct MethodWeights(BTreeMap<String, u32>);
pub struct MethodWeights(Arc<BTreeMap<String, u32>>);

impl MethodWeights {
pub fn add(&mut self, method: &str, weight: u32) {
self.0.insert(method.to_owned(), weight);
}

pub fn get(&self, method: &str) -> u32 {
self.0.get(method).cloned().unwrap_or(1)
}
}

impl MethodWeights {
pub fn from_config(methods: &[RpcMethod]) -> Self {
let mut weights = MethodWeights::default();
let mut weights = BTreeMap::default();
for method in methods {
weights.add(&method.method, method.rate_limit_weight);
weights.insert(method.method.to_owned(), method.rate_limit_weight);
}
weights

Self(Arc::new(weights))
}
}

0 comments on commit ac19fef

Please sign in to comment.