Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make MethodWeights cheap clone #193

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
}
Loading