Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeout configurable #101

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extensions:
type: auth
endpoint: auth-cluster
failureMode: deny
timeout: 10ms
ratelimit-ext:
type: ratelimit
endpoint: ratelimit-cluster
Expand Down
76 changes: 67 additions & 9 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ use std::fmt::{Debug, Display, Formatter};
use std::rc::Rc;
use std::sync::Arc;

use crate::attribute::Attribute;
use crate::envoy::{RateLimitDescriptor, RateLimitDescriptor_Entry};
use crate::policy::Policy;
use crate::policy_index::PolicyIndex;
use crate::service::GrpcService;
use cel_interpreter::functions::duration;
use cel_interpreter::objects::ValueType;
use cel_interpreter::{Context, Expression, Value};
use cel_parser::{Atom, RelationOp};
use log::debug;
use protobuf::RepeatedField;
use proxy_wasm::hostcalls;
use serde::Deserialize;

use crate::attribute::Attribute;
use crate::envoy::{RateLimitDescriptor, RateLimitDescriptor_Entry};
use crate::policy::Policy;
use crate::policy_index::PolicyIndex;
use crate::service::GrpcService;
use serde::de::{Error, Visitor};
use serde::{Deserialize, Deserializer};
use std::time::Duration;

#[derive(Deserialize, Debug, Clone)]
pub struct SelectorItem {
Expand Down Expand Up @@ -534,6 +536,52 @@ pub struct Extension {
pub endpoint: String,
// Deny/Allow request when faced with an irrecoverable failure.
pub failure_mode: FailureMode,
#[serde(default)]
pub timeout: Timeout,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Timeout(pub Duration);
impl Default for Timeout {
fn default() -> Self {
Timeout(Duration::from_millis(20))
}
}

impl<'de> Deserialize<'de> for Timeout {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(TimeoutVisitor)
}
}

struct TimeoutVisitor;
impl<'de> Visitor<'de> for TimeoutVisitor {
alexsnaps marked this conversation as resolved.
Show resolved Hide resolved
type Value = Timeout;

fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
formatter.write_str("DurationString -> Sign? Number Unit String? Sign -> '-' Number -> Digit+ ('.' Digit+)? Digit -> '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' Unit -> 'h' | 'm' | 's' | 'ms' | 'us' | 'ns' String -> DurationString")
}

fn visit_str<E>(self, string: &str) -> Result<Self::Value, E>
where
E: Error,
{
self.visit_string(String::from(string))
}

fn visit_string<E>(self, string: String) -> Result<Self::Value, E>
where
E: Error,
{
match duration(Arc::new(string)) {
Ok(Value::Duration(duration)) => Ok(Timeout(duration.to_std().unwrap())),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be an issue here, if I understand this right a "CelDuration" can be negative, a std can't if I'm not mistaken. So we expose it as if it could, then would panic if one would provide a negative one, which would be nonsensible in any case tho...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are correct, this would panic on a negative as OutOfRangeError(())

Err(e) => Err(E::custom(e)),
_ => Err(E::custom("Unsupported Duration Value")),
}
}
}

