Skip to content

Commit

Permalink
[refactor] Wiring up altogether
Browse files Browse the repository at this point in the history
Signed-off-by: dd di cesare <[email protected]>
  • Loading branch information
didierofrivia committed Sep 4, 2024
1 parent ef20110 commit 3701262
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
51 changes: 24 additions & 27 deletions src/filter/http_context.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::configuration::{FailureMode, FilterConfig};
use crate::envoy::{RateLimitResponse, RateLimitResponse_Code};
use crate::operation_dispatcher::OperationDispatcher;
use crate::policy::Policy;
use crate::service::rate_limit::RateLimitService;
use crate::service::{GrpcServiceHandler, HeaderResolver};
use log::{debug, warn};
use protobuf::Message;
use proxy_wasm::traits::{Context, HttpContext};
Expand All @@ -13,7 +12,7 @@ pub struct Filter {
pub context_id: u32,
pub config: Rc<FilterConfig>,
pub response_headers_to_add: Vec<(String, String)>,
pub header_resolver: Rc<HeaderResolver>,
pub operation_dispatcher: OperationDispatcher,
}

impl Filter {
Expand All @@ -40,33 +39,31 @@ impl Filter {
return Action::Continue;
}

// todo(adam-cattermole): For now we just get the first GrpcService but we expect to have
// an action which links to the service that should be used
let rls = self
.config
.services
.values()
.next()
.expect("expect a value");

let handler = GrpcServiceHandler::new(Rc::clone(rls), Rc::clone(&self.header_resolver));
let message = RateLimitService::message(rlp.domain.clone(), descriptors);
// Build Actions from config actions
self.operation_dispatcher.build_operations(rlp, descriptors);
// populate actions in the dispatcher
// call the next on the match

match handler.send(message) {
Ok(call_id) => {
debug!(
"#{} initiated gRPC call (id# {}) to Limitador",
self.context_id, call_id
);
Action::Pause
}
Err(e) => {
warn!("gRPC call to Limitador failed! {e:?}");
if let FailureMode::Deny = rls.failure_mode() {
self.send_http_response(500, vec![], Some(b"Internal Server Error.\n"))
if let Some(result) = self.operation_dispatcher.next() {
match result {
(_state, Ok(call_id)) => {
debug!(
"#{} initiated gRPC call (id# {}) to Limitador",
self.context_id, call_id
);
Action::Pause
}
(_state, Err(e)) => {
warn!("gRPC call to Limitador failed! {e:?}");
// TODO(didierofrivia): Get the failure_mode
/*if let FailureMode::Deny = rls.failure_mode() {
self.send_http_response(500, vec![], Some(b"Internal Server Error.\n"))
} */
Action::Continue
}
Action::Continue
}
} else {
Action::Continue
}
}

Expand Down
19 changes: 17 additions & 2 deletions src/filter/root_context.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::configuration::{FilterConfig, PluginConfiguration};
use crate::filter::http_context::Filter;
use crate::service::HeaderResolver;
use crate::operation_dispatcher::OperationDispatcher;
use crate::service::{GrpcServiceHandler, HeaderResolver};
use const_format::formatcp;
use log::{debug, error, info};
use proxy_wasm::traits::{Context, HttpContext, RootContext};
use proxy_wasm::types::ContextType;
use std::collections::HashMap;
use std::rc::Rc;

const WASM_SHIM_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -37,11 +39,24 @@ impl RootContext for FilterRoot {

fn create_http_context(&self, context_id: u32) -> Option<Box<dyn HttpContext>> {
debug!("#{} create_http_context", context_id);
let mut service_handlers: HashMap<String, Rc<GrpcServiceHandler>> = HashMap::new();
self.config
.services
.iter()
.for_each(|(extension, service)| {
service_handlers
.entry(extension.clone())
.or_insert(Rc::from(GrpcServiceHandler::new(
Rc::clone(service),
Rc::new(HeaderResolver::new()),
None,
)));
});
Some(Box::new(Filter {
context_id,
config: Rc::clone(&self.config),
response_headers_to_add: Vec::default(),
header_resolver: Rc::new(HeaderResolver::new()),
operation_dispatcher: OperationDispatcher::new(service_handlers),
}))
}

Expand Down

0 comments on commit 3701262

Please sign in to comment.