Skip to content

Commit

Permalink
[refactor] Inlucing Extension within Service and Operation as Rc
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 5363635 commit 6e93a7a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 45 deletions.
17 changes: 4 additions & 13 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,16 +486,7 @@ impl TryFrom<PluginConfiguration> for FilterConfig {
let services = config
.extensions
.into_iter()
.map(|(name, ext)| {
(
name,
Rc::new(GrpcService::new(
ext.extension_type,
ext.endpoint,
ext.failure_mode,
)),
)
})
.map(|(name, ext)| (name, Rc::new(GrpcService::new(Rc::new(ext)))))
.collect();

Ok(Self {
Expand All @@ -505,15 +496,15 @@ impl TryFrom<PluginConfiguration> for FilterConfig {
}
}

#[derive(Deserialize, Debug, Clone, Default)]
#[derive(Deserialize, Debug, Clone, Default, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum FailureMode {
#[default]
Deny,
Allow,
}

#[derive(Deserialize, Debug, Clone, Default)]
#[derive(Deserialize, Debug, Clone, Default, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ExtensionType {
Auth,
Expand All @@ -528,7 +519,7 @@ pub struct PluginConfiguration {
pub policies: Vec<Policy>,
}

#[derive(Deserialize, Debug, Clone)]
#[derive(Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct Extension {
#[serde(rename = "type")]
Expand Down
76 changes: 59 additions & 17 deletions src/operation_dispatcher.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::configuration::{Extension, ExtensionType, FailureMode};
use crate::envoy::RateLimitDescriptor;
use crate::policy::Policy;
use crate::service::{GrpcMessage, GrpcServiceHandler};
Expand Down Expand Up @@ -32,15 +33,17 @@ type Procedure = (Rc<GrpcServiceHandler>, GrpcMessage);
pub(crate) struct Operation {
state: State,
result: Result<u32, Status>,
extension: Rc<Extension>,
procedure: Procedure,
}

#[allow(dead_code)]
impl Operation {
pub fn new(procedure: Procedure) -> Self {
pub fn new(extension: Rc<Extension>, procedure: Procedure) -> Self {
Self {
state: State::Pending,
result: Err(Status::Empty),
extension,
procedure,
}
}
Expand All @@ -57,13 +60,21 @@ impl Operation {
}
}

fn get_state(&self) -> State {
pub fn get_state(&self) -> State {
self.state.clone()
}

fn get_result(&self) -> Result<u32, Status> {
pub fn get_result(&self) -> Result<u32, Status> {
self.result
}

pub fn get_extension_type(&self) -> ExtensionType {
self.extension.extension_type.clone()
}

pub fn get_failure_mode(&self) -> FailureMode {
self.extension.failure_mode.clone()
}
}

#[allow(dead_code)]
Expand Down Expand Up @@ -101,7 +112,10 @@ impl OperationDispatcher {
policy.domain.clone(),
descriptors.clone(),
);
operations.push(Operation::new((service.clone(), message)))
operations.push(Operation::new(
service.get_extension(),
(Rc::clone(service), message),
))
}
});
self.push_operations(operations);
Expand Down Expand Up @@ -174,12 +188,33 @@ mod tests {
}
}

#[test]
fn operation_getters() {
let extension = Rc::new(Extension::default());
let operation = Operation::new(
extension,
(
Rc::new(build_grpc_service_handler()),
GrpcMessage::RateLimit(build_message()),
),
);

assert_eq!(operation.get_state(), State::Pending);
assert_eq!(operation.get_extension_type(), ExtensionType::RateLimit);
assert_eq!(operation.get_failure_mode(), FailureMode::Deny);
assert_eq!(operation.get_result(), Result::Ok(1));
}

