Skip to content

Commit

Permalink
feat: make MethodWeights cheap clone (AcalaNetwork#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
yjhmelody authored Jun 24, 2024
1 parent 76f03b9 commit eb1c425
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 eb1c425

Please sign in to comment.