-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
新控制台初步完成登录校验失败频率限制,默认支持1小时5次;issue #29
- Loading branch information
Showing
9 changed files
with
228 additions
and
36 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
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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::convert::TryFrom; | ||
|
||
use ratelimiter_rs::RateLimiter; | ||
|
||
pub struct LimiterData { | ||
pub rate_to_ms_conversion: i32, | ||
pub consumed_tokens: i32, | ||
pub last_refill_time: i64, | ||
} | ||
|
||
impl LimiterData { | ||
pub fn new(rate_to_ms_conversion: i32, consumed_tokens: i32, last_refill_time: i64) -> Self { | ||
Self { | ||
rate_to_ms_conversion, | ||
consumed_tokens, | ||
last_refill_time, | ||
} | ||
} | ||
pub fn to_rate_limiter(self) -> RateLimiter { | ||
RateLimiter::load( | ||
self.rate_to_ms_conversion, | ||
self.consumed_tokens, | ||
self.last_refill_time, | ||
) | ||
} | ||
} | ||
|
||
impl TryFrom<&str> for LimiterData { | ||
type Error = anyhow::Error; | ||
|
||
fn try_from(value: &str) -> Result<Self, Self::Error> { | ||
let mut iter = value.split(','); | ||
let a: i32 = if let Some(e) = iter.next() { | ||
e.parse()? | ||
} else { | ||
return Err(anyhow::anyhow!("limiter is unvalid")); | ||
}; | ||
let b: i32 = if let Some(e) = iter.next() { | ||
e.parse()? | ||
} else { | ||
return Err(anyhow::anyhow!("limiter is unvalid")); | ||
}; | ||
let c: i64 = if let Some(e) = iter.next() { | ||
e.parse()? | ||
} else { | ||
return Err(anyhow::anyhow!("limiter is unvalid")); | ||
}; | ||
Ok(Self::new(a, b, c)) | ||
} | ||
} | ||
|
||
impl ToString for LimiterData { | ||
fn to_string(&self) -> String { | ||
format!( | ||
"{},{},{}", | ||
self.rate_to_ms_conversion, self.consumed_tokens, self.last_refill_time | ||
) | ||
} | ||
} | ||
|
||
impl From<RateLimiter> for LimiterData { | ||
fn from(value: RateLimiter) -> Self { | ||
let (a, b, c) = value.get_to_save(); | ||
Self::new(a, b, c) | ||
} | ||
} |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::{ | ||
grpc::PayloadUtils, | ||
raft::{ | ||
cluster::{ | ||
model::{RouteAddr, RouterRequest, RouterResponse}, | ||
route::RaftAddrRouter, | ||
}, | ||
network::factory::RaftClusterRequestSender, | ||
}, | ||
}; | ||
|
||
use super::{CacheLimiterReq, CacheManager, CacheManagerResult}; | ||
use actix::prelude::*; | ||
|
||
pub struct CacheRoute { | ||
cache_manager: Addr<CacheManager>, | ||
raft_addr_route: Arc<RaftAddrRouter>, | ||
cluster_sender: Arc<RaftClusterRequestSender>, | ||
} | ||
|
||
impl CacheRoute { | ||
pub fn new( | ||
cache_manager: Addr<CacheManager>, | ||
raft_addr_route: Arc<RaftAddrRouter>, | ||
cluster_sender: Arc<RaftClusterRequestSender>, | ||
) -> Self { | ||
Self { | ||
cache_manager, | ||
raft_addr_route, | ||
cluster_sender, | ||
} | ||
} | ||
|
||
fn unknown_err(&self) -> anyhow::Error { | ||
anyhow::anyhow!("unknown the raft leader addr!") | ||
} | ||
|
||
pub async fn request_limiter( | ||
&self, | ||
req: CacheLimiterReq, | ||
) -> anyhow::Result<CacheManagerResult> { | ||
match self.raft_addr_route.get_route_addr().await? { | ||
RouteAddr::Local => self.cache_manager.send(req).await?, | ||
RouteAddr::Remote(_, addr) => { | ||
let req: RouterRequest = req.into(); | ||
let request = serde_json::to_string(&req).unwrap_or_default(); | ||
let payload = PayloadUtils::build_payload("RaftRouteRequest", request); | ||
let resp_payload = self.cluster_sender.send_request(addr, payload).await?; | ||
let body_vec = resp_payload.body.unwrap_or_default().value; | ||
let resp: RouterResponse = serde_json::from_slice(&body_vec)?; | ||
match resp { | ||
RouterResponse::CacheManagerResult { result } => Ok(result), | ||
_ => Err(anyhow::anyhow!("response type is error!")), | ||
} | ||
} | ||
RouteAddr::Unknown => Err(self.unknown_err()), | ||
} | ||
} | ||
} |
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
Oops, something went wrong.