#[test]
fn operation_transition() {
let mut operation = Operation::new((
Rc::new(build_grpc_service_handler()),
GrpcMessage::RateLimit(build_message()),
));
let extension = Rc::new(Extension::default());
let mut operation = Operation::new(
extension,
(
Rc::new(build_grpc_service_handler()),
GrpcMessage::RateLimit(build_message()),
),
);
assert_eq!(operation.get_state(), State::Pending);
operation.trigger();
assert_eq!(operation.get_state(), State::Waiting);
Expand All @@ -193,11 +228,14 @@ mod tests {
let operation_dispatcher = OperationDispatcher::default();

assert_eq!(operation_dispatcher.operations.borrow().len(), 1);

operation_dispatcher.push_operations(vec![Operation::new((
Rc::new(build_grpc_service_handler()),
GrpcMessage::RateLimit(build_message()),
))]);
let extension = Rc::new(Extension::default());
operation_dispatcher.push_operations(vec![Operation::new(
extension,
(
Rc::new(build_grpc_service_handler()),
GrpcMessage::RateLimit(build_message()),
),
)]);

assert_eq!(operation_dispatcher.operations.borrow().len(), 2);
}
Expand All @@ -214,10 +252,14 @@ mod tests {

#[test]
fn operation_dispatcher_next() {
let operation = Operation::new((
Rc::new(build_grpc_service_handler()),
GrpcMessage::RateLimit(build_message()),
));
let extension = Rc::new(Extension::default());
let operation = Operation::new(
extension,
(
Rc::new(build_grpc_service_handler()),
GrpcMessage::RateLimit(build_message()),
),
);
let operation_dispatcher = OperationDispatcher::default();
operation_dispatcher.push_operations(vec![operation]);

Expand Down
28 changes: 13 additions & 15 deletions src/service.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub(crate) mod auth;
pub(crate) mod rate_limit;

use crate::configuration::{ExtensionType, FailureMode};
use crate::configuration::{Extension, ExtensionType, FailureMode};
use crate::envoy::{CheckRequest, RateLimitDescriptor, RateLimitRequest};
use crate::service::auth::{AuthService, AUTH_METHOD_NAME, AUTH_SERVICE_NAME};
use crate::service::rate_limit::{RateLimitService, RATELIMIT_METHOD_NAME, RATELIMIT_SERVICE_NAME};
Expand Down Expand Up @@ -147,36 +147,30 @@ impl GrpcMessage {

#[derive(Default)]
pub struct GrpcService {
endpoint: String,
#[allow(dead_code)]
extension_type: ExtensionType,
extension: Rc<Extension>,
name: &'static str,
method: &'static str,
failure_mode: FailureMode,
}

impl GrpcService {
pub fn new(extension_type: ExtensionType, endpoint: String, failure_mode: FailureMode) -> Self {
match extension_type {
pub fn new(extension: Rc<Extension>) -> Self {
match extension.extension_type {
ExtensionType::Auth => Self {
endpoint,
extension_type,
extension,
name: AUTH_SERVICE_NAME,
method: AUTH_METHOD_NAME,
failure_mode,
},
ExtensionType::RateLimit => Self {
endpoint,
extension_type,
extension,
name: RATELIMIT_SERVICE_NAME,
method: RATELIMIT_METHOD_NAME,
failure_mode,
},
}
}

fn endpoint(&self) -> &str {
&self.endpoint
&self.extension.endpoint
}
fn name(&self) -> &str {
self.name
Expand All @@ -185,7 +179,7 @@ impl GrpcService {
self.method
}
pub fn failure_mode(&self) -> &FailureMode {
&self.failure_mode
&self.extension.failure_mode
}
}

Expand Down Expand Up @@ -236,8 +230,12 @@ impl GrpcServiceHandler {
)
}

pub fn get_extension(&self) -> Rc<Extension> {
Rc::clone(&self.service.extension)
}

pub fn get_extension_type(&self) -> ExtensionType {
self.service.extension_type.clone()
self.service.extension.extension_type.clone()
}
}

Expand Down

0 comments on commit 6e93a7a

Please sign in to comment.