Add dependencies to search (#578)

* Add dependencies to search

* add attrs for faceting

* run prepare

* Add user data route from token

* update to 24hrs

* Fix report bugs
This commit is contained in:
Geometrically
2023-04-20 16:38:30 -07:00
committed by GitHub
parent 5c559af936
commit 59f24df294
65 changed files with 1518 additions and 2218 deletions

View File

@@ -35,27 +35,18 @@ impl ResponseError for ARError {
reset,
} => {
let mut response = actix_web::HttpResponse::TooManyRequests();
response.insert_header((
"x-ratelimit-limit",
max_requests.to_string(),
));
response.insert_header((
"x-ratelimit-remaining",
remaining.to_string(),
));
response
.insert_header(("x-ratelimit-reset", reset.to_string()));
response.insert_header(("x-ratelimit-limit", max_requests.to_string()));
response.insert_header(("x-ratelimit-remaining", remaining.to_string()));
response.insert_header(("x-ratelimit-reset", reset.to_string()));
response.json(ApiError {
error: "ratelimit_error",
description: &self.to_string(),
})
}
_ => actix_web::HttpResponse::build(self.status_code()).json(
ApiError {
error: "ratelimit_error",
description: &self.to_string(),
},
),
_ => actix_web::HttpResponse::build(self.status_code()).json(ApiError {
error: "ratelimit_error",
description: &self.to_string(),
}),
}
}
}

View File

