diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..ff4773c --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,173 @@ +name: CI + +on: + push: + branches: [ main, develop ] + pull_request: + branches: [ main ] + +env: + CARGO_TERM_COLOR: always + RUST_BACKTRACE: 1 + +jobs: + test: + name: Test Suite + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + rust: [stable, nightly] + steps: + - uses: actions/checkout@v4 + + - name: Install Rust + uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ matrix.rust }} + + - name: Cache cargo registry + uses: actions/cache@v3 + with: + path: ~/.cargo/registry + key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} + + - name: Cache cargo index + uses: actions/cache@v3 + with: + path: ~/.cargo/git + key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} + + - name: Cache cargo build + uses: actions/cache@v3 + with: + path: target + key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }} + + - name: Run tests + run: cargo test --all --verbose + + - name: Run doc tests + run: cargo test --doc --verbose + + fmt: + name: Rustfmt + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + with: + components: rustfmt + + - name: Check formatting + run: cargo fmt --all -- --check + + clippy: + name: Clippy + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + with: + components: clippy + + - name: Run clippy + run: cargo clippy --all-features -- -D warnings + + audit: + name: Security Audit + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + + - name: Install cargo-audit + run: cargo install cargo-audit + + - name: Run security audit + run: cargo audit + + coverage: + name: Code Coverage + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + + - name: Install tarpaulin + run: cargo install cargo-tarpaulin + + - name: Generate coverage + run: cargo tarpaulin --out Xml --verbose + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v3 + with: + files: ./cobertura.xml + fail_ci_if_error: false + + build: + name: Build + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + steps: + - uses: actions/checkout@v4 + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + + - name: Build debug + run: cargo build --verbose + + - name: Build release + run: cargo build --release --verbose + + examples: + name: Build Examples + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + + - name: Build examples + run: cargo build --examples --verbose + + check-dependencies: + name: Check Dependencies + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + + - name: Install cargo-outdated + run: cargo install cargo-outdated + + - name: Check for outdated dependencies + run: cargo outdated --exit-code 1 || true + + benchmark: + name: Benchmark + runs-on: ubuntu-latest + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + steps: + - uses: actions/checkout@v4 + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + + - name: Run benchmarks + run: cargo bench --no-fail-fast || true diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..e24eaad --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,207 @@ +# Contributing to Rust Service Mesh + +Thank you for your interest in contributing to Rust Service Mesh! This document provides guidelines for contributing to the project. + +## Code of Conduct + +Be respectful, inclusive, and professional. We're all here to build great software together. + +## Getting Started + +1. **Fork the repository** on GitHub +2. **Clone your fork** locally: + ```bash + git clone https://github.com/YOUR_USERNAME/Rust-ServiceMesh.git + cd Rust-ServiceMesh + ``` +3. **Create a branch** for your changes: + ```bash + git checkout -b feature/my-awesome-feature + ``` + +## Development Workflow + +### Prerequisites + +- Rust 1.75 or later +- Cargo +- Git + +### Building + +```bash +# Debug build +cargo build + +# Release build +cargo build --release +``` + +### Testing + +All contributions must include tests and pass existing tests: + +```bash +# Run all tests +cargo test + +# Run tests for a specific module +cargo test circuit_breaker + +# Run with logging +RUST_LOG=debug cargo test + +# Run clippy (required) +cargo clippy --all-features -- -D warnings + +# Format code (required) +cargo fmt +``` + +### Code Quality Standards + +#### Rust Style +- Follow standard Rust conventions (enforced by `rustfmt`) +- Run `cargo fmt` before committing +- All code must pass `cargo clippy --all-features -- -D warnings` +- Use meaningful variable and function names +- Keep functions under 100 lines when possible + +#### Documentation +- Add `///` doc comments to all public items +- Include examples in doc comments for complex APIs +- Update README.md if adding user-facing features +- Doc tests should compile (`cargo test --doc`) + +#### Error Handling +- Use `Result` types, avoid panics in library code +- Provide context in error messages +- Use `thiserror` for error types + +#### Testing +- Write unit tests for all new functionality +- Add integration tests for end-to-end scenarios +- Aim for >80% code coverage +- Test error paths, not just happy paths + +#### Performance +- Profile performance-critical code +- Avoid unnecessary allocations +- Use `Arc` for shared state, avoid `Mutex` when possible +- Prefer lock-free atomics for counters + +## Pull Request Process + +1. **Ensure your code passes all checks**: + ```bash + cargo fmt --check + cargo clippy --all-features -- -D warnings + cargo test --all + cargo build --release + ``` + +2. **Update documentation**: + - Add/update doc comments + - Update README.md if needed + - Add examples if introducing new features + +3. **Write a clear PR description**: + - Explain what changes you made and why + - Reference any related issues + - Include before/after behavior if applicable + +4. **Commit message format**: + ``` + type: brief description + + Longer explanation if needed. + + Fixes #123 + ``` + + Types: `feat`, `fix`, `docs`, `refactor`, `test`, `perf`, `chore` + +5. **Submit the PR**: + - Push to your fork + - Open a PR against `main` + - Respond to review feedback + +## Areas for Contribution + +### High Priority +- [ ] Retry logic with exponential backoff +- [ ] Connection pooling in Transport module +- [ ] Rate limiting middleware +- [ ] Health checking for upstreams +- [ ] Additional integration tests + +### Medium Priority +- [ ] Distributed tracing (OpenTelemetry) +- [ ] Advanced load balancing algorithms +- [ ] L7 routing implementation +- [ ] HTTP/2 support +- [ ] Benchmarking suite + +### Low Priority +- [ ] mTLS support +- [ ] gRPC proxying +- [ ] WASM filter support +- [ ] Kubernetes sidecar mode + +## Architecture Guidelines + +### Module Organization +- Keep modules focused and single-purpose +- Use `pub(crate)` for internal APIs +- Expose minimal public surface area +- Group related functionality + +### Async/Await +- Use Tokio for async runtime +- Avoid blocking operations in async contexts +- Use `tokio::spawn` for CPU-intensive work +- Prefer `tokio::select!` over manual polling + +### Dependencies +- Justify new dependencies in your PR +- Prefer well-maintained crates +- Check licenses (Apache-2.0 or MIT compatible) +- Run `cargo audit` to check for vulnerabilities + +### Error Handling +```rust +// Good: Contextual errors +.map_err(|e| ProxyError::ListenerBind { + addr: addr.to_string(), + source: e, +})? + +// Bad: Generic errors +.map_err(|e| format!("Error: {}", e))? +``` + +### Logging +```rust +// Use tracing macros +use tracing::{debug, info, warn, error, instrument}; + +#[instrument(level = "debug", skip(self))] +async fn my_function(&self) { + info!("Starting operation"); + debug!(param = ?value, "Processing"); +} +``` + +## Questions? + +- Open an issue for bugs or feature requests +- Start a discussion for design questions +- Check existing issues before creating new ones + +## License + +By contributing, you agree that your contributions will be dual-licensed under both the MIT License and Apache License 2.0, at the user's option. + +--- + +Thank you for contributing to Rust Service Mesh! diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 0000000..fdb2b00 --- /dev/null +++ b/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2024 HueCodes + + Licensed 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. diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..5a5f0a2 --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 HueCodes + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/examples/basic_proxy.rs b/examples/basic_proxy.rs new file mode 100644 index 0000000..ce23120 --- /dev/null +++ b/examples/basic_proxy.rs @@ -0,0 +1,71 @@ +//! Basic proxy example demonstrating minimal setup. +//! +//! Run with: +//! ```bash +//! cargo run --example basic_proxy +//! ``` + +use rust_servicemesh::listener::Listener; +use std::sync::Arc; +use std::time::Duration; +use tokio::sync::broadcast; +use tracing::{error, info}; + +#[tokio::main] +async fn main() { + // Initialize logging + tracing_subscriber::fmt() + .with_env_filter( + tracing_subscriber::EnvFilter::try_from_default_env() + .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")), + ) + .init(); + + info!("Starting basic proxy example"); + + // Configure upstream servers + let upstream_addrs = Arc::new(vec![ + "http://httpbin.org".to_string(), + ]); + + // Configure request timeout + let timeout = Duration::from_secs(30); + + // Create listener + let listener = match Listener::bind("127.0.0.1:3000", upstream_addrs, timeout).await { + Ok(l) => l, + Err(e) => { + error!("Failed to bind listener: {}", e); + return; + } + }; + + let addr = listener.local_addr(); + info!("Proxy listening on http://{}", addr); + info!("Try: curl http://{}/get", addr); + + // Create shutdown channel + let (shutdown_tx, shutdown_rx) = broadcast::channel(1); + + // Spawn proxy server + tokio::spawn(async move { + if let Err(e) = listener.serve(shutdown_rx).await { + error!("Listener error: {}", e); + } + }); + + // Wait for Ctrl+C + match tokio::signal::ctrl_c().await { + Ok(()) => { + info!("Received Ctrl+C, shutting down"); + let _ = shutdown_tx.send(()); + } + Err(e) => { + error!("Failed to listen for Ctrl+C: {}", e); + } + } + + // Give tasks time to clean up + tokio::time::sleep(Duration::from_millis(100)).await; + info!("Shutdown complete"); +} diff --git a/examples/circuit_breaker_demo.rs b/examples/circuit_breaker_demo.rs new file mode 100644 index 0000000..d5739fb --- /dev/null +++ b/examples/circuit_breaker_demo.rs @@ -0,0 +1,126 @@ +//! Circuit breaker demonstration. +//! +//! Shows how the circuit breaker transitions between states based on failures and successes. +//! +//! Run with: +//! ```bash +//! cargo run --example circuit_breaker_demo +//! ``` + +use rust_servicemesh::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig, State}; +use std::time::Duration; +use tokio::time::sleep; +use tracing::{info, warn}; + +#[tokio::main] +async fn main() { + // Initialize logging + tracing_subscriber::fmt() + .with_env_filter(tracing_subscriber::EnvFilter::new("info")) + .init(); + + info!("Circuit Breaker Demonstration"); + info!("==============================\n"); + + // Configure circuit breaker + let config = CircuitBreakerConfig { + failure_threshold: 3, + timeout: Duration::from_secs(2), + success_threshold: 2, + }; + + info!("Configuration:"); + info!(" Failure threshold: {}", config.failure_threshold); + info!(" Timeout: {:?}", config.timeout); + info!(" Success threshold: {}\n", config.success_threshold); + + let cb = CircuitBreaker::new(config); + + // Scenario 1: Closed -> Open (failures) + info!("Scenario 1: Triggering circuit breaker with failures"); + info!("State: {:?}", cb.state().await); + + for i in 1..=3 { + if cb.allow_request().await { + info!(" Request #{} allowed", i); + simulate_request(false).await; + cb.record_failure().await; + info!(" Recorded failure"); + } + } + + info!("State: {:?}\n", cb.state().await); + assert_eq!(cb.state().await, State::Open); + + // Scenario 2: Open -> reject requests + info!("Scenario 2: Requests rejected while circuit is open"); + if cb.allow_request().await { + info!(" Request allowed (unexpected!)"); + } else { + warn!(" Request REJECTED - circuit is open"); + } + info!("State: {:?}\n", cb.state().await); + + // Scenario 3: Open -> HalfOpen (timeout) + info!("Scenario 3: Waiting for timeout to transition to HalfOpen"); + info!(" Sleeping for {:?}...", Duration::from_secs(2)); + sleep(Duration::from_secs(2)).await; + + if cb.allow_request().await { + info!(" Request allowed - circuit is now HalfOpen"); + } + info!("State: {:?}\n", cb.state().await); + assert_eq!(cb.state().await, State::HalfOpen); + + // Scenario 4: HalfOpen -> Closed (successes) + info!("Scenario 4: Recording successes to close the circuit"); + for i in 1..=2 { + if cb.allow_request().await { + info!(" Request #{} allowed", i); + simulate_request(true).await; + cb.record_success().await; + info!(" Recorded success"); + } + } + + info!("State: {:?}\n", cb.state().await); + assert_eq!(cb.state().await, State::Closed); + + // Scenario 5: HalfOpen -> Open (failure) + info!("Scenario 5: HalfOpen failure reopens circuit immediately"); + cb.reset().await; + + // Trigger open + for _ in 0..3 { + cb.allow_request().await; + cb.record_failure().await; + } + + sleep(Duration::from_secs(2)).await; + cb.allow_request().await; // Transition to HalfOpen + + info!("State before failure: {:?}", cb.state().await); + cb.record_failure().await; + info!("State after failure: {:?}\n", cb.state().await); + assert_eq!(cb.state().await, State::Open); + + // Statistics + info!("Final Statistics:"); + let stats = cb.stats(); + info!(" Total requests: {}", stats.total_requests); + info!(" Total failures: {}", stats.total_failures); + info!(" Failure rate: {:.1}%", + (stats.total_failures as f64 / stats.total_requests as f64) * 100.0); + + info!("\nDemo complete!"); +} + +/// Simulates a request with configurable success/failure. +async fn simulate_request(success: bool) { + sleep(Duration::from_millis(10)).await; + if success { + info!(" [Simulated request succeeded]"); + } else { + warn!(" [Simulated request failed]"); + } +} diff --git a/src/circuit_breaker.rs b/src/circuit_breaker.rs new file mode 100644 index 0000000..e0ae6da --- /dev/null +++ b/src/circuit_breaker.rs @@ -0,0 +1,316 @@ +//! Circuit breaker implementation for fault tolerance. +//! +//! Implements a Hystrix-style circuit breaker with three states: +//! - **Closed**: Normal operation, requests flow through +//! - **Open**: Too many failures, reject requests immediately +//! - **HalfOpen**: Recovery mode, allow limited requests to test if service recovered + +use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; +use std::sync::Arc; +use std::time::{Duration, Instant}; +use tokio::sync::RwLock; + +/// Circuit breaker state +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum State { + /// Circuit is closed, requests flow normally + Closed, + /// Circuit is open, requests are rejected + Open, + /// Circuit is half-open, testing if service recovered + HalfOpen, +} + +/// Configuration for the circuit breaker. +#[derive(Debug, Clone)] +pub struct CircuitBreakerConfig { + /// Number of failures before opening the circuit + pub failure_threshold: u64, + /// Duration to wait before transitioning from Open to HalfOpen + pub timeout: Duration, + /// Number of successful requests in HalfOpen before closing + pub success_threshold: u64, +} + +impl Default for CircuitBreakerConfig { + fn default() -> Self { + Self { + failure_threshold: 5, + timeout: Duration::from_secs(30), + success_threshold: 2, + } + } +} + +/// Circuit breaker for preventing cascading failures. +/// +/// # Example +/// +/// ``` +/// use rust_servicemesh::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig}; +/// +/// #[tokio::main] +/// async fn main() { +/// let config = CircuitBreakerConfig::default(); +/// let cb = CircuitBreaker::new(config); +/// +/// if cb.allow_request().await { +/// // Make request +/// match make_request().await { +/// Ok(_) => cb.record_success().await, +/// Err(_) => cb.record_failure().await, +/// } +/// } +/// } +/// +/// async fn make_request() -> Result<(), ()> { +/// Ok(()) +/// } +/// ``` +pub struct CircuitBreaker { + state: Arc>, + failure_count: Arc, + success_count: Arc, + last_failure_time: Arc>>, + config: CircuitBreakerConfig, + total_requests: Arc, + total_failures: Arc, +} + +impl CircuitBreaker { + /// Creates a new circuit breaker with the given configuration. + pub fn new(config: CircuitBreakerConfig) -> Self { + Self { + state: Arc::new(RwLock::new(State::Closed)), + failure_count: Arc::new(AtomicU64::new(0)), + success_count: Arc::new(AtomicU64::new(0)), + last_failure_time: Arc::new(RwLock::new(None)), + config, + total_requests: Arc::new(AtomicUsize::new(0)), + total_failures: Arc::new(AtomicUsize::new(0)), + } + } + + /// Checks if a request should be allowed through. + /// + /// Returns `true` if the request should proceed, `false` if it should be rejected. + pub async fn allow_request(&self) -> bool { + self.total_requests.fetch_add(1, Ordering::Relaxed); + + let state = *self.state.read().await; + + match state { + State::Closed => true, + State::Open => { + // Check if timeout has elapsed + let last_failure = self.last_failure_time.read().await; + if let Some(last_time) = *last_failure { + if last_time.elapsed() >= self.config.timeout { + drop(last_failure); + // Transition to HalfOpen + *self.state.write().await = State::HalfOpen; + self.success_count.store(0, Ordering::Relaxed); + true + } else { + false + } + } else { + false + } + } + State::HalfOpen => true, + } + } + + /// Records a successful request. + pub async fn record_success(&self) { + let state = *self.state.read().await; + + match state { + State::HalfOpen => { + let successes = self.success_count.fetch_add(1, Ordering::Relaxed) + 1; + if successes >= self.config.success_threshold { + // Transition to Closed + *self.state.write().await = State::Closed; + self.failure_count.store(0, Ordering::Relaxed); + self.success_count.store(0, Ordering::Relaxed); + } + } + State::Closed => { + // Reset failure count on success + self.failure_count.store(0, Ordering::Relaxed); + } + State::Open => {} + } + } + + /// Records a failed request. + pub async fn record_failure(&self) { + self.total_failures.fetch_add(1, Ordering::Relaxed); + + let state = *self.state.read().await; + + match state { + State::Closed => { + let failures = self.failure_count.fetch_add(1, Ordering::Relaxed) + 1; + if failures >= self.config.failure_threshold { + // Transition to Open + *self.state.write().await = State::Open; + *self.last_failure_time.write().await = Some(Instant::now()); + } + } + State::HalfOpen => { + // Immediately reopen on failure + *self.state.write().await = State::Open; + *self.last_failure_time.write().await = Some(Instant::now()); + self.failure_count.store(0, Ordering::Relaxed); + self.success_count.store(0, Ordering::Relaxed); + } + State::Open => { + *self.last_failure_time.write().await = Some(Instant::now()); + } + } + } + + /// Returns the current state of the circuit breaker. + pub async fn state(&self) -> State { + *self.state.read().await + } + + /// Returns statistics about the circuit breaker. + pub fn stats(&self) -> CircuitBreakerStats { + CircuitBreakerStats { + total_requests: self.total_requests.load(Ordering::Relaxed), + total_failures: self.total_failures.load(Ordering::Relaxed), + current_failure_count: self.failure_count.load(Ordering::Relaxed), + current_success_count: self.success_count.load(Ordering::Relaxed), + } + } + + /// Resets the circuit breaker to the closed state. + #[allow(dead_code)] + pub async fn reset(&self) { + *self.state.write().await = State::Closed; + self.failure_count.store(0, Ordering::Relaxed); + self.success_count.store(0, Ordering::Relaxed); + *self.last_failure_time.write().await = None; + } +} + +/// Statistics for the circuit breaker. +#[derive(Debug, Clone)] +pub struct CircuitBreakerStats { + pub total_requests: usize, + pub total_failures: usize, + pub current_failure_count: u64, + pub current_success_count: u64, +} + +#[cfg(test)] +mod tests { + use super::*; + use tokio::time::sleep; + + #[tokio::test] + async fn test_circuit_breaker_closed_to_open() { + let config = CircuitBreakerConfig { + failure_threshold: 3, + timeout: Duration::from_millis(100), + success_threshold: 2, + }; + let cb = CircuitBreaker::new(config); + + assert_eq!(cb.state().await, State::Closed); + assert!(cb.allow_request().await); + + // Record failures + cb.record_failure().await; + cb.record_failure().await; + cb.record_failure().await; + + assert_eq!(cb.state().await, State::Open); + assert!(!cb.allow_request().await); + } + + #[tokio::test] + async fn test_circuit_breaker_open_to_halfopen() { + let config = CircuitBreakerConfig { + failure_threshold: 2, + timeout: Duration::from_millis(50), + success_threshold: 2, + }; + let cb = CircuitBreaker::new(config); + + // Trigger open state + cb.record_failure().await; + cb.record_failure().await; + assert_eq!(cb.state().await, State::Open); + + // Wait for timeout + sleep(Duration::from_millis(60)).await; + + // Should transition to HalfOpen + assert!(cb.allow_request().await); + assert_eq!(cb.state().await, State::HalfOpen); + } + + #[tokio::test] + async fn test_circuit_breaker_halfopen_to_closed() { + let config = CircuitBreakerConfig { + failure_threshold: 2, + timeout: Duration::from_millis(50), + success_threshold: 2, + }; + let cb = CircuitBreaker::new(config); + + // Trigger open state + cb.record_failure().await; + cb.record_failure().await; + + // Wait for timeout and transition to HalfOpen + sleep(Duration::from_millis(60)).await; + assert!(cb.allow_request().await); + + // Record successes + cb.record_success().await; + cb.record_success().await; + + assert_eq!(cb.state().await, State::Closed); + } + + #[tokio::test] + async fn test_circuit_breaker_halfopen_to_open() { + let config = CircuitBreakerConfig { + failure_threshold: 2, + timeout: Duration::from_millis(50), + success_threshold: 2, + }; + let cb = CircuitBreaker::new(config); + + // Trigger open state + cb.record_failure().await; + cb.record_failure().await; + + // Wait for timeout and transition to HalfOpen + sleep(Duration::from_millis(60)).await; + assert!(cb.allow_request().await); + + // Record failure in HalfOpen - should reopen + cb.record_failure().await; + assert_eq!(cb.state().await, State::Open); + } + + #[tokio::test] + async fn test_circuit_breaker_stats() { + let config = CircuitBreakerConfig::default(); + let cb = CircuitBreaker::new(config); + + cb.allow_request().await; + cb.allow_request().await; + cb.record_failure().await; + + let stats = cb.stats(); + assert_eq!(stats.total_requests, 2); + assert_eq!(stats.total_failures, 1); + } +} diff --git a/src/lib.rs b/src/lib.rs index eae574b..c73c632 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ pub mod admin; pub mod admin_listener; +pub mod circuit_breaker; pub mod config; pub mod error; pub mod listener; diff --git a/src/listener.rs b/src/listener.rs index 778a1f8..3ae1736 100644 --- a/src/listener.rs +++ b/src/listener.rs @@ -9,6 +9,7 @@ use hyper::Request; use hyper_util::rt::TokioIo; use std::net::SocketAddr; use std::sync::Arc; +use std::time::Duration; use tokio::net::TcpListener; use tokio::sync::broadcast; use tower::Service; @@ -23,13 +24,15 @@ use tracing::{error, info, instrument, warn}; /// ```no_run /// use rust_servicemesh::listener::Listener; /// use std::sync::Arc; +/// use std::time::Duration; /// use tokio::sync::broadcast; /// /// #[tokio::main] /// async fn main() -> Result<(), Box> { /// let (shutdown_tx, _) = broadcast::channel(1); /// let upstream = vec!["http://127.0.0.1:8080".to_string()]; -/// let listener = Listener::bind("127.0.0.1:3000", Arc::new(upstream)).await?; +/// let timeout = Duration::from_secs(30); +/// let listener = Listener::bind("127.0.0.1:3000", Arc::new(upstream), timeout).await?; /// listener.serve(shutdown_tx.subscribe()).await?; /// Ok(()) /// } @@ -47,12 +50,17 @@ impl Listener { /// /// * `addr` - Address to bind to (e.g., "127.0.0.1:3000") /// * `upstream_addrs` - List of upstream server addresses + /// * `request_timeout` - Maximum duration for upstream requests /// /// # Errors /// /// Returns `ProxyError::ListenerBind` if binding fails. #[instrument(level = "info", skip(upstream_addrs))] - pub async fn bind(addr: &str, upstream_addrs: Arc>) -> Result { + pub async fn bind( + addr: &str, + upstream_addrs: Arc>, + request_timeout: Duration, + ) -> Result { let tcp_listener = TcpListener::bind(addr) .await .map_err(|e| ProxyError::ListenerBind { @@ -71,7 +79,7 @@ impl Listener { Ok(Self { tcp_listener, - proxy_service: ProxyService::new(upstream_addrs), + proxy_service: ProxyService::new(upstream_addrs, request_timeout), addr: local_addr, }) } @@ -145,14 +153,16 @@ mod tests { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_listener_bind() { let upstream = Arc::new(vec!["http://127.0.0.1:9999".to_string()]); - let listener = Listener::bind("127.0.0.1:0", upstream).await; + let timeout = Duration::from_secs(30); + let listener = Listener::bind("127.0.0.1:0", upstream, timeout).await; assert!(listener.is_ok()); } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_listener_bind_invalid_address() { let upstream = Arc::new(vec!["http://127.0.0.1:9999".to_string()]); - let listener = Listener::bind("999.999.999.999:0", upstream).await; + let timeout = Duration::from_secs(30); + let listener = Listener::bind("999.999.999.999:0", upstream, timeout).await; assert!(listener.is_err()); } } diff --git a/src/main.rs b/src/main.rs index 2a2d1a8..e87574e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod admin; mod admin_listener; +mod circuit_breaker; mod config; mod error; mod listener; @@ -43,7 +44,12 @@ async fn run() -> Result<(), Box> { let (shutdown_tx, _shutdown_rx) = broadcast::channel(1); - let proxy_listener = Listener::bind(&config.listen_addr, config.upstream_addrs_arc()).await?; + let proxy_listener = Listener::bind( + &config.listen_addr, + config.upstream_addrs_arc(), + config.request_timeout, + ) + .await?; let proxy_addr = proxy_listener.local_addr(); info!("proxy listening on {}", proxy_addr); diff --git a/src/service.rs b/src/service.rs index cff96d5..d0778f5 100644 --- a/src/service.rs +++ b/src/service.rs @@ -12,7 +12,8 @@ use std::future::Future; use std::pin::Pin; use std::sync::Arc; use std::task::{Context, Poll}; -use std::time::Instant; +use std::time::{Duration, Instant}; +use tokio::time::timeout; use tower::Service; use tracing::{debug, info, instrument, warn}; @@ -25,11 +26,13 @@ use tracing::{debug, info, instrument, warn}; /// ```no_run /// use rust_servicemesh::service::ProxyService; /// use std::sync::Arc; +/// use std::time::Duration; /// /// #[tokio::main] /// async fn main() { /// let upstream = "http://example.com:8080".to_string(); -/// let service = ProxyService::new(Arc::new(vec![upstream])); +/// let timeout = Duration::from_secs(30); +/// let service = ProxyService::new(Arc::new(vec![upstream]), timeout); /// } /// ``` #[derive(Clone)] @@ -37,6 +40,7 @@ pub struct ProxyService { upstream_addrs: Arc>, client: Client, next_upstream: Arc, + request_timeout: Duration, } impl ProxyService { @@ -45,12 +49,14 @@ impl ProxyService { /// # Arguments /// /// * `upstream_addrs` - List of upstream server addresses (e.g., "http://127.0.0.1:8080") - pub fn new(upstream_addrs: Arc>) -> Self { + /// * `request_timeout` - Maximum duration for upstream requests + pub fn new(upstream_addrs: Arc>, request_timeout: Duration) -> Self { let client = Client::builder(TokioExecutor::new()).build_http(); Self { upstream_addrs, client, next_upstream: Arc::new(std::sync::atomic::AtomicUsize::new(0)), + request_timeout, } } @@ -97,8 +103,8 @@ impl ProxyService { debug!("forwarding to upstream: {}", upstream_uri); *req.uri_mut() = upstream_uri; - match self.client.request(req).await { - Ok(response) => { + match timeout(self.request_timeout, self.client.request(req)).await { + Ok(Ok(response)) => { let status = response.status().as_u16(); let duration = start.elapsed().as_secs_f64(); @@ -116,7 +122,7 @@ impl ProxyService { let boxed_body = body.boxed(); Ok(Response::from_parts(parts, boxed_body)) } - Err(e) => { + Ok(Err(e)) => { warn!("upstream request failed: {}", e); let duration = start.elapsed().as_secs_f64(); Metrics::record_request(&method, 502, &upstream_owned, duration); @@ -125,6 +131,15 @@ impl ProxyService { "Upstream request failed", )) } + Err(_) => { + warn!("upstream request timed out after {:?}", self.request_timeout); + let duration = start.elapsed().as_secs_f64(); + Metrics::record_request(&method, 504, &upstream_owned, duration); + Ok(Self::error_response( + StatusCode::GATEWAY_TIMEOUT, + "Upstream request timed out", + )) + } } } diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 6f7a9b9..86b06c4 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -8,6 +8,7 @@ use hyper_util::rt::TokioExecutor; use hyper_util::rt::TokioIo; use std::convert::Infallible; use std::sync::Arc; +use std::time::Duration; use tokio::net::TcpListener; use tokio::sync::broadcast; @@ -18,6 +19,15 @@ async fn mock_upstream_handler(_req: Request) -> Result) -> Result, Infallible> { + // Simulate a slow upstream that takes longer than the timeout + tokio::time::sleep(Duration::from_secs(10)).await; + Ok(Response::builder() + .status(StatusCode::OK) + .body("slow response".to_string()) + .unwrap()) +} + async fn start_mock_upstream() -> String { let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); let addr = listener.local_addr().unwrap(); @@ -40,12 +50,35 @@ async fn start_mock_upstream() -> String { format!("http://127.0.0.1:{}", addr.port()) } +async fn start_slow_upstream() -> String { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + + tokio::spawn(async move { + loop { + let (stream, _) = match listener.accept().await { + Ok(conn) => conn, + Err(_) => break, + }; + + tokio::spawn(async move { + let io = TokioIo::new(stream); + let service = service_fn(slow_upstream_handler); + let _ = http1::Builder::new().serve_connection(io, service).await; + }); + } + }); + + format!("http://127.0.0.1:{}", addr.port()) +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_proxy_basic_request() { let upstream_addr = start_mock_upstream().await; let upstream_addrs = Arc::new(vec![upstream_addr]); + let timeout = Duration::from_secs(30); - let listener = rust_servicemesh::listener::Listener::bind("127.0.0.1:0", upstream_addrs) + let listener = rust_servicemesh::listener::Listener::bind("127.0.0.1:0", upstream_addrs, timeout) .await .unwrap(); @@ -76,8 +109,9 @@ async fn test_proxy_round_robin() { let upstream1 = start_mock_upstream().await; let upstream2 = start_mock_upstream().await; let upstream_addrs = Arc::new(vec![upstream1, upstream2]); + let timeout = Duration::from_secs(30); - let listener = rust_servicemesh::listener::Listener::bind("127.0.0.1:0", upstream_addrs) + let listener = rust_servicemesh::listener::Listener::bind("127.0.0.1:0", upstream_addrs, timeout) .await .unwrap(); @@ -104,3 +138,46 @@ async fn test_proxy_round_robin() { let _ = shutdown_tx.send(()); } + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_proxy_timeout_enforcement() { + // Start a slow upstream that takes 10 seconds to respond + let slow_upstream = start_slow_upstream().await; + let upstream_addrs = Arc::new(vec![slow_upstream]); + + // Set a short timeout (1 second) + let timeout = Duration::from_secs(1); + + let listener = rust_servicemesh::listener::Listener::bind("127.0.0.1:0", upstream_addrs, timeout) + .await + .unwrap(); + + let proxy_addr = listener.local_addr(); + let (shutdown_tx, shutdown_rx) = broadcast::channel(1); + + tokio::spawn(async move { + let _ = listener.serve(shutdown_rx).await; + }); + + tokio::time::sleep(tokio::time::Duration::from_millis(100)).await; + + let client: Client<_, Empty> = Client::builder(TokioExecutor::new()).build_http(); + let uri = format!("http://{}/test", proxy_addr); + + let start = std::time::Instant::now(); + let req = Request::builder() + .uri(uri) + .body(Empty::::new()) + .unwrap(); + let response = client.request(req).await.unwrap(); + let elapsed = start.elapsed(); + + // Should get 504 Gateway Timeout + assert_eq!(response.status(), StatusCode::GATEWAY_TIMEOUT); + + // Should timeout in approximately 1 second, not 10 + assert!(elapsed < Duration::from_secs(2), "Request should timeout quickly"); + assert!(elapsed >= Duration::from_secs(1), "Request should wait for timeout"); + + let _ = shutdown_tx.send(()); +}