Skip to content

Commit

Permalink
fix: update code with suggestions from peer review
Browse files Browse the repository at this point in the history
  • Loading branch information
saibatizoku committed Jan 3, 2024
1 parent c946d75 commit d195378
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion catalyst-gateway/bin/src/service/api/health/ready_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) async fn endpoint(state: Data<&Arc<State>>) -> AllResponses {
Err(Error::EventDb(DBError::MismatchedSchema { was, expected })) => {
tracing::error!(
expected = expected,
was = was,
current = was,
"DB schema version status mismatch"
);
state.set_schema_version_status(SchemaVersionStatus::Mismatch);
Expand Down
2 changes: 1 addition & 1 deletion catalyst-gateway/bin/src/service/api/registration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl RegistrationApi {
500: ServerError,
503: ServiceUnavailable,
} {
if let Some(event_db) = pool.event_db() {
if let Ok(event_db) = pool.event_db() {
let voter = event_db
.get_voter(
&event_id.0.map(Into::into),
Expand Down
3 changes: 3 additions & 0 deletions catalyst-gateway/bin/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub(crate) enum Error {
/// An IO error has occurred
#[error(transparent)]
Io(#[from] std::io::Error),
/// A mismatch in the expected EventDB schema version
#[error("expected schema version mismatch")]
SchemaVersionMismatch,
}

/// # Run Catalyst Gateway Service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ impl<E: Endpoint> Endpoint for SchemaVersionValidationImpl<E> {
type Output = E::Output;

async fn call(&self, req: Request) -> Result<Self::Output> {
let req_data: Option<&Data<&Arc<State>>> = req.data();
if let Some(state) = req_data {
if let Some(state) = req.data::<Data<&Arc<State>>>() {
// Check if the inner schema version status is set to `Mismatch`,
// if so, return the `ServiceUnavailable` error, which implements
// `ResponseError`, with status code `503`.
// Otherwise, return the endpoint as usual.
if state.is_schema_version_status(&SchemaVersionStatus::Mismatch) {
return Err(ServiceUnavailable.into());
}
}
// Calls the endpoint with the request, and returns the response.
self.ep.call(req).await
}
}
Expand Down
15 changes: 8 additions & 7 deletions catalyst-gateway/bin/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::{Arc, Mutex, MutexGuard};
use crate::{
cli::Error,
event_db::{establish_connection, queries::EventDbQueries},
service::Error as ServiceError,
};

/// The status of the expected DB schema version.
Expand Down Expand Up @@ -51,11 +52,11 @@ impl State {
}

/// Get the reference to the database connection pool for `EventDB`.
pub(crate) fn event_db(&self) -> Option<Arc<dyn EventDbQueries>> {
let guard = self._schema_version_status_lock();
pub(crate) fn event_db(&self) -> Result<Arc<dyn EventDbQueries>, Error> {
let guard = self.schema_version_status_lock();
match *guard {
SchemaVersionStatus::Ok => Some(self.event_db.clone()),
SchemaVersionStatus::Mismatch => None,
SchemaVersionStatus::Ok => Ok(self.event_db.clone()),
SchemaVersionStatus::Mismatch => Err(ServiceError::SchemaVersionMismatch.into()),
}
}

Expand All @@ -67,13 +68,13 @@ impl State {
/// Compare the `State`'s inner value with a given `&SchemaVersionStatus`, returns
/// `bool`.
pub(crate) fn is_schema_version_status(&self, svs: &SchemaVersionStatus) -> bool {
let guard = self._schema_version_status_lock();
let guard = self.schema_version_status_lock();
&*guard == svs
}

/// Set the state's `SchemaVersionStatus`.
pub(crate) fn set_schema_version_status(&self, svs: SchemaVersionStatus) {
let mut guard = self._schema_version_status_lock();
let mut guard = self.schema_version_status_lock();
tracing::debug!(
status = format!("{:?}", svs),
"db schema version status was set"
Expand All @@ -84,7 +85,7 @@ impl State {
/// Get the `MutexGuard<SchemaVersionStatus>` from inner the variable.
///
/// Handle poisoned mutex by recovering the guard, and tracing the error.
fn _schema_version_status_lock(&self) -> MutexGuard<SchemaVersionStatus> {
fn schema_version_status_lock(&self) -> MutexGuard<SchemaVersionStatus> {
match self.schema_version_status.lock() {
Ok(guard) => guard,
Err(poisoned) => {
Expand Down

0 comments on commit d195378

Please sign in to comment.