Skip to content

PipelineContext removal #522

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

Merged
merged 1 commit into from
Nov 17, 2021
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
2 changes: 0 additions & 2 deletions sdk/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ mod models;
mod options;
pub mod parsing;
pub mod pipeline;
mod pipeline_context;
mod policies;
pub mod prelude;
mod request;
Expand All @@ -48,7 +47,6 @@ pub use http_client::{new_http_client, to_json, HttpClient};
pub use mock_transaction::constants::*;
pub use models::*;
pub use options::*;
pub use pipeline_context::PipelineContext;
pub use policies::{Policy, PolicyResult};
pub use request::*;
pub use response::*;
Expand Down
31 changes: 11 additions & 20 deletions sdk/core/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,18 @@ use std::time::Duration;
///
/// ```
/// use azure_core::{ClientOptions, RetryOptions, TelemetryOptions};
/// let options: ClientOptions<()> = ClientOptions::default()
/// let options: ClientOptions = ClientOptions::default()
/// .retry(RetryOptions::default().max_retries(10u32))
/// .telemetry(TelemetryOptions::default().application_id("my-application"));
/// ```
#[derive(Clone, Debug)]
pub struct ClientOptions<C>
where
C: Send + Sync,
{
pub struct ClientOptions {
// TODO: Expose transport override.
/// Policies called per call.
pub(crate) per_call_policies: Vec<Arc<dyn Policy<C>>>,
pub(crate) per_call_policies: Vec<Arc<dyn Policy>>,

/// Policies called per retry.
pub(crate) per_retry_policies: Vec<Arc<dyn Policy<C>>>,
pub(crate) per_retry_policies: Vec<Arc<dyn Policy>>,

/// Retry options.
pub(crate) retry: RetryOptions,
Expand All @@ -37,10 +34,7 @@ where
pub(crate) transport: TransportOptions,
}

impl<C> Default for ClientOptions<C>
where
C: Send + Sync,
{
impl Default for ClientOptions {
fn default() -> Self {
Self {
per_call_policies: Vec::new(),
Expand All @@ -52,10 +46,7 @@ where
}
}

impl<C> ClientOptions<C>
where
C: Send + Sync,
{
impl ClientOptions {
pub fn new() -> Self {
Self::default()
}
Expand All @@ -72,18 +63,18 @@ where
}

/// A mutable reference to per-call policies.
pub fn per_call_policies_mut(&mut self) -> &mut Vec<Arc<dyn Policy<C>>> {
pub fn per_call_policies_mut(&mut self) -> &mut Vec<Arc<dyn Policy>> {
&mut self.per_call_policies
}

/// A mutable reference to per-retry policies.
pub fn per_retry_policies_mut(&mut self) -> &mut Vec<Arc<dyn Policy<C>>> {
pub fn per_retry_policies_mut(&mut self) -> &mut Vec<Arc<dyn Policy>> {
&mut self.per_retry_policies
}

setters! {
per_call_policies: Vec<Arc<dyn Policy<C>>> => per_call_policies,
per_retry_policies: Vec<Arc<dyn Policy<C>>> => per_retry_policies,
per_call_policies: Vec<Arc<dyn Policy>> => per_call_policies,
per_retry_policies: Vec<Arc<dyn Policy>> => per_retry_policies,
retry: RetryOptions => retry,
telemetry: TelemetryOptions => telemetry,
transport: TransportOptions => transport,
Expand Down Expand Up @@ -157,7 +148,7 @@ impl Default for RetryOptions {
}

impl RetryOptions {
pub(crate) fn to_policy<C: Send + Sync>(&self) -> Arc<dyn Policy<C>> {
pub(crate) fn to_policy(&self) -> Arc<dyn Policy> {
match self.mode {
RetryMode::Exponential => Arc::new(ExponentialRetryPolicy::new(
self.delay,
Expand Down
38 changes: 12 additions & 26 deletions sdk/core/src/pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(not(target_arch = "wasm32"))]
use crate::policies::TransportPolicy;
use crate::policies::{CustomHeadersInjectorPolicy, Policy, TelemetryPolicy};
use crate::{ClientOptions, Error, HttpClient, PipelineContext, Request, Response};
use crate::{ClientOptions, Context, Error, HttpClient, Request, Response};
use std::sync::Arc;

/// Execution pipeline.
Expand Down Expand Up @@ -32,18 +32,12 @@ use std::sync::Arc;
/// context. For example, in CosmosDB, the generic carries the operation-specific information used by
/// the authorization policy.
#[derive(Debug, Clone)]
pub struct Pipeline<C>
where
C: Send + Sync,
{
pub struct Pipeline {
http_client: Arc<dyn HttpClient>,
pipeline: Vec<Arc<dyn Policy<C>>>,
pipeline: Vec<Arc<dyn Policy>>,
}

impl<C> Pipeline<C>
where
C: Send + Sync,
{
impl Pipeline {
/// Creates a new pipeline given the client library crate name and version,
/// alone with user-specified and client library-specified policies.
///
Expand All @@ -52,11 +46,11 @@ where
pub fn new(
crate_name: Option<&'static str>,
crate_version: Option<&'static str>,
options: ClientOptions<C>,
per_call_policies: Vec<Arc<dyn Policy<C>>>,
per_retry_policies: Vec<Arc<dyn Policy<C>>>,
options: ClientOptions,
per_call_policies: Vec<Arc<dyn Policy>>,
per_retry_policies: Vec<Arc<dyn Policy>>,
) -> Self {
let mut pipeline: Vec<Arc<dyn Policy<C>>> = Vec::with_capacity(
let mut pipeline: Vec<Arc<dyn Policy>> = Vec::with_capacity(
options.per_call_policies.len()
+ per_call_policies.len()
+ options.per_retry_policies.len()
Expand All @@ -83,7 +77,7 @@ where
#[cfg(not(target_arch = "wasm32"))]
{
#[allow(unused_mut)]
let mut policy: Arc<dyn Policy<_>> =
let mut policy: Arc<dyn Policy> =
Arc::new(TransportPolicy::new(options.transport.clone()));

// This code replaces the default transport policy at runtime if these two conditions
Expand Down Expand Up @@ -132,23 +126,15 @@ where
self.http_client.as_ref()
}

pub fn replace_policy(
&mut self,
policy: Arc<dyn Policy<C>>,
position: usize,
) -> Arc<dyn Policy<C>> {
pub fn replace_policy(&mut self, policy: Arc<dyn Policy>, position: usize) -> Arc<dyn Policy> {
std::mem::replace(&mut self.pipeline[position], policy)
}

pub fn policies(&self) -> &[Arc<dyn Policy<C>>] {
pub fn policies(&self) -> &[Arc<dyn Policy>] {
&self.pipeline
}

pub async fn send(
&self,
ctx: &mut PipelineContext<C>,
request: &mut Request,
) -> Result<Response, Error> {
pub async fn send(&self, ctx: &mut Context, request: &mut Request) -> Result<Response, Error> {
self.pipeline[0]
.send(ctx, request, &self.pipeline[1..])
.await
Expand Down
50 changes: 0 additions & 50 deletions sdk/core/src/pipeline_context.rs

This file was deleted.

14 changes: 5 additions & 9 deletions sdk/core/src/policies/custom_headers_injector_policy.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::policies::{Policy, PolicyResult};
use crate::{PipelineContext, Request, Response};
use crate::{Context, Request, Response};
use http::header::HeaderMap;
use std::sync::Arc;

Expand All @@ -16,18 +16,14 @@ impl From<HeaderMap> for CustomHeaders {
pub struct CustomHeadersInjectorPolicy {}

#[async_trait::async_trait]
impl<C> Policy<C> for CustomHeadersInjectorPolicy
where
C: Send + Sync,
{
impl Policy for CustomHeadersInjectorPolicy {
async fn send(
&self,
ctx: &mut PipelineContext<C>,
ctx: &mut Context,
request: &mut Request,
next: &[Arc<dyn Policy<C>>],
next: &[Arc<dyn Policy>],
) -> PolicyResult<Response> {
if let Some(CustomHeaders(custom_headers)) = ctx.get_inner_context().get::<CustomHeaders>()
{
if let Some(CustomHeaders(custom_headers)) = ctx.get::<CustomHeaders>() {
custom_headers
.iter()
.for_each(|(header_name, header_value)| {
Expand Down
15 changes: 5 additions & 10 deletions sdk/core/src/policies/mock_transport_player_policy.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::bytes_response::BytesResponse;
use crate::policies::{Policy, PolicyResult};
use crate::{MockFrameworkError, TransportOptions};
use crate::{PipelineContext, Request, Response};

use crate::mock_transaction::MockTransaction;
use crate::policies::{Policy, PolicyResult};
use crate::{Context, MockFrameworkError, Request, Response, TransportOptions};
use std::sync::Arc;

#[derive(Debug, Clone)]
Expand All @@ -23,15 +21,12 @@ impl MockTransportPlayerPolicy {
}

#[async_trait::async_trait]
impl<C> Policy<C> for MockTransportPlayerPolicy
where
C: Send + Sync,
{
impl Policy for MockTransportPlayerPolicy {
async fn send(
&self,
_ctx: &mut PipelineContext<C>,
_ctx: &mut Context,
request: &mut Request,
next: &[Arc<dyn Policy<C>>],
next: &[Arc<dyn Policy>],
) -> PolicyResult<Response> {
// there must be no more policies
assert_eq!(0, next.len());
Expand Down
13 changes: 4 additions & 9 deletions sdk/core/src/policies/mock_transport_recorder_policy.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::bytes_response::BytesResponse;
use crate::mock_transaction::MockTransaction;
use crate::policies::{Policy, PolicyResult};
use crate::{MockFrameworkError, TransportOptions};
use crate::{PipelineContext, Request, Response};

use crate::{Context, MockFrameworkError, Request, Response, TransportOptions};
use std::io::Write;
use std::sync::Arc;

Expand All @@ -24,15 +22,12 @@ impl MockTransportRecorderPolicy {
}

#[async_trait::async_trait]
impl<C> Policy<C> for MockTransportRecorderPolicy
where
C: Send + Sync,
{
impl Policy for MockTransportRecorderPolicy {
async fn send(
&self,
_ctx: &mut PipelineContext<C>,
_ctx: &mut Context,
request: &mut Request,
next: &[Arc<dyn Policy<C>>],
next: &[Arc<dyn Policy>],
) -> PolicyResult<Response> {
// there must be no more policies
assert_eq!(0, next.len());
Expand Down
11 changes: 4 additions & 7 deletions sdk/core/src/policies/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod retry_policies;
mod telemetry_policy;
mod transport;

use crate::{PipelineContext, Request, Response};
use crate::{Context, Request, Response};
pub use custom_headers_injector_policy::{CustomHeaders, CustomHeadersInjectorPolicy};
#[cfg(feature = "mock_transport_framework")]
pub use mock_transport_player_policy::MockTransportPlayerPolicy;
Expand All @@ -29,14 +29,11 @@ pub type PolicyResult<T> = Result<T, Box<dyn Error + Send + Sync>>;
/// the implementer to call the following policy.
/// The `C` generic represents the *contents* of the AuthorizationPolicy specific of this pipeline.
#[async_trait::async_trait]
pub trait Policy<C>: Send + Sync + std::fmt::Debug
where
C: Send + Sync,
{
pub trait Policy: Send + Sync + std::fmt::Debug {
async fn send(
&self,
ctx: &mut PipelineContext<C>,
ctx: &mut Context,
request: &mut Request,
next: &[Arc<dyn Policy<C>>],
next: &[Arc<dyn Policy>],
) -> PolicyResult<Response>;
}
11 changes: 4 additions & 7 deletions sdk/core/src/policies/retry_policies/no_retry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::policies::{Policy, PolicyResult, Request, Response};
use crate::PipelineContext;
use crate::Context;
use std::sync::Arc;

/// Retry policy that does not retry.
Expand All @@ -11,15 +11,12 @@ pub struct NoRetryPolicy {
}

#[async_trait::async_trait]
impl<C> Policy<C> for NoRetryPolicy
where
C: Send + Sync,
{
impl Policy for NoRetryPolicy {
async fn send(
&self,
ctx: &mut PipelineContext<C>,
ctx: &mut Context,
request: &mut Request,
next: &[Arc<dyn Policy<C>>],
next: &[Arc<dyn Policy>],
) -> PolicyResult<Response> {
// just call the following policies and bubble up the error
next[0].send(ctx, request, &next[1..]).await
Expand Down
Loading