Skip to content

Commit

Permalink
fix: re-use the same analysis service instance
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Jan 29, 2025
1 parent b5fca5d commit 1c03182
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
4 changes: 1 addition & 3 deletions modules/analysis/src/endpoints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ use trustify_common::{
};
use utoipa_actix_web::service_config::ServiceConfig;

pub fn configure(config: &mut ServiceConfig, db: Database) {
let analysis = AnalysisService::new();

pub fn configure(config: &mut ServiceConfig, db: Database, analysis: AnalysisService) {
config
.app_data(web::Data::new(analysis))
.app_data(web::Data::new(db))
Expand Down
18 changes: 16 additions & 2 deletions modules/analysis/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use trustify_common::{
use trustify_entity::{relationship::Relationship, sbom};
use uuid::Uuid;

#[derive(Clone, Default)]
#[derive(Clone)]
pub struct AnalysisService {
graph: Arc<RwLock<GraphMap>>,
}
Expand Down Expand Up @@ -142,8 +142,22 @@ pub fn ancestor_nodes(
}

impl AnalysisService {
/// Create a new analysis service instance.
///
/// ## Caching
///
/// A new instance will have a new cache. Instanced cloned from it, will share that cache.
///
/// Therefore, it is ok to create a new instance. However, if you want to make use of the
/// caching, it is necessary to re-use that instance.
///
/// Also, we do not implement default because of this. As a new instance has the implication
/// of having its own cache. So creating a new instance should be a deliberate choice.
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self::default()
Self {
graph: Default::default(),
}
}

#[instrument(skip_all, err)]
Expand Down
4 changes: 3 additions & 1 deletion modules/analysis/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::service::AnalysisService;
use crate::{
endpoints::configure,
model::{AncNode, AncestorSummary},
Expand All @@ -9,7 +10,8 @@ use trustify_test_context::{
};

pub async fn caller(ctx: &TrustifyContext) -> anyhow::Result<impl CallService + '_> {
call::caller(|svc| configure(svc, ctx.db.clone())).await
let analysis = AnalysisService::new();
call::caller(|svc| configure(svc, ctx.db.clone(), analysis)).await
}

#[derive(PartialEq, Eq, Debug, Copy, Clone)]
Expand Down
3 changes: 3 additions & 0 deletions server/src/openapi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::profile::api::{configure, default_openapi_info, Config, ModuleConfig};
use actix_web::App;
use trustify_common::{config::Database, db};
use trustify_module_analysis::service::AnalysisService;
use trustify_module_storage::service::{dispatch::DispatchBackend, fs::FileSystemBackend};
use utoipa::{
openapi::security::{OpenIdConnect, SecurityScheme},
Expand All @@ -11,6 +12,7 @@ use utoipa_actix_web::AppExt;
pub async fn create_openapi() -> anyhow::Result<utoipa::openapi::OpenApi> {
let (db, postgresql) = db::embedded::create().await?;
let (storage, _temp) = FileSystemBackend::for_test().await?;
let analysis = AnalysisService::new();

let (_, mut openapi) = App::new()
.into_utoipa_app()
Expand All @@ -22,6 +24,7 @@ pub async fn create_openapi() -> anyhow::Result<utoipa::openapi::OpenApi> {
db,
storage: storage.into(),
auth: None,
analysis,
with_graphql: true,
},
);
Expand Down
12 changes: 8 additions & 4 deletions server/src/profile/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ impl InitData {
let ui = Arc::new(UiResources::new(&self.ui)?);
let db = self.db.clone();
let storage = self.storage.clone();
let analysis = AnalysisService::new();

let http = {
HttpServerBuilder::try_from(self.http)?
Expand All @@ -323,6 +324,7 @@ impl InitData {
db: self.db.clone(),
storage: self.storage.clone(),
auth: self.authenticator.clone(),
analysis: analysis.clone(),

with_graphql: self.with_graphql,
},
Expand Down Expand Up @@ -369,6 +371,7 @@ pub(crate) struct Config {
pub(crate) config: ModuleConfig,
pub(crate) db: db::Database,
pub(crate) storage: DispatchBackend,
pub(crate) analysis: AnalysisService,
pub(crate) auth: Option<Arc<Authenticator>>,
pub(crate) with_graphql: bool,
}
Expand All @@ -382,6 +385,7 @@ pub(crate) fn configure(svc: &mut utoipa_actix_web::service_config::ServiceConfi
db,
storage,
auth,
analysis,

with_graphql,
} = config;
Expand Down Expand Up @@ -413,8 +417,6 @@ pub(crate) fn configure(svc: &mut utoipa_actix_web::service_config::ServiceConfi

// register REST API & UI

let analysis = AnalysisService::new();

svc.app_data(graph)
.configure(|svc| {
endpoints::configure(svc, auth.clone());
Expand All @@ -436,9 +438,9 @@ pub(crate) fn configure(svc: &mut utoipa_actix_web::service_config::ServiceConfi
fundamental,
db.clone(),
storage,
analysis,
analysis.clone(),
);
trustify_module_analysis::endpoints::configure(svc, db.clone());
trustify_module_analysis::endpoints::configure(svc, db.clone(), analysis);
trustify_module_user::endpoints::configure(svc, db.clone());
}),
);
Expand Down Expand Up @@ -497,6 +499,7 @@ mod test {
let db = ctx.db;
let (storage, _) = FileSystemBackend::for_test().await?;
let ui = Arc::new(UiResources::new(&UI::default())?);
let analysis = AnalysisService::new();
let app = actix_web::test::init_service(
App::new()
.into_utoipa_app()
Expand All @@ -509,6 +512,7 @@ mod test {
db,
storage: DispatchBackend::Filesystem(storage),
auth: None,
analysis,
with_graphql: true,
},
);
Expand Down

0 comments on commit 1c03182

Please sign in to comment.