style(client): cargo fmt
This commit is contained in:
+32
-35
@@ -132,10 +132,7 @@ impl Default for HttpClientConfig {
|
|||||||
connect_timeout: Some(DEFAULT_CONNECT_TIMEOUT),
|
connect_timeout: Some(DEFAULT_CONNECT_TIMEOUT),
|
||||||
read_timeout: Some(DEFAULT_READ_TIMEOUT),
|
read_timeout: Some(DEFAULT_READ_TIMEOUT),
|
||||||
retry_policy: ExponentialBackoff::builder()
|
retry_policy: ExponentialBackoff::builder()
|
||||||
.retry_bounds(
|
.retry_bounds(RETRY_BACKOFF_MIN_INTERVAL, RETRY_BACKOFF_MAX_INTERVAL)
|
||||||
RETRY_BACKOFF_MIN_INTERVAL,
|
|
||||||
RETRY_BACKOFF_MAX_INTERVAL,
|
|
||||||
)
|
|
||||||
.jitter(Jitter::Bounded)
|
.jitter(Jitter::Bounded)
|
||||||
.base(2)
|
.base(2)
|
||||||
.build_with_max_retries(3),
|
.build_with_max_retries(3),
|
||||||
@@ -263,10 +260,7 @@ struct RetryGateMiddleware {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl RetryGateMiddleware {
|
impl RetryGateMiddleware {
|
||||||
fn new(
|
fn new(policy: ExponentialBackoff, max_total_retry_duration: Duration) -> Self {
|
||||||
policy: ExponentialBackoff,
|
|
||||||
max_total_retry_duration: Duration,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
retry: Arc::new(RetryTransientMiddleware::new_with_policy(
|
retry: Arc::new(RetryTransientMiddleware::new_with_policy(
|
||||||
TotalRetryBudget {
|
TotalRetryBudget {
|
||||||
@@ -325,30 +319,28 @@ async fn build_client(
|
|||||||
config: &HttpClientConfig,
|
config: &HttpClientConfig,
|
||||||
) -> Result<ClientWithMiddleware, HttpClientBuildError> {
|
) -> Result<ClientWithMiddleware, HttpClientBuildError> {
|
||||||
let ca_pem = match &config.ca_bundle {
|
let ca_pem = match &config.ca_bundle {
|
||||||
Some(path) => Some(
|
Some(path) => Some(tokio::fs::read(path).await.map_err(|source| {
|
||||||
tokio::fs::read(path)
|
HttpClientBuildError::CaBundleRead {
|
||||||
.await
|
path: path.clone(),
|
||||||
.map_err(|source| HttpClientBuildError::CaBundleRead {
|
source,
|
||||||
path: path.clone(),
|
}
|
||||||
source,
|
})?),
|
||||||
})?,
|
|
||||||
),
|
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
let client_pems = match &config.client_cert {
|
let client_pems = match &config.client_cert {
|
||||||
Some(cfg) => {
|
Some(cfg) => {
|
||||||
let cert_pem = tokio::fs::read(&cfg.cert_pem)
|
let cert_pem = tokio::fs::read(&cfg.cert_pem).await.map_err(|source| {
|
||||||
.await
|
HttpClientBuildError::ClientCertRead {
|
||||||
.map_err(|source| HttpClientBuildError::ClientCertRead {
|
|
||||||
path: cfg.cert_pem.clone(),
|
path: cfg.cert_pem.clone(),
|
||||||
source,
|
source,
|
||||||
})?;
|
}
|
||||||
let key_pem = tokio::fs::read(&cfg.key_pem)
|
})?;
|
||||||
.await
|
let key_pem = tokio::fs::read(&cfg.key_pem).await.map_err(|source| {
|
||||||
.map_err(|source| HttpClientBuildError::ClientCertRead {
|
HttpClientBuildError::ClientCertRead {
|
||||||
path: cfg.key_pem.clone(),
|
path: cfg.key_pem.clone(),
|
||||||
source,
|
source,
|
||||||
})?;
|
}
|
||||||
|
})?;
|
||||||
Some((cert_pem, key_pem))
|
Some((cert_pem, key_pem))
|
||||||
}
|
}
|
||||||
None => None,
|
None => None,
|
||||||
@@ -360,12 +352,14 @@ fn build_client_sync(
|
|||||||
config: &HttpClientConfig,
|
config: &HttpClientConfig,
|
||||||
) -> Result<ClientWithMiddleware, HttpClientBuildError> {
|
) -> Result<ClientWithMiddleware, HttpClientBuildError> {
|
||||||
let ca_pem = match &config.ca_bundle {
|
let ca_pem = match &config.ca_bundle {
|
||||||
Some(path) => Some(std::fs::read(path).map_err(|source| {
|
Some(path) => {
|
||||||
HttpClientBuildError::CaBundleRead {
|
Some(
|
||||||
path: path.clone(),
|
std::fs::read(path).map_err(|source| HttpClientBuildError::CaBundleRead {
|
||||||
source,
|
path: path.clone(),
|
||||||
}
|
source,
|
||||||
})?),
|
})?,
|
||||||
|
)
|
||||||
|
}
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
let client_pems = match &config.client_cert {
|
let client_pems = match &config.client_cert {
|
||||||
@@ -464,7 +458,6 @@ mod tests {
|
|||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
|
|
||||||
|
|
||||||
fn minimal_config() -> HttpClientConfig {
|
fn minimal_config() -> HttpClientConfig {
|
||||||
HttpClientConfig {
|
HttpClientConfig {
|
||||||
pool_max_idle_per_host: Some(8),
|
pool_max_idle_per_host: Some(8),
|
||||||
@@ -498,7 +491,9 @@ mod tests {
|
|||||||
client_cert: None,
|
client_cert: None,
|
||||||
..minimal_config()
|
..minimal_config()
|
||||||
};
|
};
|
||||||
http.reload(new_config.clone()).await.expect("reload succeeds");
|
http.reload(new_config.clone())
|
||||||
|
.await
|
||||||
|
.expect("reload succeeds");
|
||||||
let after = http.client();
|
let after = http.client();
|
||||||
assert!(
|
assert!(
|
||||||
!Arc::ptr_eq(&before, &after),
|
!Arc::ptr_eq(&before, &after),
|
||||||
@@ -526,7 +521,10 @@ mod tests {
|
|||||||
assert_eq!(config.max_total_retry_duration, Duration::from_secs(10));
|
assert_eq!(config.max_total_retry_duration, Duration::from_secs(10));
|
||||||
assert_eq!(config.retry_after_ceiling, Duration::from_secs(300));
|
assert_eq!(config.retry_after_ceiling, Duration::from_secs(300));
|
||||||
assert_eq!(config.retry_policy.max_n_retries, Some(3));
|
assert_eq!(config.retry_policy.max_n_retries, Some(3));
|
||||||
assert_eq!(config.retry_policy.max_retry_interval, Duration::from_secs(2));
|
assert_eq!(
|
||||||
|
config.retry_policy.max_retry_interval,
|
||||||
|
Duration::from_secs(2)
|
||||||
|
);
|
||||||
assert!(config.ca_bundle.is_none());
|
assert!(config.ca_bundle.is_none());
|
||||||
assert!(config.client_cert.is_none());
|
assert!(config.client_cert.is_none());
|
||||||
}
|
}
|
||||||
@@ -786,8 +784,7 @@ mod tests {
|
|||||||
let request = String::from_utf8_lossy(&buf[..n]);
|
let request = String::from_utf8_lossy(&buf[..n]);
|
||||||
if request.contains("GET /final") {
|
if request.contains("GET /final") {
|
||||||
hits.fetch_add(1, Ordering::SeqCst);
|
hits.fetch_add(1, Ordering::SeqCst);
|
||||||
let response =
|
let response = "HTTP/1.1 200 OK\r\ncontent-length: 2\r\n\r\nok";
|
||||||
"HTTP/1.1 200 OK\r\ncontent-length: 2\r\n\r\nok";
|
|
||||||
let _ = sock.write_all(response.as_bytes()).await;
|
let _ = sock.write_all(response.as_bytes()).await;
|
||||||
} else {
|
} else {
|
||||||
let response = format!(
|
let response = format!(
|
||||||
|
|||||||
@@ -291,7 +291,9 @@ mod tests {
|
|||||||
let target = url("https://api.example.com/v1/chat");
|
let target = url("https://api.example.com/v1/chat");
|
||||||
let response = synthetic_response(StatusCode::TOO_MANY_REQUESTS, Some("3600"));
|
let response = synthetic_response(StatusCode::TOO_MANY_REQUESTS, Some("3600"));
|
||||||
mw.record_if_throttled(target.clone(), &response);
|
mw.record_if_throttled(target.clone(), &response);
|
||||||
let deadline = mw.deadline_for_test(&target).expect("clamped deadline kept");
|
let deadline = mw
|
||||||
|
.deadline_for_test(&target)
|
||||||
|
.expect("clamped deadline kept");
|
||||||
let upper = SystemTime::now() + Duration::from_secs(5);
|
let upper = SystemTime::now() + Duration::from_secs(5);
|
||||||
assert!(deadline <= upper, "custom ceiling must be honored");
|
assert!(deadline <= upper, "custom ceiling must be honored");
|
||||||
assert!(deadline > SystemTime::now());
|
assert!(deadline > SystemTime::now());
|
||||||
@@ -420,8 +422,9 @@ mod tests {
|
|||||||
let elapsed = SystemTime::now().duration_since(started).unwrap();
|
let elapsed = SystemTime::now().duration_since(started).unwrap();
|
||||||
let max_jitter = max_sleep_jitter(remaining);
|
let max_jitter = max_sleep_jitter(remaining);
|
||||||
assert!(
|
assert!(
|
||||||
elapsed <= remaining.checked_sub(max_jitter).unwrap_or(remaining)
|
elapsed
|
||||||
+ Duration::from_millis(50),
|
<= remaining.checked_sub(max_jitter).unwrap_or(remaining)
|
||||||
|
+ Duration::from_millis(50),
|
||||||
"wake must happen roughly a jitter-slice before the deadline, took {elapsed:?}"
|
"wake must happen roughly a jitter-slice before the deadline, took {elapsed:?}"
|
||||||
);
|
);
|
||||||
assert!(max_jitter > Duration::ZERO, "jitter must be non-zero");
|
assert!(max_jitter > Duration::ZERO, "jitter must be non-zero");
|
||||||
@@ -438,4 +441,4 @@ mod tests {
|
|||||||
"jitter is capped at 2 s even for long waits"
|
"jitter is capped at 2 s even for long waits"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user