feat(router): Add Smart Routing to route payments efficiently (#2665)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: shashank_attarde <shashank.attarde@juspay.in>
Co-authored-by: Aprabhat19 <amishaprabhat@gmail.com>
Co-authored-by: Amisha Prabhat <55580080+Aprabhat19@users.noreply.github.com>
This commit is contained in:
Prajjwal Kumar
2023-11-03 18:37:31 +05:30
committed by GitHub
parent 6c5de9cee4
commit 9b618d2447
96 changed files with 15376 additions and 233 deletions

View File

@ -28,6 +28,7 @@ rand = "0.8.5"
regex = "1.8.4"
reqwest = { version = "0.11.18", features = ["json", "native-tls", "gzip", "multipart"] }
ring = { version = "0.16.20", features = ["std"] }
rustc-hash = "1.1.0"
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
serde_urlencoded = "0.7.1"

View File

@ -13,6 +13,8 @@ pub mod pii;
pub mod request;
#[cfg(feature = "signals")]
pub mod signals;
#[allow(missing_docs)] // Todo: add docs
pub mod static_cache;
pub mod types;
pub mod validation;

View File

@ -0,0 +1,91 @@
use std::sync::{Arc, RwLock};
use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;
#[derive(Debug)]
pub struct CacheEntry<T> {
data: Arc<T>,
timestamp: i64,
}
#[derive(Debug, Clone, thiserror::Error)]
pub enum CacheError {
#[error("Could not acquire the lock for cache entry")]
CouldNotAcquireLock,
#[error("Entry not found in cache")]
EntryNotFound,
}
#[derive(Debug)]
pub struct StaticCache<T> {
data: Lazy<RwLock<FxHashMap<String, CacheEntry<T>>>>,
}
impl<T> StaticCache<T>
where
T: Send,
{
pub const fn new() -> Self {
Self {
data: Lazy::new(|| RwLock::new(FxHashMap::default())),
}
}
pub fn present(&self, key: &String) -> Result<bool, CacheError> {
let the_map = self
.data
.read()
.map_err(|_| CacheError::CouldNotAcquireLock)?;
Ok(the_map.get(key).is_some())
}
pub fn expired(&self, key: &String, timestamp: i64) -> Result<bool, CacheError> {
let the_map = self
.data
.read()
.map_err(|_| CacheError::CouldNotAcquireLock)?;
Ok(match the_map.get(key) {
None => false,
Some(entry) => timestamp > entry.timestamp,
})
}
pub fn retrieve(&self, key: &String) -> Result<Arc<T>, CacheError> {
let the_map = self
.data
.read()
.map_err(|_| CacheError::CouldNotAcquireLock)?;
let cache_entry = the_map.get(key).ok_or(CacheError::EntryNotFound)?;
Ok(Arc::clone(&cache_entry.data))
}
pub fn save(&self, key: String, data: T, timestamp: i64) -> Result<(), CacheError> {
let mut the_map = self
.data
.write()
.map_err(|_| CacheError::CouldNotAcquireLock)?;
let entry = CacheEntry {
data: Arc::new(data),
timestamp,
};
the_map.insert(key, entry);
Ok(())
}
pub fn clear(&self) -> Result<(), CacheError> {
let mut the_map = self
.data
.write()
.map_err(|_| CacheError::CouldNotAcquireLock)?;
the_map.clear();
Ok(())
}
}