refactor(tvix/castore/blob/from_addr): use match guards

This will allow feature-flagging some of the backends.

Change-Id: Idffbf8b3fd154f5a3d938225c3871feffea8ff8c
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11200
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
This commit is contained in:
Florian Klink 2024-03-19 11:41:26 +02:00 committed by clbot
parent 602f4b2cb8
commit 7deadd50d5

View file

@ -20,66 +20,72 @@ pub async fn from_addr(uri: &str) -> Result<Box<dyn BlobService>, crate::Error>
let url = Url::parse(uri) let url = Url::parse(uri)
.map_err(|e| crate::Error::StorageError(format!("unable to parse url: {}", e)))?; .map_err(|e| crate::Error::StorageError(format!("unable to parse url: {}", e)))?;
Ok(if url.scheme() == "memory" { let blob_service: Box<dyn BlobService> = match url.scheme() {
// memory doesn't support host or path in the URL. "memory" => {
if url.has_host() || !url.path().is_empty() { // memory doesn't support host or path in the URL.
return Err(Error::StorageError("invalid url".to_string())); if url.has_host() || !url.path().is_empty() {
} return Err(Error::StorageError("invalid url".to_string()));
Box::<MemoryBlobService>::default() }
} else if url.scheme() == "sled" { Box::<MemoryBlobService>::default()
// sled doesn't support host, and a path can be provided (otherwise
// it'll live in memory only).
if url.has_host() {
return Err(Error::StorageError("no host allowed".to_string()));
} }
"sled" => {
// sled doesn't support host, and a path can be provided (otherwise
// it'll live in memory only).
if url.has_host() {
return Err(Error::StorageError("no host allowed".to_string()));
}
if url.path() == "/" { if url.path() == "/" {
return Err(Error::StorageError( return Err(Error::StorageError(
"cowardly refusing to open / with sled".to_string(), "cowardly refusing to open / with sled".to_string(),
)); ));
} }
// TODO: expose other parameters as URL parameters? // TODO: expose other parameters as URL parameters?
if url.path().is_empty() { Box::new(if url.path().is_empty() {
return Ok(Box::new( SledBlobService::new_temporary().map_err(|e| Error::StorageError(e.to_string()))?
SledBlobService::new_temporary().map_err(|e| Error::StorageError(e.to_string()))?, } else {
)); SledBlobService::new(url.path()).map_err(|e| Error::StorageError(e.to_string()))?
})
} }
return Ok(Box::new( scheme if scheme.starts_with("grpc+") => {
SledBlobService::new(url.path()).map_err(|e| Error::StorageError(e.to_string()))?, // schemes starting with grpc+ go to the GRPCPathInfoService.
)); // That's normally grpc+unix for unix sockets, and grpc+http(s) for the HTTP counterparts.
} else if url.scheme().starts_with("grpc+") { // - In the case of unix sockets, there must be a path, but may not be a host.
// schemes starting with grpc+ go to the GRPCPathInfoService. // - In the case of non-unix sockets, there must be a host, but no path.
// That's normally grpc+unix for unix sockets, and grpc+http(s) for the HTTP counterparts. // Constructing the channel is handled by tvix_castore::channel::from_url.
// - In the case of unix sockets, there must be a path, but may not be a host. let client = BlobServiceClient::new(crate::tonic::channel_from_url(&url).await?);
// - In the case of non-unix sockets, there must be a host, but no path. Box::new(GRPCBlobService::from_client(client))
// Constructing the channel is handled by tvix_castore::channel::from_url.
let client = BlobServiceClient::new(crate::tonic::channel_from_url(&url).await?);
Box::new(GRPCBlobService::from_client(client))
} else if url.scheme() == "simplefs" {
if url.path().is_empty() {
return Err(Error::StorageError("Invalid filesystem path".to_string()));
} }
"simplefs" => {
if url.path().is_empty() {
return Err(Error::StorageError("Invalid filesystem path".to_string()));
}
Box::new(SimpleFilesystemBlobService::new(url.path().into()).await?) Box::new(SimpleFilesystemBlobService::new(url.path().into()).await?)
} else if let Some(_trimmed_scheme) = url.scheme().strip_prefix("objectstore+") { }
// We need to convert the URL to string, strip the prefix there, and then scheme if scheme.starts_with("objectstore+") => {
// parse it back as url, as Url::set_scheme() rejects some of the transitions we want to do. // We need to convert the URL to string, strip the prefix there, and then
let trimmed_url = { // parse it back as url, as Url::set_scheme() rejects some of the transitions we want to do.
let s = url.to_string(); let trimmed_url = {
Url::parse(s.strip_prefix("objectstore+").unwrap()).unwrap() let s = url.to_string();
}; Url::parse(s.strip_prefix("objectstore+").unwrap()).unwrap()
return Ok(Box::new( };
ObjectStoreBlobService::parse_url(&trimmed_url) Box::new(
.map_err(|e| Error::StorageError(e.to_string()))?, ObjectStoreBlobService::parse_url(&trimmed_url)
)); .map_err(|e| Error::StorageError(e.to_string()))?,
} else { )
Err(crate::Error::StorageError(format!( }
"unknown scheme: {}", scheme => {
url.scheme() return Err(crate::Error::StorageError(format!(
)))? "unknown scheme: {}",
}) scheme
)))
}
};
Ok(blob_service)
} }
#[cfg(test)] #[cfg(test)]