Skip to content

Commit

Permalink
refactor(webserver): cache list_repositories in CodeService (#1886)
Browse files Browse the repository at this point in the history
* refactor(webserver): cache list_repositories in CodeService

* update

* update

* update
  • Loading branch information
wsxiaoys authored Apr 18, 2024
1 parent 902c148 commit 138ef83
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ authors = ["TabbyML Team"]
homepage = "https://github.com/TabbyML/tabby"

[workspace.dependencies]
cached = { version = "0.49.3", features = ["async"] }
cached = "0.49.3"
lazy_static = "1.4.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
Expand Down
3 changes: 3 additions & 0 deletions crates/tabby-common/src/api/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub enum CodeSearchError {

#[error(transparent)]
TantivyError(#[from] tantivy::TantivyError),

#[error(transparent)]
Other(#[from] anyhow::Error),
}

#[async_trait]
Expand Down
3 changes: 2 additions & 1 deletion crates/tabby/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ thiserror.workspace = true
chrono.workspace = true
axum-prometheus = "0.4.0"
uuid.workspace = true
nucleo = { workspace = true }
nucleo.workspace = true
cached = { workspace = true, features = ["async"] }

[dependencies.openssl]
optional = true
Expand Down
4 changes: 4 additions & 0 deletions crates/tabby/src/routes/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,9 @@ pub async fn search(
warn!("{}", err);
Err(StatusCode::BAD_REQUEST)
}
Err(CodeSearchError::Other(err)) => {
warn!("{}", err);
Err(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
16 changes: 11 additions & 5 deletions crates/tabby/src/services/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{sync::Arc, time::Duration};

use anyhow::Result;
use async_trait::async_trait;
use cached::{CachedAsync, TimedCache};
use nucleo::Utf32String;
use tabby_common::{
api::code::{CodeSearch, CodeSearchError, Hit, HitDocument, SearchResponse},
Expand All @@ -25,6 +26,7 @@ struct CodeSearchImpl {

schema: CodeSearchSchema,
repository_access: Arc<dyn RepositoryAccess>,
repo_cache: Mutex<TimedCache<(), Vec<RepositoryConfig>>>,
}

impl CodeSearchImpl {
Expand All @@ -47,6 +49,7 @@ impl CodeSearchImpl {
reader,
query_parser,
schema: code_schema,
repo_cache: Mutex::new(TimedCache::with_lifespan(10 * 60)),
})
}

Expand Down Expand Up @@ -126,11 +129,14 @@ impl CodeSearch for CodeSearchImpl {
let language_query = self.schema.language_query(language);
let body_query = self.schema.body_query(tokens);

let repos = self
.repository_access
.list_repositories()
.await
.unwrap_or_default();
let mut cache = self.repo_cache.lock().await;

let repos = cache
.try_get_or_set_with((), || async {
let repos = self.repository_access.list_repositories().await?;
Ok::<_, anyhow::Error>(repos)
})
.await?;

let Some(git_url) = closest_match(git_url, repos.iter()) else {
return Ok(SearchResponse::default());
Expand Down
4 changes: 4 additions & 0 deletions crates/tabby/src/services/completion/completion_prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ async fn collect_snippets(
warn!("Failed to parse query: {}", err);
return ret;
}
Err(CodeSearchError::Other(err)) => {
warn!("Failed to search: {}", err);
return ret;
}
};

let mut count_characters = 0;
Expand Down
2 changes: 1 addition & 1 deletion ee/tabby-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sql_query_builder = { version = "2.1.0", features = ["sqlite"] }
sqlx = { version = "0.7.3", features = ["sqlite", "chrono", "runtime-tokio", "macros"] }
tokio = { workspace = true, features = ["fs"] }
uuid.workspace = true
cached.workspace = true
cached = { workspace = true, features = ["async"]}

[dev-dependencies]
assert_matches = "1.5.0"
Expand Down
1 change: 0 additions & 1 deletion ee/tabby-webserver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ async-trait.workspace = true
axum = { workspace = true, features = ["ws", "headers"] }
base64 = "0.22.0"
bincode = "1.3.3"
cached = { workspace = true }
chrono = { workspace = true, features = ["serde"] }
futures.workspace = true
hash-ids.workspace = true
Expand Down
31 changes: 7 additions & 24 deletions ee/tabby-webserver/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use axum::{
response::{IntoResponse, Response},
routing, Extension, Json, Router,
};
use cached::{CachedAsync, TimedCache};
use hyper::{header::CONTENT_TYPE, Body, StatusCode};
use juniper::ID;
use juniper_axum::{graphiql, playground};
Expand All @@ -21,7 +20,6 @@ use tabby_common::{
config::{RepositoryAccess, RepositoryConfig},
};
use tabby_db::DbConn;
use tokio::sync::Mutex;
use tracing::{error, warn};

use crate::{
Expand All @@ -45,14 +43,18 @@ use crate::{
struct RepositoryAccessImpl {
git_repository_service: Arc<dyn GitRepositoryService>,
github_repository_service: Arc<dyn GithubRepositoryProviderService>,
url_cache: Mutex<TimedCache<(), Vec<RepositoryConfig>>>,
}

#[async_trait]
impl RepositoryAccess for RepositoryAccessImpl {
async fn list_repositories(&self) -> anyhow::Result<Vec<RepositoryConfig>> {
let mut cache = self.url_cache.lock().await;
let mut repos = vec![];
let mut repos: Vec<RepositoryConfig> = self
.git_repository_service
.list(None, None, None, None)
.await?
.into_iter()
.map(|repo| RepositoryConfig::new(repo.git_url))
.collect();

repos.extend(
self.github_repository_service
Expand All @@ -63,24 +65,6 @@ impl RepositoryAccess for RepositoryAccessImpl {
.map(RepositoryConfig::new),
);

repos.extend(
cache
.try_get_or_set_with((), || async {
let repos = self
.git_repository_service
.list(None, None, None, None)
.await?;
Ok::<_, anyhow::Error>(
repos
.into_iter()
.map(|repo| RepositoryConfig::new(repo.git_url))
.collect(),
)
})
.await?
.clone(),
);

Ok(repos)
}
}
Expand Down Expand Up @@ -112,7 +96,6 @@ impl WebserverHandle {
repository_access: Arc::new(RepositoryAccessImpl {
git_repository_service,
github_repository_service,
url_cache: Mutex::new(TimedCache::with_lifespan(10 * 60)),
}),
}
}
Expand Down

0 comments on commit 138ef83

Please sign in to comment.