#[derive(Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -627,12 +675,14 @@ mod test {
"authorino": {
"type": "auth",
"endpoint": "authorino-cluster",
"failureMode": "deny"
"failureMode": "deny",
"timeout": "24ms"
},
"limitador": {
"type": "ratelimit",
"endpoint": "limitador-cluster",
"failureMode": "allow"
"failureMode": "allow",
"timeout": "42ms"
}
},
"policies": [
Expand Down Expand Up @@ -703,6 +753,7 @@ mod test {
assert_eq!(auth_extension.extension_type, ExtensionType::Auth);
assert_eq!(auth_extension.endpoint, "authorino-cluster");
assert_eq!(auth_extension.failure_mode, FailureMode::Deny);
assert_eq!(auth_extension.timeout, Timeout(Duration::from_millis(24)))
} else {
panic!()
}
Expand All @@ -711,6 +762,7 @@ mod test {
assert_eq!(rl_extension.extension_type, ExtensionType::RateLimit);
assert_eq!(rl_extension.endpoint, "limitador-cluster");
assert_eq!(rl_extension.failure_mode, FailureMode::Allow);
assert_eq!(rl_extension.timeout, Timeout(Duration::from_millis(42)))
} else {
panic!()
}
Expand Down Expand Up @@ -979,6 +1031,12 @@ mod test {
let filter_config = res.unwrap();
assert_eq!(filter_config.policies.len(), 1);

let extensions = &filter_config.extensions;
assert_eq!(
extensions.get("limitador").unwrap().timeout,
Timeout(Duration::from_millis(20))
);

let rules = &filter_config.policies[0].rules;
assert_eq!(rules.len(), 1);

Expand Down
11 changes: 8 additions & 3 deletions src/operation_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ impl Operation {
fn trigger(&self) -> Result<u32, Status> {
if let Some(message) = (self.grpc_message_build_fn)(self.get_extension_type(), &self.action)
{
let res = self
.service
.send(self.get_map_values_bytes_fn, self.grpc_call_fn, message);
let res = self.service.send(
self.get_map_values_bytes_fn,
self.grpc_call_fn,
message,
self.extension.timeout.0,
);
self.set_result(res);
self.next_state();
res
Expand Down Expand Up @@ -229,6 +232,7 @@ fn grpc_message_build_fn(
#[cfg(test)]
mod tests {
use super::*;
use crate::configuration::Timeout;
use crate::envoy::RateLimitRequest;
use protobuf::RepeatedField;
use std::time::Duration;
Expand Down Expand Up @@ -283,6 +287,7 @@ mod tests {
extension_type,
endpoint: "local".to_string(),
failure_mode: FailureMode::Deny,
timeout: Timeout(Duration::from_millis(42)),
}),
action: Action {
extension: "local".to_string(),
Expand Down
3 changes: 2 additions & 1 deletion src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl GrpcServiceHandler {
get_map_values_bytes_fn: GetMapValuesBytesFn,
grpc_call_fn: GrpcCallFn,
message: GrpcMessageRequest,
timeout: Duration,
) -> Result<u32, Status> {
let msg = Message::write_to_bytes(&message).unwrap();
let metadata = self
Expand All @@ -140,7 +141,7 @@ impl GrpcServiceHandler {
self.service.method(),
metadata,
Some(&msg),
Duration::from_secs(5),
timeout,
)
}

Expand Down
3 changes: 2 additions & 1 deletion tests/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const CONFIG: &str = r#"{
"authorino": {
"type": "auth",
"endpoint": "authorino-cluster",
"failureMode": "deny"
"failureMode": "deny",
"timeout": "5s"
}
},
"policies": [
Expand Down
6 changes: 4 additions & 2 deletions tests/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ const CONFIG: &str = r#"{
"authorino": {
"type": "auth",
"endpoint": "authorino-cluster",
"failureMode": "deny"
"failureMode": "deny",
"timeout": "5s"
},
"limitador": {
"type": "ratelimit",
"endpoint": "limitador-cluster",
"failureMode": "deny"
"failureMode": "deny",
"timeout": "5s"
}
},
"policies": [
Expand Down
12 changes: 8 additions & 4 deletions tests/rate_limited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ fn it_limits() {
"limitador": {
"type": "ratelimit",
"endpoint": "limitador-cluster",
"failureMode": "deny"
"failureMode": "deny",
"timeout": "5s"
}
},
"policies": [
Expand Down Expand Up @@ -242,7 +243,8 @@ fn it_passes_additional_headers() {
"limitador": {
"type": "ratelimit",
"endpoint": "limitador-cluster",
"failureMode": "deny"
"failureMode": "deny",
"timeout": "5s"
}
},
"policies": [
Expand Down Expand Up @@ -411,7 +413,8 @@ fn it_rate_limits_with_empty_conditions() {
"limitador": {
"type": "ratelimit",
"endpoint": "limitador-cluster",
"failureMode": "deny"
"failureMode": "deny",
"timeout": "5s"
}
},
"policies": [
Expand Down Expand Up @@ -529,7 +532,8 @@ fn it_does_not_rate_limits_when_selector_does_not_exist_and_misses_default_value
"limitador": {
"type": "ratelimit",
"endpoint": "limitador-cluster",
"failureMode": "deny"
"failureMode": "deny",
"timeout": "5s"
}
},
"policies": [
Expand Down
Loading