@@ -36,9 +36,9 @@ impl MemoryStore {
pub fn with_capacity(capacity: usize) -> Self {
debug!("Creating new MemoryStore");
MemoryStore {
inner: Arc::new(
DashMap::<String, (usize, Duration)>::with_capacity(capacity),
),
inner: Arc::new(DashMap::<String, (usize, Duration)>::with_capacity(
capacity,
)),
}
}
}
@@ -74,18 +74,10 @@ impl Supervised for MemoryStoreActor {
impl Handler<ActorMessage> for MemoryStoreActor {
type Result = ActorResponse;
fn handle(
&mut self,
msg: ActorMessage,
ctx: &mut Self::Context,
) -> Self::Result {
fn handle(&mut self, msg: ActorMessage, ctx: &mut Self::Context) -> Self::Result {
match msg {
ActorMessage::Set { key, value, expiry } => {
debug!(
"Inserting key {} with expiry {}",
&key,
&expiry.as_secs()
);
debug!("Inserting key {} with expiry {}", &key, &expiry.as_secs());
let future_key = String::from(&key);
let now = SystemTime::now();
let now = now.duration_since(UNIX_EPOCH).unwrap();
@@ -93,10 +85,7 @@ impl Handler<ActorMessage> for MemoryStoreActor {
ctx.notify_later(ActorMessage::Remove(future_key), expiry);
ActorResponse::Set(Box::pin(future::ready(Ok(()))))
}
ActorMessage::Update { key, value } => match self
.inner
.get_mut(&key)
{
ActorMessage::Update { key, value } => match self.inner.get_mut(&key) {
Some(mut c) => {
let val_mut: &mut (usize, Duration) = c.value_mut();
if val_mut.0 > value {
@@ -107,22 +96,18 @@ impl Handler<ActorMessage> for MemoryStoreActor {
let new_val = val_mut.0;
ActorResponse::Update(Box::pin(future::ready(Ok(new_val))))
}
None => ActorResponse::Update(Box::pin(future::ready(Err(
ARError::ReadWrite(
"memory store: read failed!".to_string(),
),
)))),
None => ActorResponse::Update(Box::pin(future::ready(Err(ARError::ReadWrite(
"memory store: read failed!".to_string(),
))))),
},
ActorMessage::Get(key) => {
if self.inner.contains_key(&key) {
let val = match self.inner.get(&key) {
Some(c) => c,
None => {
return ActorResponse::Get(Box::pin(future::ready(
Err(ARError::ReadWrite(
"memory store: read failed!".to_string(),
)),
)))
return ActorResponse::Get(Box::pin(future::ready(Err(
ARError::ReadWrite("memory store: read failed!".to_string()),
))))
}
};
let val = val.value().0;
@@ -135,17 +120,14 @@ impl Handler<ActorMessage> for MemoryStoreActor {
let c = match self.inner.get(&key) {
Some(d) => d,
None => {
return ActorResponse::Expire(Box::pin(future::ready(
Err(ARError::ReadWrite(
"memory store: read failed!".to_string(),
)),
)))
return ActorResponse::Expire(Box::pin(future::ready(Err(
ARError::ReadWrite("memory store: read failed!".to_string()),
))))
}
};
let dur = c.value().1;
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let res =
dur.checked_sub(now).unwrap_or_else(|| Duration::new(0, 0));
let res = dur.checked_sub(now).unwrap_or_else(|| Duration::new(0, 0));
ActorResponse::Expire(Box::pin(future::ready(Ok(res))))
}
ActorMessage::Remove(key) => {
@@ -153,11 +135,9 @@ impl Handler<ActorMessage> for MemoryStoreActor {
let val = match self.inner.remove::<String>(&key) {
Some(c) => c,
None => {
return ActorResponse::Remove(Box::pin(future::ready(
Err(ARError::ReadWrite(
"memory store: remove failed!".to_string(),
)),
)))
return ActorResponse::Remove(Box::pin(future::ready(Err(
ARError::ReadWrite("memory store: remove failed!".to_string()),
))))
}
};
let val = val.1;

View File

@@ -18,8 +18,7 @@ use std::{
time::Duration,
};
type RateLimiterIdentifier =
Rc<Box<dyn Fn(&ServiceRequest) -> Result<String, ARError> + 'static>>;
type RateLimiterIdentifier = Rc<Box<dyn Fn(&ServiceRequest) -> Result<String, ARError> + 'static>>;
pub struct RateLimiter<T>
where
@@ -42,8 +41,7 @@ where
pub fn new(store: Addr<T>) -> Self {
let identifier = |req: &ServiceRequest| {
let connection_info = req.connection_info();
let ip =
connection_info.peer_addr().ok_or(ARError::Identification)?;
let ip = connection_info.peer_addr().ok_or(ARError::Identification)?;
Ok(String::from(ip))
};
RateLimiter {
@@ -74,9 +72,7 @@ where
}
/// Function to get the identifier for the client request
pub fn with_identifier<
F: Fn(&ServiceRequest) -> Result<String, ARError> + 'static,
>(
pub fn with_identifier<F: Fn(&ServiceRequest) -> Result<String, ARError> + 'static>(
mut self,
identifier: F,
) -> Self {
@@ -89,8 +85,7 @@ impl<T, S, B> Transform<S, ServiceRequest> for RateLimiter<T>
where
T: Handler<ActorMessage> + Send + Sync + 'static,
T::Context: ToEnvelope<T, ActorMessage>,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = AWError>
+ 'static,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = AWError> + 'static,
S::Future: 'static,
B: 'static,
{
@@ -130,21 +125,16 @@ where
impl<T, S, B> Service<ServiceRequest> for RateLimitMiddleware<S, T>
where
T: Handler<ActorMessage> + 'static,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = AWError>
+ 'static,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = AWError> + 'static,
S::Future: 'static,
B: 'static,
T::Context: ToEnvelope<T, ActorMessage>,
{
type Response = ServiceResponse<B>;
type Error = S::Error;
type Future =
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
fn poll_ready(
&self,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.borrow_mut().poll_ready(cx)
}
@@ -178,15 +168,9 @@ where
if let Some(c) = opt {
// Existing entry in store
let expiry = store
.send(ActorMessage::Expire(String::from(
&identifier,
)))
.send(ActorMessage::Expire(String::from(&identifier)))
.await
.map_err(|_| {
ARError::ReadWrite(
"Setting timeout".to_string(),
)
})?;
.map_err(|_| ARError::ReadWrite("Setting timeout".to_string()))?;
let reset: Duration = match expiry {
ActorResponse::Expire(dur) => dur.await?,
_ => unreachable!(),
@@ -208,9 +192,7 @@ where
})
.await
.map_err(|_| {
ARError::ReadWrite(
"Decrementing ratelimit".to_string(),
)
ARError::ReadWrite("Decrementing ratelimit".to_string())
})?;
let updated_value: usize = match res {
ActorResponse::Update(c) => c.await?,
@@ -223,23 +205,15 @@ where
// Safe unwraps, since usize is always convertible to string
headers.insert(
HeaderName::from_static("x-ratelimit-limit"),
HeaderValue::from_str(
max_requests.to_string().as_str(),
)?,
HeaderValue::from_str(max_requests.to_string().as_str())?,
);
headers.insert(
HeaderName::from_static(
"x-ratelimit-remaining",
),
HeaderValue::from_str(
updated_value.to_string().as_str(),
)?,
HeaderName::from_static("x-ratelimit-remaining"),
HeaderValue::from_str(updated_value.to_string().as_str())?,
);
headers.insert(
HeaderName::from_static("x-ratelimit-reset"),
HeaderValue::from_str(
reset.as_secs().to_string().as_str(),
)?,
HeaderValue::from_str(reset.as_secs().to_string().as_str())?,
);
Ok(res)
}
@@ -253,11 +227,7 @@ where
expiry: interval,
})
.await
.map_err(|_| {
ARError::ReadWrite(
"Creating store entry".to_string(),
)
})?;
.map_err(|_| ARError::ReadWrite("Creating store entry".to_string()))?;
match res {
ActorResponse::Set(c) => c.await?,
_ => unreachable!(),
@@ -268,24 +238,15 @@ where
// Safe unwraps, since usize is always convertible to string
headers.insert(
HeaderName::from_static("x-ratelimit-limit"),
HeaderValue::from_str(
max_requests.to_string().as_str(),
)
.unwrap(),
HeaderValue::from_str(max_requests.to_string().as_str()).unwrap(),
);
headers.insert(
HeaderName::from_static("x-ratelimit-remaining"),
HeaderValue::from_str(
current_value.to_string().as_str(),
)
.unwrap(),
HeaderValue::from_str(current_value.to_string().as_str()).unwrap(),
);
headers.insert(
HeaderName::from_static("x-ratelimit-reset"),
HeaderValue::from_str(
interval.as_secs().to_string().as_str(),
)
.unwrap(),
HeaderValue::from_str(interval.as_secs().to_string().as_str()).unwrap(),
);
Ok(res)
}