Skip to content

Commit

Permalink
fix: avoid token bucket access if memory cache exists
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Oct 26, 2023
1 parent 641e3bd commit 465461a
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions libs/jwst-storage/src/storage/docs/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,14 @@ impl DocDBStorage {
#[async_trait]
impl DocStorage<JwstStorageError> for DocDBStorage {
async fn detect_workspace(&self, workspace_id: &str) -> JwstStorageResult<bool> {
if self.workspaces.read().await.contains_key(workspace_id) {
return Ok(true);
}

trace!("check workspace exists: get lock");
let _lock = self.bucket.read().await;

Ok(self.workspaces.read().await.contains_key(workspace_id)
|| Self::workspace_count(&self.pool, workspace_id).await.map(|c| c > 0)?)
Ok(Self::workspace_count(&self.pool, workspace_id).await.map(|c| c > 0)?)
}

async fn get_or_create_workspace(&self, workspace_id: String) -> JwstStorageResult<Workspace> {
Expand Down Expand Up @@ -319,11 +322,11 @@ impl DocStorage<JwstStorageError> for DocDBStorage {
}

async fn delete_workspace(&self, workspace_id: &str) -> JwstStorageResult<()> {
debug!("delete workspace: get lock");
let _lock = self.bucket.write().await;

debug!("delete workspace cache: {workspace_id}");
self.workspaces.write().await.remove(workspace_id);

debug!("delete workspace: get lock");
let _lock = self.bucket.write().await;
DocDBStorage::delete_workspace(&self.pool, workspace_id).await?;

Ok(())
Expand Down Expand Up @@ -361,13 +364,12 @@ impl DocStorage<JwstStorageError> for DocDBStorage {
}

async fn update_doc_with_guid(&self, workspace_id: String, data: &[u8]) -> JwstStorageResult<()> {
debug!("write_update: get lock");
let _lock = self.bucket.write().await;

trace!("write_update: {:?}", data);
let mut decoder = RawDecoder::new(data.to_vec());
let guid = decoder.read_var_string()?;

debug!("write_update: get lock");
let _lock = self.bucket.write().await;
self.update(&self.pool, &workspace_id, &guid, decoder.drain()).await?;

Ok(())
Expand Down

1 comment on commit 465461a

@vercel
Copy link

@vercel vercel bot commented on 465461a Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.