-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor] Extracting logic to Service trait and RateLimitService
Signed-off-by: dd di cesare <[email protected]>
- Loading branch information
1 parent
c19464b
commit 989f04f
Showing
4 changed files
with
94 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ mod filter; | |
mod glob; | ||
mod policy; | ||
mod policy_index; | ||
mod service; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
pub(crate) mod rate_limit; | ||
|
||
use protobuf::reflect::ProtobufValue; | ||
use protobuf::{Message, RepeatedField}; | ||
use proxy_wasm::types::Status; | ||
|
||
pub trait Service<M: Message, V: ProtobufValue> { | ||
fn message(domain: String, descriptors: RepeatedField<V>) -> M; | ||
fn send(&self, message: M) -> Result<u32, Status>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use crate::envoy::{RateLimitDescriptor, RateLimitRequest}; | ||
use crate::service::Service; | ||
use protobuf::{Message, RepeatedField}; | ||
use proxy_wasm::hostcalls::dispatch_grpc_call; | ||
use proxy_wasm::types::Status; | ||
use std::time::Duration; | ||
|
||
const RATELIMIT_SERVICE_NAME: &str = "envoy.service.ratelimit.v3.RateLimitService"; | ||
const RATELIMIT_METHOD_NAME: &str = "ShouldRateLimit"; | ||
|
||
pub struct RateLimitService<'a> { | ||
endpoint: String, | ||
metadata: Vec<(&'a str, &'a [u8])>, | ||
} | ||
|
||
impl<'a> RateLimitService<'a> { | ||
pub fn new(endpoint: &str, metadata: Vec<(&'a str, &'a [u8])>) -> RateLimitService<'a> { | ||
Self { | ||
endpoint: String::from(endpoint), | ||
metadata, | ||
} | ||
} | ||
} | ||
|
||
impl Service<RateLimitRequest, RateLimitDescriptor> for RateLimitService<'_> { | ||
fn message( | ||
domain: String, | ||
descriptors: RepeatedField<RateLimitDescriptor>, | ||
) -> RateLimitRequest { | ||
RateLimitRequest { | ||
domain, | ||
descriptors, | ||
hits_addend: 1, | ||
unknown_fields: Default::default(), | ||
cached_size: Default::default(), | ||
} | ||
} | ||
fn send(&self, message: RateLimitRequest) -> Result<u32, Status> { | ||
let msg = Message::write_to_bytes(&message).unwrap(); // TODO(didierofrivia): Error Handling | ||
dispatch_grpc_call( | ||
self.endpoint.as_str(), | ||
RATELIMIT_SERVICE_NAME, | ||
RATELIMIT_METHOD_NAME, | ||
self.metadata.clone(), | ||
Some(&msg), | ||
Duration::from_secs(5), | ||
) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use protobuf::{CachedSize, RepeatedField, UnknownFields}; | ||
use crate::envoy::{RateLimitDescriptor, RateLimitDescriptor_Entry}; | ||
use crate::service::rate_limit::RateLimitService; | ||
use crate::service::Service; | ||
|
||
#[test] | ||
fn builds_message() { | ||
let domain = "rlp1"; | ||
let mut field = RateLimitDescriptor::new(); | ||
let mut entry = RateLimitDescriptor_Entry::new(); | ||
entry.set_key("key1".to_string()); | ||
entry.set_value("value1".to_string()); | ||
field.set_entries(RepeatedField::from_vec(vec![entry])); | ||
let descriptors = RepeatedField::from_vec(vec![field]); | ||
|
||
let msg = RateLimitService::message(domain.to_string(), descriptors.clone()); | ||
|
||
assert_eq!(msg.hits_addend, 1); | ||
assert_eq!(msg.domain, domain.to_string()); | ||
assert_eq!(msg.descriptors , descriptors); | ||
assert_eq!(msg.unknown_fields , UnknownFields::default()); | ||
assert_eq!(msg.cached_size , CachedSize::default()); | ||
} | ||
} |