You've already forked AstralRinth
forked from didirus/AstralRinth
Rustic cleanups, dedups and making the code less hard to read in general (#251)
* typos :help_me: * (part 1/?) massive cleanup to make the code more Rust-ic and cut down heap allocations. * (part 2/?) massive cleanup to make the code more Rust-ic and cut down heap allocations. * (part 3/?) cut down some pretty major heap allocations here - more Bytes and BytesMuts, less Vec<u8>s also I don't really understand why you need to `to_vec` when you don't really use it again afterwards * (part 4/?) deduplicate error handling in backblaze logic * (part 5/?) fixes, cleanups, refactors, and reformatting * (part 6/?) cleanups and refactors * remove loads of `as_str` in types that already are `Display` * Revert "remove loads of `as_str` in types that already are `Display`" This reverts commit 4f974310cfb167ceba03001d81388db4f0fbb509. * reformat and move routes util to the util module * use streams * Run prepare + formatting issues Co-authored-by: Jai A <jaiagr+gpg@pm.me> Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
pub mod scheduler;
|
||||
pub mod pod;
|
||||
pub mod scheduler;
|
||||
pub mod status;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
|
||||
lazy_static!{
|
||||
pub static ref SEARCH_READY: AtomicBool = AtomicBool::new(false);
|
||||
}
|
||||
lazy_static! {
|
||||
pub static ref SEARCH_READY: AtomicBool = AtomicBool::new(false);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::sync::{RwLock, Arc};
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PodInfo {
|
||||
@@ -12,7 +12,7 @@ impl PodInfo {
|
||||
Self {
|
||||
pod_name: dotenv::var("POD_NAME").unwrap_or("DEV".to_string()),
|
||||
node_name: dotenv::var("NODE_NAME").unwrap_or("self-hosted".to_string()),
|
||||
pod_id: Arc::new(RwLock::new(None))
|
||||
pod_id: Arc::new(RwLock::new(None)),
|
||||
}
|
||||
}
|
||||
pub fn get_id(&self) -> String {
|
||||
@@ -30,4 +30,4 @@ impl PodInfo {
|
||||
fn generate_id(&self) -> String {
|
||||
base64::encode(format!("{}-{}", self.node_name, self.pod_name))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
use crate::scheduler::Scheduler;
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
use prometheus::{opts, IntGaugeVec};
|
||||
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
|
||||
use actix_web::Error;
|
||||
use prometheus::{opts, IntGaugeVec};
|
||||
|
||||
use std::pin::Pin;
|
||||
use std::future::{Future};
|
||||
use std::task::{Context, Poll};
|
||||
use futures::future::{ok, Ready};
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
use crate::health::pod::PodInfo;
|
||||
use actix_web::http::{HeaderName, HeaderValue};
|
||||
use actix_web_prom::{PrometheusMetrics};
|
||||
use actix_web_prom::PrometheusMetrics;
|
||||
|
||||
pub struct HealthCounters {
|
||||
pod: PodInfo,
|
||||
@@ -35,13 +35,16 @@ impl HealthCounters {
|
||||
pub fn register(&self, builder: &mut PrometheusMetrics) {
|
||||
builder
|
||||
.registry
|
||||
.register(Box::new(self.opened_db_conn.clone())).unwrap();
|
||||
.register(Box::new(self.opened_db_conn.clone()))
|
||||
.unwrap();
|
||||
builder
|
||||
.registry
|
||||
.register(Box::new(self.idle_db_conn.clone())).unwrap();
|
||||
.register(Box::new(self.idle_db_conn.clone()))
|
||||
.unwrap();
|
||||
builder
|
||||
.registry
|
||||
.register(Box::new(self.current_requests.clone())).unwrap();
|
||||
.register(Box::new(self.current_requests.clone()))
|
||||
.unwrap();
|
||||
}
|
||||
pub fn schedule(&self, pool: Pool<Postgres>, scheduler: &mut Scheduler) {
|
||||
let this = self.clone();
|
||||
@@ -69,10 +72,10 @@ impl Clone for HealthCounters {
|
||||
}
|
||||
|
||||
impl<S, B> Transform<S> for HealthCounters
|
||||
where
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S::Future: 'static,
|
||||
B: 'static,
|
||||
where
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S::Future: 'static,
|
||||
B: 'static,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<B>;
|
||||
@@ -82,23 +85,23 @@ impl<S, B> Transform<S> for HealthCounters
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ok(MonitoringMiddleware { service, counters: self.clone() })
|
||||
ok(MonitoringMiddleware {
|
||||
service,
|
||||
counters: self.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
pub struct MonitoringMiddleware<S> {
|
||||
service: S,
|
||||
counters: HealthCounters,
|
||||
}
|
||||
|
||||
impl<S, B> Service for MonitoringMiddleware<S>
|
||||
where
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S::Future: 'static,
|
||||
B: 'static,
|
||||
where
|
||||
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S::Future: 'static,
|
||||
B: 'static,
|
||||
{
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceResponse<B>;
|
||||
@@ -112,7 +115,10 @@ impl<S, B> Service for MonitoringMiddleware<S>
|
||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
||||
// The request has started.
|
||||
let pattern_or_path = req.match_pattern().unwrap_or("unknown".to_string());
|
||||
let counter = self.counters.current_requests.with_label_values(&[&*pattern_or_path,req.method().as_str()]);
|
||||
let counter = self
|
||||
.counters
|
||||
.current_requests
|
||||
.with_label_values(&[&*pattern_or_path, req.method().as_str()]);
|
||||
counter.inc();
|
||||
let pod = self.counters.pod.clone();
|
||||
let fut = self.service.call(req);
|
||||
@@ -120,8 +126,11 @@ impl<S, B> Service for MonitoringMiddleware<S>
|
||||
let mut res: Self::Response = fut.await?;
|
||||
// The request finished, remove a counter
|
||||
counter.dec();
|
||||
res.headers_mut().insert(HeaderName::from_static("x-server"), HeaderValue::from_str(&*pod.get_id()).unwrap());
|
||||
res.headers_mut().insert(
|
||||
HeaderName::from_static("x-server"),
|
||||
HeaderValue::from_str(&*pod.get_id()).unwrap(),
|
||||
);
|
||||
Ok(res)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
use sqlx::{PgPool};
|
||||
use actix_web::web;
|
||||
use sqlx::PgPool;
|
||||
|
||||
pub async fn test_database(postgres: web::Data<PgPool>) -> Result<(), sqlx::Error> {
|
||||
let mut transaction = postgres.acquire().await?;
|
||||
let result = sqlx::query(
|
||||
"
|
||||
SELECT 1
|
||||
"
|
||||
).execute(&mut transaction)
|
||||
.await;
|
||||
match result {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => Err(e)
|
||||
}
|
||||
}
|
||||
sqlx::query(
|
||||
"
|
||||
SELECT 1
|
||||
",
|
||||
)
|
||||
.execute(&mut transaction)
|
||||
.await
|
||||
.map(|_| ())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user