Skip to content

Commit

Permalink
Move some code to common util library
Browse files Browse the repository at this point in the history
  • Loading branch information
allada committed Dec 27, 2020
1 parent d2ed91e commit 2048d24
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 49 deletions.
1 change: 1 addition & 0 deletions cas/grpc_service/BUILD
Expand Up @@ -11,6 +11,7 @@ rust_library(
"//third_party:futures_core",
"//third_party:stdext",
"//cas/store",
"//util:common",
],
visibility = ["//cas:__pkg__"]
)
Expand Down
56 changes: 7 additions & 49 deletions cas/grpc_service/cas_server.rs
Expand Up @@ -8,8 +8,9 @@ use std::pin::Pin;

use futures_core::Stream;
use tokio::io::Error;
use tonic::{Code, Request, Response, Status};
use tonic::{Request, Response, Status};

use common;
use macros::{error_if, make_err};
use proto::build::bazel::remote::execution::v2::{
batch_update_blobs_response, content_addressable_storage_server::ContentAddressableStorage,
Expand All @@ -20,49 +21,6 @@ use proto::build::bazel::remote::execution::v2::{
};
use store::Store;

// use tokio::io::Error;
// use tonic::Code;
use proto::google::rpc::Status as GrpcStatus;
use std::result::Result;
fn result_to_status(result: Result<(), Error>) -> GrpcStatus {
use tokio::io::ErrorKind;
fn kind_to_code(kind: &ErrorKind) -> Code {
match kind {
ErrorKind::NotFound => Code::NotFound,
ErrorKind::PermissionDenied => Code::PermissionDenied,
ErrorKind::ConnectionRefused => Code::Unavailable,
ErrorKind::ConnectionReset => Code::Unavailable,
ErrorKind::ConnectionAborted => Code::Unavailable,
ErrorKind::NotConnected => Code::Internal,
ErrorKind::AddrInUse => Code::Internal,
ErrorKind::AddrNotAvailable => Code::Internal,
ErrorKind::BrokenPipe => Code::Internal,
ErrorKind::AlreadyExists => Code::AlreadyExists,
ErrorKind::WouldBlock => Code::Internal,
ErrorKind::InvalidInput => Code::InvalidArgument,
ErrorKind::InvalidData => Code::InvalidArgument,
ErrorKind::TimedOut => Code::DeadlineExceeded,
ErrorKind::WriteZero => Code::Internal,
ErrorKind::Interrupted => Code::Aborted,
ErrorKind::Other => Code::Internal,
ErrorKind::UnexpectedEof => Code::Internal,
_ => Code::Internal,
}
}
match result {
Ok(()) => GrpcStatus {
code: Code::Ok as i32,
message: "".to_string(),
details: vec![],
},
Err(error) => GrpcStatus {
code: kind_to_code(&error.kind()) as i32,
message: format!("Error: {:?}", error),
details: vec![],
},
}
}

#[derive(Debug)]
pub struct CasServer {
pub store: Box<dyn Store>,
Expand Down Expand Up @@ -110,10 +68,10 @@ impl ContentAddressableStorage for CasServer {
let result_status: Result<(), Error> = try {
let digest = request
.digest
.ok_or_else(|| make_err!("Digest not found in request"))?;
let size_bytes = usize::try_from(digest.size_bytes).or_else(|_| {
Err(make_err!("Digest size_bytes was not convertable to usize"))
})?;
.ok_or(make_err!("Digest not found in request"))?;
let size_bytes = usize::try_from(digest.size_bytes).or(Err(make_err!(
"Digest size_bytes was not convertable to usize"
)))?;
error_if!(
size_bytes != request.data.len(),
"Digest for upload had mismatching sizes, digest said {} data said {}",
Expand All @@ -130,7 +88,7 @@ impl ContentAddressableStorage for CasServer {
};
let response = batch_update_blobs_response::Response {
digest: orig_digest,
status: Some(result_to_status(result_status)),
status: Some(common::result_to_status(result_status)),
};
batch_response.responses.push(response);
}
Expand Down
11 changes: 11 additions & 0 deletions util/BUILD
Expand Up @@ -10,3 +10,14 @@ rust_library(
],
visibility = ["//visibility:public"],
)

rust_library(
name = "common",
srcs = ["common.rs"],
deps = [
"//third_party:tokio",
"//third_party:tonic",
"//proto",
],
visibility = ["//visibility:public"],
)
46 changes: 46 additions & 0 deletions util/common.rs
@@ -0,0 +1,46 @@
// Copyright 2020 Nathan (Blaise) Bruer. All rights reserved.

use std::result::Result;

use tokio::io::{Error, ErrorKind};

use proto::google::rpc::Status as GrpcStatus;
use tonic::Code;

pub fn result_to_status(result: Result<(), Error>) -> GrpcStatus {
fn kind_to_code(kind: &ErrorKind) -> Code {
match kind {
ErrorKind::NotFound => Code::NotFound,
ErrorKind::PermissionDenied => Code::PermissionDenied,
ErrorKind::ConnectionRefused => Code::Unavailable,
ErrorKind::ConnectionReset => Code::Unavailable,
ErrorKind::ConnectionAborted => Code::Unavailable,
ErrorKind::NotConnected => Code::Internal,
ErrorKind::AddrInUse => Code::Internal,
ErrorKind::AddrNotAvailable => Code::Internal,
ErrorKind::BrokenPipe => Code::Internal,
ErrorKind::AlreadyExists => Code::AlreadyExists,
ErrorKind::WouldBlock => Code::Internal,
ErrorKind::InvalidInput => Code::InvalidArgument,
ErrorKind::InvalidData => Code::InvalidArgument,
ErrorKind::TimedOut => Code::DeadlineExceeded,
ErrorKind::WriteZero => Code::Internal,
ErrorKind::Interrupted => Code::Aborted,
ErrorKind::Other => Code::Internal,
ErrorKind::UnexpectedEof => Code::Internal,
_ => Code::Internal,
}
}
match result {
Ok(()) => GrpcStatus {
code: Code::Ok as i32,
message: "".to_string(),
details: vec![],
},
Err(error) => GrpcStatus {
code: kind_to_code(&error.kind()) as i32,
message: format!("Error: {:?}", error),
details: vec![],
},
}
}

0 comments on commit 2048d24

Please sign in to comment.