From 49f30b0c036adacda38bae0a0a21787d3fcf443a Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Mon, 24 Jun 2024 16:48:31 +0800 Subject: [PATCH] feat: make `MethodWeights` cheap clone --- src/extensions/rate_limit/weight.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/extensions/rate_limit/weight.rs b/src/extensions/rate_limit/weight.rs index bf7d33c..06fedd0 100644 --- a/src/extensions/rate_limit/weight.rs +++ b/src/extensions/rate_limit/weight.rs @@ -1,14 +1,11 @@ use crate::config::RpcMethod; use std::collections::BTreeMap; +use std::sync::Arc; #[derive(Clone, Debug, Default)] -pub struct MethodWeights(BTreeMap); +pub struct MethodWeights(Arc>); 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) } @@ -16,10 +13,11 @@ impl MethodWeights { 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)) } }