Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(webserver): cache list_repositories in CodeService #1886

Merged
merged 5 commits into from
Apr 18, 2024
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
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 @@
warn!("{}", err);
Err(StatusCode::BAD_REQUEST)
}
Err(CodeSearchError::Other(err)) => {
warn!("{}", err);

Check warning on line 64 in crates/tabby/src/routes/search.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby/src/routes/search.rs#L64

Added line #L64 was not covered by tests
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 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 @@

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

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

Check warning on line 52 in crates/tabby/src/services/code.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby/src/services/code.rs#L52

Added line #L52 was not covered by tests
})
}

Expand Down Expand Up @@ -126,11 +129,14 @@
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;

Check warning on line 132 in crates/tabby/src/services/code.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby/src/services/code.rs#L132

Added line #L132 was not covered by tests

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

Check warning on line 139 in crates/tabby/src/services/code.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby/src/services/code.rs#L134-L139

Added lines #L134 - L139 were not covered by tests

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 @@
warn!("Failed to parse query: {}", err);
return ret;
}
Err(CodeSearchError::Other(err)) => {
warn!("Failed to search: {}", err);
return ret;

Check warning on line 205 in crates/tabby/src/services/completion/completion_prompt.rs

View check run for this annotation

Codecov / codecov/patch

crates/tabby/src/services/completion/completion_prompt.rs#L203-L205

Added lines #L203 - L205 were not covered by tests
}
};

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 @@
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 @@
config::{RepositoryAccess, RepositoryConfig},
};
use tabby_db::DbConn;
use tokio::sync::Mutex;
use tracing::{error, warn};

use crate::{
Expand All @@ -45,14 +43,18 @@
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();

Check warning on line 57 in ee/tabby-webserver/src/handler.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/handler.rs#L51-L57

Added lines #L51 - L57 were not covered by tests

repos.extend(
self.github_repository_service
Expand All @@ -63,24 +65,6 @@
.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 @@
repository_access: Arc::new(RepositoryAccessImpl {
git_repository_service,
github_repository_service,
url_cache: Mutex::new(TimedCache::with_lifespan(10 * 60)),
}),
}
}
Expand Down
Loading