Skip to content

Commit f47cfa8

Browse files
committed
[refactor] Using action failure_mode when processing grpc error
Signed-off-by: dd di cesare <[email protected]>
1 parent 6061b92 commit f47cfa8

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

src/filter/http_context.rs

+21-25
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,26 @@ impl Filter {
6363
}
6464
}
6565

66-
fn handle_error_on_grpc_response(&self) {
67-
// todo(adam-cattermole): We need a method of knowing which service is the one currently
68-
// being used (the current action) so that we can get the failure mode
69-
let rls = self
70-
.config
71-
.services
72-
.values()
73-
.next()
74-
.expect("expect a value");
75-
match rls.failure_mode() {
66+
fn handle_error_on_grpc_response(&self, failure_mode: &FailureMode) {
67+
match failure_mode {
7668
FailureMode::Deny => {
7769
self.send_http_response(500, vec![], Some(b"Internal Server Error.\n"))
7870
}
7971
FailureMode::Allow => self.resume_http_request(),
8072
}
8173
}
8274

83-
fn process_ratelimit_grpc_response(&mut self, rl_resp: GrpcMessageResponse) {
75+
fn process_ratelimit_grpc_response(
76+
&mut self,
77+
rl_resp: GrpcMessageResponse,
78+
failure_mode: &FailureMode,
79+
) {
8480
match rl_resp {
8581
GrpcMessageResponse::RateLimit(RateLimitResponse {
8682
overall_code: RateLimitResponse_Code::UNKNOWN,
8783
..
8884
}) => {
89-
self.handle_error_on_grpc_response();
85+
self.handle_error_on_grpc_response(failure_mode);
9086
}
9187
GrpcMessageResponse::RateLimit(RateLimitResponse {
9288
overall_code: RateLimitResponse_Code::OVER_LIMIT,
@@ -157,16 +153,16 @@ impl Context for Filter {
157153
self.context_id
158154
);
159155

160-
let res_body_bytes = match self.get_grpc_call_response_body(0, resp_size) {
161-
Some(bytes) => bytes,
162-
None => {
163-
warn!("grpc response body is empty!");
164-
self.handle_error_on_grpc_response();
165-
return;
166-
}
167-
};
168-
169156
if let Some(operation) = self.operation_dispatcher.get_operation(token_id) {
157+
let failure_mode = &operation.get_failure_mode();
158+
let res_body_bytes = match self.get_grpc_call_response_body(0, resp_size) {
159+
Some(bytes) => bytes,
160+
None => {
161+
warn!("grpc response body is empty!");
162+
self.handle_error_on_grpc_response(failure_mode);
163+
return;
164+
}
165+
};
170166
let res = match GrpcMessageResponse::new(
171167
operation.get_extension_type(),
172168
&res_body_bytes,
@@ -177,19 +173,19 @@ impl Context for Filter {
177173
warn!(
178174
"failed to parse grpc response body into GrpcMessageResponse message: {e}"
179175
);
180-
self.handle_error_on_grpc_response();
176+
self.handle_error_on_grpc_response(failure_mode);
181177
return;
182178
}
183179
};
184180
match operation.get_extension_type() {
185-
ExtensionType::Auth => {}
186-
ExtensionType::RateLimit => self.process_ratelimit_grpc_response(res),
181+
ExtensionType::Auth => {} // TODO(didierofrivia): Process auth grpc response.
182+
ExtensionType::RateLimit => self.process_ratelimit_grpc_response(res, failure_mode),
187183
}
188184

189185
self.resume_http_request();
190186
} else {
191187
warn!("No Operation found with token_id: {token_id}");
192-
self.handle_error_on_grpc_response();
188+
self.handle_error_on_grpc_response(&FailureMode::Deny); // TODO(didierofrivia): Decide on what's the default failure mode
193189
}
194190
}
195191
}

src/service.rs

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl GrpcService {
4646
fn method(&self) -> &str {
4747
self.method
4848
}
49+
#[allow(dead_code)]
4950
pub fn failure_mode(&self) -> &FailureMode {
5051
&self.extension.failure_mode
5152
}

0 commit comments

Comments
 (0)