Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

slightly improved http error status codes and db health check #154

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions kepler-core/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ where
}
}

impl<C, B, K> OrbitDatabase<C, B, K>
where
C: TransactionTrait,
{
pub async fn check_db_connection(&self) -> Result<(), DbErr> {
// there's a `ping` method on the connection, but we can't access it from here
// but starting a transaction should be enough to check the connection
self.conn.begin().await.map(|_| ())
}
}

pub type InvocationInputs<W> = HashMap<(OrbitId, String), (Metadata, HashBuffer<W>)>;

impl<C, B, K> OrbitDatabase<C, B, K>
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod tracing;
use config::{BlockStorage, Config, Keys, StagingStorage};
use kepler_core::{
keys::{SecretsSetup, StaticSecret},
sea_orm::{Database, DatabaseConnection},
sea_orm::{ConnectOptions, Database, DatabaseConnection},
storage::{either::Either, memory::MemoryStaging, StorageConfig},
OrbitDatabase,
};
Expand Down Expand Up @@ -86,8 +86,11 @@ pub async fn app(config: &Figment) -> Result<Rocket<Build>> {
Keys::Static(s) => s.try_into()?,
};

let mut connect_opts = ConnectOptions::from(&kepler_config.storage.database);
connect_opts.max_connections(100);

let kepler = Kepler::new(
Database::connect(&kepler_config.storage.database).await?,
Database::connect(connect_opts).await?,
kepler_config.storage.blocks.open().await?,
key_setup.setup(()).await?,
)
Expand Down
27 changes: 24 additions & 3 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,31 @@ use crate::{
BlockStage, BlockStores, Kepler,
};
use kepler_core::{
sea_orm::DbErr,
storage::{ImmutableReadStore, ImmutableStaging},
types::Resource,
util::{DelegationInfo, InvocationInfo},
TxError,
TxError, TxStoreError,
};

pub mod util;
use util::LimitedReader;

#[allow(clippy::let_unit_value)]
pub mod util_routes {
use super::*;

#[options("/<_s..>")]
pub async fn cors(_s: std::path::PathBuf) {}

#[get("/healthz")]
pub fn healthcheck() {}
pub async fn healthcheck(s: &State<Kepler>) -> Status {
if s.check_db_connection().await.is_ok() {
Status::Ok
} else {
Status::InternalServerError
}
}
}

#[get("/peer/generate/<orbit>")]
Expand Down Expand Up @@ -69,6 +78,7 @@ pub async fn delegate(
(
match e {
TxError::OrbitNotFound => Status::NotFound,
TxError::Db(DbErr::ConnectionAcquire) => Status::InternalServerError,
_ => Status::Unauthorized,
},
e.to_string(),
Expand Down Expand Up @@ -183,7 +193,18 @@ pub async fn invoke(
_ => unreachable!(),
},
)
.map_err(|e| (Status::Unauthorized, e.to_string()));
.map_err(|e| {
(
match e {
TxStoreError::Tx(TxError::OrbitNotFound) => Status::NotFound,
TxStoreError::Tx(TxError::Db(DbErr::ConnectionAcquire)) => {
Status::InternalServerError
}
_ => Status::Unauthorized,
},
e.to_string(),
)
});

timer.observe_duration();
res
Expand Down