-
Notifications
You must be signed in to change notification settings - Fork 288
Refactored RecoverableConnection #2670
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
LarryOsterman
merged 5 commits into
Azure:main
from
LarryOsterman:larryo/update_token_refresh_jitter
Jun 16, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a3596bf
Convert jitter from seconds to duration
LarryOsterman 7b710db
Refactored recoverable connection into individual types, one per cate…
LarryOsterman 25e4597
Fixed local variable
LarryOsterman dd6cdb5
PR feedback 1
LarryOsterman 025cc1e
PR feedback 2
LarryOsterman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
sdk/eventhubs/azure_messaging_eventhubs/src/common/management.rs
This file contains hidden or 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 hidden or 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
123 changes: 123 additions & 0 deletions
123
sdk/eventhubs/azure_messaging_eventhubs/src/common/recoverable/claims_based_security.rs
This file contains hidden or 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,123 @@ | ||
// Copyright (c) Microsoft Corporation. All Rights reserved | ||
// Licensed under the MIT license. | ||
|
||
use super::RecoverableConnection; | ||
use crate::{common::retry_azure_operation, RetryOptions}; | ||
use azure_core::{credentials::Secret, error::ErrorKind as AzureErrorKind, error::Result}; | ||
use azure_core_amqp::{ | ||
AmqpClaimsBasedSecurity, AmqpClaimsBasedSecurityApis, AmqpConnection, AmqpError, AmqpSession, | ||
AmqpSessionApis, | ||
}; | ||
use std::error::Error; | ||
use std::sync::Arc; | ||
use tracing::{debug, warn}; | ||
|
||
/// Thin wrapper around the [`AmqpClaimsBasedSecurityApis`] trait that implements the retry functionality. | ||
/// | ||
/// A RecoverableClaimsBasedSecurity is a thin wrapper around the [`AmqpClaimsBasedSecurityApis`] trait which implements | ||
/// the retry functionality. That allows implementations which call into the authorize_path API to not have | ||
/// to worry about retrying the operation themselves. | ||
pub(crate) struct RecoverableClaimsBasedSecurity { | ||
recoverable_connection: Arc<RecoverableConnection>, | ||
} | ||
|
||
impl RecoverableClaimsBasedSecurity { | ||
/// Creates a new RecoverableClaimsBasedSecurity. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `recoverable_connection` - The recoverable connection to use for authorization. | ||
pub(super) fn new(recoverable_connection: Arc<RecoverableConnection>) -> Self { | ||
Self { | ||
recoverable_connection, | ||
} | ||
} | ||
|
||
pub(super) async fn create_claims_based_security( | ||
connection: Arc<AmqpConnection>, | ||
retry_options: &RetryOptions, | ||
) -> Result<Arc<AmqpClaimsBasedSecurity>> { | ||
retry_azure_operation( | ||
|| async { | ||
let session = AmqpSession::new(); | ||
session.begin(connection.as_ref(), None).await?; | ||
|
||
let claims_based_security = Arc::new(AmqpClaimsBasedSecurity::new(session)?); | ||
|
||
// Attach the claims_based_security client to the session. | ||
claims_based_security.attach().await?; | ||
Ok(claims_based_security) | ||
}, | ||
retry_options, | ||
Some(Self::should_retry_claims_based_security_response), | ||
) | ||
.await | ||
} | ||
|
||
fn should_retry_claims_based_security_response(e: &azure_core::Error) -> bool { | ||
match e.kind() { | ||
AzureErrorKind::Amqp => { | ||
warn!(err=?e, "Amqp operation failed: {:?}", e.source()); | ||
if let Some(e) = e.source() { | ||
debug!(err=?e, "Error: {e}"); | ||
|
||
if let Some(amqp_error) = e.downcast_ref::<Box<AmqpError>>() { | ||
RecoverableConnection::should_retry_amqp_error(amqp_error) | ||
} else if let Some(amqp_error) = e.downcast_ref::<AmqpError>() { | ||
RecoverableConnection::should_retry_amqp_error(amqp_error) | ||
} else { | ||
debug!(err=?e, "Non AMQP error: {e}"); | ||
false | ||
} | ||
} else { | ||
debug!("No source error found"); | ||
false | ||
} | ||
} | ||
_ => { | ||
debug!(err=?e, "Non AMQP error: {e}"); | ||
false | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] | ||
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] | ||
impl AmqpClaimsBasedSecurityApis for RecoverableClaimsBasedSecurity { | ||
async fn authorize_path( | ||
&self, | ||
path: String, | ||
token_type: Option<String>, | ||
secret: &Secret, | ||
expires_on: time::OffsetDateTime, | ||
) -> Result<()> { | ||
let result = retry_azure_operation( | ||
|| { | ||
let path = path.clone(); | ||
let token_type = token_type.clone(); | ||
let secret = secret.clone(); | ||
|
||
async move { | ||
let claims_based_security_client = | ||
self.recoverable_connection.ensure_amqp_cbs().await?; | ||
claims_based_security_client | ||
.authorize_path(path, token_type, &secret, expires_on) | ||
.await | ||
} | ||
}, | ||
&self.recoverable_connection.retry_options, | ||
Some(Self::should_retry_claims_based_security_response), | ||
) | ||
.await?; | ||
Ok(result) | ||
} | ||
|
||
async fn attach(&self) -> azure_core::Result<()> { | ||
unimplemented!("AmqpClaimsBasedSecurityClient does not support attach operation"); | ||
} | ||
|
||
async fn detach(self) -> azure_core::Result<()> { | ||
unimplemented!("AmqpClaimsBasedSecurityClient does not support detach operation"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.