Skip to content

Commit 5264df7

Browse files
refactor(Common): Complete transport layer refactoring to PascalCase naming
Major refactoring of the transport layer in the Common crate to align with PascalCase naming conventions per CLAUDE.md. All method names and struct fields renamed from snake_case to PascalCase (e.g., `send_request` -> `SendRequest`, `correlation_id` -> `CorrelationIdentifier`). Key changes: - TransportStrategy trait methods renamed to PascalCase - UnifiedRequest/UnifiedResponse fields and methods updated - TransportError restructured with renamed fields - TransportConfig builder methods converted to PascalCase - Added new transport components: CircuitBreaker, Retry, Metrics - TransportTypeDetector now requires Send + Sync with instance methods - Registry simplified to remove RwLock overhead Added new dependencies: uuid (v4), log, tokio (full), rand to support circuit breaker and retry logic. This refactoring establishes the foundation for improved transport resilience and consistency with Land's naming standards.
1 parent fd11100 commit 5264df7

21 files changed

+2115
-2544
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ thiserror = { workspace = true }
1818
url = { workspace = true }
1919

2020
# Transport-specific dependencies
21-
uuid = { workspace = true }
21+
uuid = { workspace = true, features = ["v4"] }
2222
futures = { workspace = true }
2323
anyhow = { workspace = true }
2424
prometheus = { workspace = true }
25+
log = { workspace = true }
26+
tokio = { workspace = true, features = ["full"] }
27+
rand = { workspace = true }
2528

2629
[features]
2730
default = []

Source/Transport/CircuitBreaker.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#![allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
2+
//! # Circuit Breaker Pattern
3+
//!
4+
//! Circuit breaker implementation for transport fault tolerance.
5+
//! `CircuitBreakerState` lives in [`super::TransportStrategy`].
6+
7+
use std::time::Duration;
8+
9+
/// Configuration for the circuit breaker.
10+
#[derive(Debug, Clone)]
11+
pub struct CircuitBreakerConfiguration {
12+
/// Number of consecutive failures before the circuit opens.
13+
pub FailureThreshold: u32,
14+
/// Duration to wait before transitioning to half-open.
15+
pub ResetTimeout: Duration,
16+
/// Successful requests in half-open state required to close the circuit.
17+
pub SuccessThreshold: u32,
18+
}
19+
20+
impl Default for CircuitBreakerConfiguration {
21+
fn default() -> Self {
22+
Self {
23+
FailureThreshold: 5,
24+
ResetTimeout: Duration::from_secs(60),
25+
SuccessThreshold: 2,
26+
}
27+
}
28+
}
29+
30+
/// Circuit breaker that wraps a transport to add fault-tolerance.
31+
///
32+
/// Tracks consecutive failures and opens the circuit when the
33+
/// `FailureThreshold` is exceeded, preventing cascading failures.
34+
#[derive(Debug, Clone)]
35+
pub struct CircuitBreaker {
36+
Configuration: CircuitBreakerConfiguration,
37+
FailureCount: u32,
38+
IsOpen: bool,
39+
}
40+
41+
impl CircuitBreaker {
42+
/// Creates a new circuit breaker with the given configuration.
43+
pub fn New(Configuration: CircuitBreakerConfiguration) -> Self {
44+
Self {
45+
Configuration,
46+
FailureCount: 0,
47+
IsOpen: false,
48+
}
49+
}
50+
51+
/// Returns `true` if the circuit allows requests through.
52+
pub fn IsClosed(&self) -> bool {
53+
!self.IsOpen
54+
}
55+
56+
/// Records a successful request, resetting the failure counter.
57+
pub fn RecordSuccess(&mut self) {
58+
self.FailureCount = 0;
59+
self.IsOpen = false;
60+
}
61+
62+
/// Records a failed request, potentially opening the circuit.
63+
pub fn RecordFailure(&mut self) {
64+
self.FailureCount += 1;
65+
if self.FailureCount >= self.Configuration.FailureThreshold {
66+
self.IsOpen = true;
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)