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: Do read check first instead #1546

Merged
merged 2 commits into from
Jan 9, 2023
Merged
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
18 changes: 11 additions & 7 deletions src/cache/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,20 +392,24 @@ impl Storage for opendal::Operator {

let path = ".sccache_check";

let can_write = match self.object(path).write("Hello, World!").await {
Ok(_) => true,
Err(err) if err.kind() == ErrorKind::ObjectAlreadyExists => true,
Err(err) if err.kind() == ErrorKind::ObjectPermissionDenied => false,
Err(err) => bail!("cache storage failed to write: {:?}", err),
};

// Read is required, return error directly if we can't read .
match self.object(path).read().await {
Ok(_) => (),
// Read not exist file with not found is ok.
Err(err) if err.kind() == ErrorKind::ObjectNotFound => (),
Err(err) => bail!("cache storage failed to read: {:?}", err),
};

let can_write = match self.object(path).write("Hello, World!").await {
Ok(_) => true,
Err(err) if err.kind() == ErrorKind::ObjectAlreadyExists => true,
// Toralte all other write errors because we can do read as least.
Err(err) => {
warn!("storage write check failed: {err:?}");
false
}
};

if can_write {
Ok(CacheMode::ReadWrite)
} else {
Expand Down