Skip to content
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
17 changes: 11 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ use crate::*;
///
/// A simple wrapper around [aws_sdk_dynamodb::Client], that adds hooks with alternator-specific optimizations.
///
/// By default, enables round-robin load balancing, gzip request compression, strips requests from headers
/// that are not used by alternator, and chooses an arbitrary aws region, as alternator doesn't require one.
/// By default:
/// - enables round-robin load balancing
/// - strips headers that are not used by the alternator from all requests
/// - chooses an arbitrary aws region, as alternator doesn't require one

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This wording is a bit misleading. The client does not choose an arbitrary region; it sets a concrete default region. I would phrase this as "sets a default AWS region" instead.

/// - does not use request compression
///
/// Can be build using [AlternatorConfig] like so:
/// ```ignore
Expand All @@ -26,15 +29,17 @@ impl AlternatorClient {
let dynamodb_config = config.dynamodb_config.clone();
let extensions = config.alternator_ext.clone();

let request_compression = extensions.request_compression.unwrap_or_default();
let enforce_header_whitelist = extensions.enforce_header_whitelist.unwrap_or(true);
let request_compression = extensions
.request_compression
.unwrap_or(RequestCompression::disabled());
let optimize_headers = extensions.optimize_headers.unwrap_or(true);
let has_region = dynamodb_config.region().is_some();

let mut builder = dynamodb_config
.to_builder()
.interceptor(AlternatorInterceptor::new(
request_compression,
enforce_header_whitelist,
optimize_headers,
));

// If live nodes are not in config - create new config with live nodes.
Expand Down Expand Up @@ -524,7 +529,7 @@ mod tests {
fn test_client_stores_his_config_for_reference_only() {
let client = AlternatorClient::from_conf(
AlternatorConfig::builder()
.enforce_header_whitelist(true)
.optimize_headers(true)
.behavior_version_latest()
.build(),
);
Expand Down
32 changes: 16 additions & 16 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::*;
#[derive(Clone, Debug, Default)]
pub(crate) struct AlternatorExtensions {
pub(crate) request_compression: Option<RequestCompression>,
pub(crate) enforce_header_whitelist: Option<bool>,
pub(crate) optimize_headers: Option<bool>,
pub(crate) active_interval: Option<std::time::Duration>,
pub(crate) idle_interval: Option<std::time::Duration>,
pub(crate) routing_scope: Option<RoutingScope>,
Expand Down Expand Up @@ -57,16 +57,16 @@ impl AlternatorConfig {
AlternatorBuilder::from(config).build()
}

/// Before sending each request, strip them from headers that are not used by the Alternator.
/// Before sending each request, strip the headers from them which are not used by the Alternator.
///
/// This is done by an interceptor in `modify_before_transmit` hook.
///
/// Take note, that this may break your own interceptors,
/// if they happened to look inside these headers after this happens.
///
/// Turned on by default.
pub fn enforce_header_whitelist(&self) -> Option<bool> {
self.alternator_ext.enforce_header_whitelist
pub fn optimize_headers(&self) -> Option<bool> {
self.alternator_ext.optimize_headers
}
Comment on lines +68 to 70

/// Enable / disable request compression.
Expand All @@ -77,7 +77,7 @@ impl AlternatorConfig {
/// Take note, that this may break your own interceptors,
/// if they happened to look inside the body after this happens.
///
/// By default, Gzip compression is used, with 1024 threshold and level 6 of compression.
/// Turned off by default.
pub fn request_compression(&self) -> Option<RequestCompression> {
self.alternator_ext.request_compression.clone()
}
Expand Down Expand Up @@ -201,29 +201,29 @@ impl AlternatorBuilder {
}
}

/// Before sending each request, strip them from headers that are not used by the Alternator.
/// Before sending each request, strip the headers from them which are not used by the Alternator.
///
/// This is done by an interceptor in `modify_before_transmit` hook.
///
/// Take note, that this may break your own interceptors,
/// if they happened to look inside these headers after this happens.
///
/// Turned on by default.
pub fn enforce_header_whitelist(mut self, enforce: bool) -> Self {
self.set_enforce_header_whitelist(enforce);
pub fn optimize_headers(mut self, optimize: bool) -> Self {
self.set_optimize_headers(optimize);
self
}

/// Before sending each request, strip them from headers that are not used by the Alternator.
/// Before sending each request, strip the headers from them which are not used by the Alternator.
///
/// This is done by an interceptor in `modify_before_transmit` hook.
///
/// Take note, that this may break your own interceptors,
/// if they happened to look inside these headers after this happens.
///
/// Turned on by default.
pub fn set_enforce_header_whitelist(&mut self, enforce: bool) -> &mut Self {
self.alternator_ext.enforce_header_whitelist = Some(enforce);
pub fn set_optimize_headers(&mut self, optimize: bool) -> &mut Self {
self.alternator_ext.optimize_headers = Some(optimize);
self
}

Expand All @@ -235,7 +235,7 @@ impl AlternatorBuilder {
/// Take note, that this may break your own interceptors,
/// if they happened to look inside the body after this happens.
///
/// By default, Gzip compression is used, with 1024 threshold and level 6 of compression.
/// Turned off by default.
pub fn request_compression(mut self, request_compression: RequestCompression) -> Self {
self.set_request_compression(request_compression);
self
Expand All @@ -249,7 +249,7 @@ impl AlternatorBuilder {
/// Take note, that this may break your own interceptors,
/// if they happened to look inside the body after this happens.
///
/// By default, Gzip compression is used, with 1024 threshold and level 6 of compression.
/// Turned off by default.
pub fn set_request_compression(
&mut self,
request_compression: RequestCompression,
Expand Down Expand Up @@ -1031,7 +1031,7 @@ mod test {
.behavior_version_latest()
.build();

assert!(config.enforce_header_whitelist().is_none());
assert!(config.optimize_headers().is_none());

assert_eq!(
config
Expand All @@ -1042,7 +1042,7 @@ mod test {

let config = config.to_builder().build();

assert!(config.enforce_header_whitelist().is_none());
assert!(config.optimize_headers().is_none());

assert_eq!(
config
Expand All @@ -1055,7 +1055,7 @@ mod test {
#[test]
fn config_does_not_add_hooks() {
let config = AlternatorConfig::builder()
.enforce_header_whitelist(true)
.optimize_headers(true)
.behavior_version_latest()
.build();

Expand Down
10 changes: 4 additions & 6 deletions src/customize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use aws_sdk_dynamodb::client::customize::CustomizableOperation;
/// .customize()
/// .alternator_config_override(
/// AlternatorConfig::builder()
/// .enforce_header_whitelist(false)
/// .optimize_headers(false)
/// .request_compression(RequestCompression::disabled())
/// .behavior_version_latest()
/// .build()
Expand All @@ -36,11 +36,9 @@ impl<T, E, B> AlternatorCustomizableOperation<T, E, B> for CustomizableOperation
));
}

if let Some(enforce_header_whitelist) =
config_override.alternator_ext.enforce_header_whitelist
{
this = this.interceptor(AlternatorOverrideInterceptor::for_enforce_header_whitelist(
enforce_header_whitelist,
if let Some(optimize_headers) = config_override.alternator_ext.optimize_headers {
this = this.interceptor(AlternatorOverrideInterceptor::for_optimize_headers(
optimize_headers,
));
}

Expand Down
32 changes: 15 additions & 17 deletions src/interceptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ use crate::keyrouting::resolver;
#[derive(Debug)]
pub(crate) struct AlternatorInterceptor {
request_compression: RequestCompression,
enforce_header_whitelist: bool,
optimize_headers: bool,
}
impl AlternatorInterceptor {
pub fn new(request_compression: RequestCompression, enforce_header_whitelist: bool) -> Self {
pub fn new(request_compression: RequestCompression, optimize_headers: bool) -> Self {
Self {
request_compression,
enforce_header_whitelist,
optimize_headers,
}
}
}
Expand Down Expand Up @@ -69,14 +69,14 @@ impl Intercept for AlternatorInterceptor {
cfg: &mut ConfigBag,
) -> Result<(), BoxError> {
// check for overrides
let enforce_header_whitelist = cfg
let optimize_headers = cfg
.interceptor_state()
.load::<EnforceHeaderWhitelistStore>()
.map(|store| store.enforce_header_whitelist)
.unwrap_or(self.enforce_header_whitelist);
.load::<OptimizeHeadersStore>()
.map(|store| store.optimize_headers)
.unwrap_or(self.optimize_headers);

// enforce header whitelist
if enforce_header_whitelist {
// optimize headers
if optimize_headers {
strip_headers(context.request_mut());
}

Expand Down Expand Up @@ -121,10 +121,10 @@ impl Storable for RequestCompressionStore {
}

#[derive(Debug, Clone)]
pub(crate) struct EnforceHeaderWhitelistStore {
enforce_header_whitelist: bool,
pub(crate) struct OptimizeHeadersStore {
optimize_headers: bool,
}
impl Storable for EnforceHeaderWhitelistStore {
impl Storable for OptimizeHeadersStore {
type Storer = StoreReplace<Self>;
}

Expand Down Expand Up @@ -163,12 +163,10 @@ impl AlternatorOverrideInterceptor<RequestCompressionStore> {
}
}
}
impl AlternatorOverrideInterceptor<EnforceHeaderWhitelistStore> {
pub(crate) fn for_enforce_header_whitelist(enforce_header_whitelist: bool) -> Self {
impl AlternatorOverrideInterceptor<OptimizeHeadersStore> {
pub(crate) fn for_optimize_headers(optimize_headers: bool) -> Self {
AlternatorOverrideInterceptor {
store: EnforceHeaderWhitelistStore {
enforce_header_whitelist,
},
store: OptimizeHeadersStore { optimize_headers },
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ mod client;
mod compression;
mod config;
mod customize;
mod header_whitelist;
mod interceptors;
pub mod keyrouting;
mod live_nodes;
mod optimize_headers;
mod query_plan;
mod routing_scope;

pub use crate::client::*;
pub use crate::compression::*;
pub use crate::config::*;
pub use crate::customize::*;
pub(crate) use crate::header_whitelist::*;
pub(crate) use crate::interceptors::*;
pub use crate::keyrouting::{KeyRouteAffinityConfig, KeyRouteAffinityType};
pub(crate) use crate::live_nodes::*;
pub(crate) use crate::optimize_headers::*;
pub(crate) use crate::query_plan::*;
pub use crate::routing_scope::*;
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/http_content/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ mod driver_utils;

pub mod body_compression;
pub mod correct_line;
pub mod header_whitelist;
pub mod optimize_headers;
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ pub async fn test_without_credentials(ctx: &mut HttpTestContext<WithoutCredentia
.endpoint_url(format!("http://{}", ctx.get_proxy_address()))
.seed_hosts(Vec::<String>::new())
.behavior_version(aws_sdk_dynamodb::config::BehaviorVersion::latest())
.enforce_header_whitelist(true)
.optimize_headers(true)
.allow_no_auth()
.build(),
);
Expand Down Expand Up @@ -262,7 +262,7 @@ pub async fn test_with_credentials(ctx: &mut HttpTestContext<WithCredentialsConf
.endpoint_url(format!("http://{}", ctx.get_proxy_address()))
.seed_hosts(Vec::<String>::new())
.behavior_version(aws_sdk_dynamodb::config::BehaviorVersion::latest())
.enforce_header_whitelist(true)
.optimize_headers(true)
.credentials_provider(
aws_sdk_dynamodb::config::Credentials::for_tests_with_session_token(),
)
Expand Down Expand Up @@ -323,7 +323,7 @@ pub async fn test_whitelist_needed(ctx: &mut HttpTestContext<WhitelistNeededConf
.endpoint_url(format!("http://{}", ctx.get_proxy_address()))
.seed_hosts(Vec::<String>::new())
.behavior_version(aws_sdk_dynamodb::config::BehaviorVersion::latest())
.enforce_header_whitelist(false)
.optimize_headers(false)
.credentials_provider(
aws_sdk_dynamodb::config::Credentials::for_tests_with_session_token(),
)
Expand All @@ -348,8 +348,8 @@ async fn make_customized_calls(
) {
let client_strips_headers = client
.config()
.enforce_header_whitelist()
.expect("Enforce header whitelist not set while constructing client");
.optimize_headers()
.expect("optimize_headers not set while constructing client");

// in the first call we assert that we can customize an operation to override client's config
// proxy ensures that a whitelist is used (WithCredentialsConfig) or it isn't (WhitelistNeededConfig)
Expand Down Expand Up @@ -383,7 +383,7 @@ async fn make_customized_calls(
.billing_mode(BillingMode::PayPerRequest)
.customize()
.alternator_config_override(
AlternatorConfig::builder().enforce_header_whitelist(!client_strips_headers),
AlternatorConfig::builder().optimize_headers(!client_strips_headers),
)
.send()
.await
Expand Down Expand Up @@ -437,13 +437,13 @@ pub async fn test_enabled_by_per_request_customization(
.credentials_provider(
aws_sdk_dynamodb::config::Credentials::for_tests_with_session_token(),
)
.enforce_header_whitelist(false)
.optimize_headers(false)
.build(),
);

// perform 2 calls to alternator, use proxy to peek and forward requests
//
// first call overrides config's enforce_header_whitelist setting,
// first call overrides config's optimize_headers setting,
// then proxy checks if it is stripped according to WithCredentialsConfig whitelist
//
// second call does not override the config
Expand All @@ -465,13 +465,13 @@ pub async fn test_disabled_by_per_request_customization(
.credentials_provider(
aws_sdk_dynamodb::config::Credentials::for_tests_with_session_token(),
)
.enforce_header_whitelist(true)
.optimize_headers(true)
.build(),
);

// perform 2 calls to alternator, use proxy to peek and forward requests
//
// first call overrides config's enforce_header_whitelist setting,
// first call overrides config's optimize_headers setting,
// then proxy checks if request has not been stripped
//
// second call does not override the config
Expand Down