hubcio commented on code in PR #2933:
URL: https://github.com/apache/iggy/pull/2933#discussion_r2986413904


##########
core/connectors/sdk/src/retry.rs:
##########
@@ -0,0 +1,159 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+//! Shared retry and resilience utilities for connector implementations.
+//!
+//! Provides:
+//! - [`CircuitBreaker`] — consecutive-failure circuit breaker
+//! - [`parse_duration`] — humantime duration parsing with fallback
+//! - [`jitter`] — ±20 % random jitter for retry delays
+//! - [`exponential_backoff`] — capped exponential backoff
+//! - [`parse_retry_after`] — HTTP `Retry-After` header parsing
+
+use humantime::Duration as HumanDuration;
+use rand::RngExt as _;
+use std::str::FromStr;
+use std::time::Duration;
+use tokio::sync::Mutex;
+use tracing::{info, warn};
+
+// ---------------------------------------------------------------------------
+// Circuit breaker
+// ---------------------------------------------------------------------------
+
+#[derive(Debug)]
+struct CircuitState {
+    consecutive_failures: u32,
+    open_until: Option<tokio::time::Instant>,
+}
+
+/// A simple consecutive-failure circuit breaker.
+///
+/// All mutable state is held under a single [`Mutex`] so that
+/// `consecutive_failures` and `open_until` are always updated atomically,
+/// preventing races between concurrent `record_failure` / `is_open` callers.
+#[derive(Debug)]
+pub struct CircuitBreaker {
+    threshold: u32,
+    cool_down: Duration,
+    state: Mutex<CircuitState>,
+}
+
+impl CircuitBreaker {

Review Comment:
   37.87% patch coverage - every function here is pure or near-pure and 
trivially unit-testable. needs a `#[cfg(test)] mod tests` block covering: 
`CircuitBreaker` state transitions (closed -> open -> half-open -> closed), 
`exponential_backoff` scaling + saturation + max_delay clamping, `jitter` range 
bounds + small-duration no-op, `parse_duration` valid/invalid/None, 
`parse_retry_after` integer seconds + invalid input. ~25 tests, zero external 
deps.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to