Skip to content

Commit

Permalink
[refactor] Using action failure_mode when processing grpc error
Browse files Browse the repository at this point in the history
Signed-off-by: dd di cesare <didi@posteo.net>
  • Loading branch information
didierofrivia committed Sep 16, 2024
1 parent 6061b92 commit f47cfa8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
46 changes: 21 additions & 25 deletions src/filter/http_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,26 @@ impl Filter {
}
}

fn handle_error_on_grpc_response(&self) {
// todo(adam-cattermole): We need a method of knowing which service is the one currently
// being used (the current action) so that we can get the failure mode
let rls = self
.config
.services
.values()
.next()
.expect("expect a value");
match rls.failure_mode() {
fn handle_error_on_grpc_response(&self, failure_mode: &FailureMode) {
match failure_mode {
FailureMode::Deny => {
self.send_http_response(500, vec![], Some(b"Internal Server Error.\n"))
}
FailureMode::Allow => self.resume_http_request(),
}
}

fn process_ratelimit_grpc_response(&mut self, rl_resp: GrpcMessageResponse) {
fn process_ratelimit_grpc_response(
&mut self,
rl_resp: GrpcMessageResponse,
failure_mode: &FailureMode,
) {
match rl_resp {
GrpcMessageResponse::RateLimit(RateLimitResponse {
overall_code: RateLimitResponse_Code::UNKNOWN,
..
}) => {
self.handle_error_on_grpc_response();
self.handle_error_on_grpc_response(failure_mode);
}
GrpcMessageResponse::RateLimit(RateLimitResponse {
overall_code: RateLimitResponse_Code::OVER_LIMIT,
Expand Down Expand Up @@ -157,16 +153,16 @@ impl Context for Filter {
self.context_id
);

let res_body_bytes = match self.get_grpc_call_response_body(0, resp_size) {
Some(bytes) => bytes,
None => {
warn!("grpc response body is empty!");
self.handle_error_on_grpc_response();
return;
}
};

if let Some(operation) = self.operation_dispatcher.get_operation(token_id) {
let failure_mode = &operation.get_failure_mode();
let res_body_bytes = match self.get_grpc_call_response_body(0, resp_size) {
Some(bytes) => bytes,
None => {
warn!("grpc response body is empty!");
self.handle_error_on_grpc_response(failure_mode);
return;
}
};
let res = match GrpcMessageResponse::new(
operation.get_extension_type(),
&res_body_bytes,
Expand All @@ -177,19 +173,19 @@ impl Context for Filter {
warn!(
"failed to parse grpc response body into GrpcMessageResponse message: {e}"
);
self.handle_error_on_grpc_response();
self.handle_error_on_grpc_response(failure_mode);
return;
}
};
match operation.get_extension_type() {
ExtensionType::Auth => {}
ExtensionType::RateLimit => self.process_ratelimit_grpc_response(res),
ExtensionType::Auth => {} // TODO(didierofrivia): Process auth grpc response.
ExtensionType::RateLimit => self.process_ratelimit_grpc_response(res, failure_mode),
}

self.resume_http_request();
} else {
warn!("No Operation found with token_id: {token_id}");
self.handle_error_on_grpc_response();
self.handle_error_on_grpc_response(&FailureMode::Deny); // TODO(didierofrivia): Decide on what's the default failure mode
}
}
}
1 change: 1 addition & 0 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl GrpcService {
fn method(&self) -> &str {
self.method
}
#[allow(dead_code)]
pub fn failure_mode(&self) -> &FailureMode {
&self.extension.failure_mode
}
Expand Down

0 comments on commit f47cfa8

Please sign in to comment.