forked from AcalaNetwork/subway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make
MethodWeights
cheap clone (AcalaNetwork#16)
- Loading branch information
Showing
1 changed file
with
6 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |