Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
77b0dd9f12 | ||
|
|
74abe7172f | ||
|
|
3c64563a44 | ||
|
|
2f445d3b85 | ||
|
|
2985fa0a0e | ||
|
|
7a063fa75c | ||
|
|
ebb3c8ad6b | ||
|
|
076b0bb757 | ||
|
|
8e66c7d887 | ||
|
|
851c8c4ebb | ||
|
|
43063c4698 | ||
|
|
a03db38c5c | ||
|
|
f9df8f2887 | ||
|
|
97b5e02820 | ||
|
|
bf8f76cc10 | ||
|
|
510aadc066 |
@@ -0,0 +1,29 @@
|
|||||||
|
# Implementation Spec: Round 10 — COMPLETE
|
||||||
|
|
||||||
|
## Goal
|
||||||
|
Auto-integration + ergonomics: rate-limit workers, auto-metrics on service calls —
|
||||||
|
reduce manual wiring/boilerplate.
|
||||||
|
|
||||||
|
## New Features
|
||||||
|
|
||||||
|
### 1. AutoMetricsServiceBuilder (mytheclipse-core, observability)
|
||||||
|
File: `crates/mytheclipse/src/auto_metrics_service.rs`
|
||||||
|
- Composes ServiceBuilder + MetricsCollector (+ MetricsBridge when resiliency)
|
||||||
|
- `.run()` auto-records: calls_total counter (labelled by outcome ok/err/timeout/
|
||||||
|
circuit_open/rate_limited) + duration histogram; emits bridge when attached
|
||||||
|
- Chainable .with_collector/.with_bridge/.with_builders
|
||||||
|
- 1 test
|
||||||
|
|
||||||
|
### 2. RateLimitedWorkerPool (mytheclipse-queue, in-memory)
|
||||||
|
File: `crates/mytheclipse-queue/src/worker_rate_limited.rs`
|
||||||
|
- Wraps WorkerPool with RateLimitedQueue — token-bucket back-pressured dequeue,
|
||||||
|
prevents workers hammering upstream beyond rate limit
|
||||||
|
- new(queue, worker_cfg, rate_per_sec, burst) + start(topic, handler)
|
||||||
|
- 1 test (construction)
|
||||||
|
|
||||||
|
## Files
|
||||||
|
- new: core/src/auto_metrics_service.rs, queue/src/worker_rate_limited.rs
|
||||||
|
- core/lib.rs: +module+export AutoMetricsServiceBuilder
|
||||||
|
- queue/lib.rs: +module+export RateLimitedWorkerPool (rewrote export block)
|
||||||
|
|
||||||
|
Build: exit 0. Tests: 0 FAILED (86 core pass). Clippy: 0 new warnings.
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
# Implementation Spec: Round 11 — COMPLETE
|
||||||
|
|
||||||
|
## Goal
|
||||||
|
Auto thread/core allocation + race hardening (RAII shutdown).
|
||||||
|
|
||||||
|
## New Features
|
||||||
|
|
||||||
|
### 1. RuntimeConfig (mytheclipse-core, lifecycle)
|
||||||
|
File: `crates/mytheclipse/src/runtime_auto.rs`
|
||||||
|
- `RuntimeConfig::auto()` / `from_cores(n)` / `compact()` infer worker_threads,
|
||||||
|
max_blocking_threads, compute_threads, io_threads from host CPU topology
|
||||||
|
(std::thread::available_parallelism)
|
||||||
|
- `available_parallelism()` helper
|
||||||
|
- `build_rayon_pool(cfg)` gated on `compute` feature
|
||||||
|
- 3 tests
|
||||||
|
|
||||||
|
### 2. ShutdownGuard (mytheclipse-core, lifecycle)
|
||||||
|
File: `crates/mytheclipse/src/shutdown_guard.rs`
|
||||||
|
- RAII guard — runs completion callback exactly once on drop (panic-safe via
|
||||||
|
Mutex<Option<Box<FnOnce>>>), prevents double-shutdown race
|
||||||
|
- `new(cb)` + `finish()` (fire now + disarm)
|
||||||
|
- 3 tests (fires on drop, finish once, panic path)
|
||||||
|
|
||||||
|
## Files
|
||||||
|
- new: core/src/runtime_auto.rs, core/src/shutdown_guard.rs
|
||||||
|
- core/lib.rs: +module+export for both
|
||||||
|
|
||||||
|
Build: exit 0. Tests: 0 FAILED. Clippy: 0 new warnings.
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
# Implementation Spec: Round 5
|
||||||
|
|
||||||
|
## Status: COMPLETE
|
||||||
|
|
||||||
|
## New Features
|
||||||
|
|
||||||
|
### 1. CircuitBreakerHealthCheck (mytheclipse-core, observability+resiliency)
|
||||||
|
- `CircuitBreakerHealthCheck` di metrics_bridge.rs — HealthCheck impl yang memetakan CircuitBreaker snapshot state → HealthStatus (Open→Unhealthy, HalfOpen→Degraded, Closed→Ok)
|
||||||
|
- Gated `#[cfg(feature="resiliency")]`; re-export gated `#[cfg(all(observability, resiliency))]`
|
||||||
|
- `observability` feature now implies `lifecycle` (needed for crate::health module access)
|
||||||
|
|
||||||
|
### 2. TypedKeyRegistry (mytheclipse-crypto, password)
|
||||||
|
- `TypedKeyRegistry<K,V>` di key_registry.rs — ID-based key lookup + rotation + revoke, wraps KeyRing
|
||||||
|
- `key_for(id) -> Option<&K>`, `rotate_with_id(id, key)`, `revoke(id)`
|
||||||
|
|
||||||
|
### 3. MetricsHttpHandler (mytheclipse-http, metrics-http)
|
||||||
|
- new feature `metrics-http` (axum + tower + mytheclipse/observability)
|
||||||
|
- `metrics_routes(collector)` → Router serving /metrics (Prometheus text) + /
|
||||||
|
- added tower dep (util), ServiceExt import in test module
|
||||||
|
- 1 test via ServiceExt::oneshot
|
||||||
|
|
||||||
|
### 4. BatchProcessor (mytheclipse-queue, in-memory)
|
||||||
|
- `BatchJobHandler` trait — handle Vec<Job> atomically
|
||||||
|
- `BatchConfig` { batch_size, batch_timeout, concurrency }
|
||||||
|
- `BatchProcessor<Q>` — accumulates jobs per topic, flushes on size/timeout
|
||||||
|
- 2 tests: flush_on_batch_size, flush_on_timeout
|
||||||
|
|
||||||
|
## Verification
|
||||||
|
- cargo build --workspace --all-features → exit 0
|
||||||
|
- cargo test --workspace --all-features → all pass (160+ tests)
|
||||||
|
- cargo clippy --workspace --all-features → no new warnings
|
||||||
|
- commit + push: f02a1ce
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
# Implementation Spec: Round 6
|
||||||
|
|
||||||
|
## New Features
|
||||||
|
|
||||||
|
### 1. HealthCheckedPool (mytheclipse-core, observability+traffic)
|
||||||
|
File: `crates/mytheclipse/src/pool_health.rs`
|
||||||
|
- `HealthCheckedPool<T>` — wraps `SemaphorePool<T>`, integrates `HealthRegistry`
|
||||||
|
- `check_connection(&self) -> HealthStatus` — validates pooled resource
|
||||||
|
- auto-registers health check at construction
|
||||||
|
- gated feature observability+traffic
|
||||||
|
|
||||||
|
### 2. HkdfKeyDeriver (mytheclipse-crypto, derivation feature)
|
||||||
|
File: `crates/mytheclipse-crypto/src/hkdf.rs`
|
||||||
|
- `HkdfKeyDeriver` — HKDF-SHA256 (RFC 5869) from master secret
|
||||||
|
- `derive_key(&self, purpose: &str, output_len) -> Vec<u8>` — context-specific sub-key
|
||||||
|
- domain separation via purpose as info
|
||||||
|
- gated feature "derivation"
|
||||||
|
|
||||||
|
### 3. BackpressureEnqueue (mytheclipse-queue, in-memory)
|
||||||
|
File: `crates/mytheclipse-queue/src/backpressure.rs`
|
||||||
|
- `BackpressureEnforcer` — tracks in-flight count, enforces max
|
||||||
|
- `enqueue_or_nack(queue, topic, payload, max_inflight) -> Result<(), BackpressureError>`
|
||||||
|
- non-blocking: returns BackpressureError when at capacity
|
||||||
|
|
||||||
|
## Verification
|
||||||
|
- build + test + clippy + commit + push
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
# Implementation Spec: Round 7 — COMPLETE
|
||||||
|
|
||||||
|
3 fitur implementasi selesai:
|
||||||
|
- `BgJoiner` (core, lifecycle) — graceful task join, 2 tests
|
||||||
|
- `MiddlewarePipeline` (core, observability+resiliency) — composable async mw stack, 2 tests
|
||||||
|
- `RateLimitedQueue` (queue) — token-bucket rate-limited enqueue wrapper, 2 tests + QueueError::RateLimit variant
|
||||||
|
|
||||||
|
Build: `cargo build --workspace --all-features` exit 0.
|
||||||
|
Tests: semua pass (0 FAILED).
|
||||||
|
Clippy: 0 new warnings.
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
# Round 8 — COMPLETE
|
||||||
|
|
||||||
|
## New Feature
|
||||||
|
### ResilientHttpClient (mytheclipse-http, resilience feature)
|
||||||
|
- File: `crates/mytheclipse-http/src/resilient_client.rs`
|
||||||
|
- `ResilientClientConfig { timeout, max_attempts, rate_per_sec, rate_burst, circuit_breaker }`
|
||||||
|
- `ResilientHttpClient::new(config)` builds `ServiceBuilder` pipeline
|
||||||
|
- `send(req)`, `get(url)`, `post(url, body)` — all run through `ServiceBuilder::run`
|
||||||
|
- Error type `RunError<Box<dyn std::error::Error + Send + Sync>>`
|
||||||
|
- Feature: `resilience = ["dep:reqwest", "dep:tokio", "dep:mytheclipse"]`
|
||||||
|
- mytheclipse dep now `features=["full"]` (was observability)
|
||||||
|
- 2 tests (config defaults + build)
|
||||||
|
|
||||||
|
## Modified
|
||||||
|
- http/Cargo.toml — resilience feature + mytheclipse full features
|
||||||
|
- http/lib.rs — module + re-export
|
||||||
|
- core/lib.rs — pub use RunError, ServiceConfig (needed by http crate)
|
||||||
|
- error.rs — RateLimit(String) variant (queue crate, round 6 carryover)
|
||||||
|
|
||||||
|
## Build: exit 0. Tests: 0 FAILED. Clippy: 0 new warnings.
|
||||||
|
|
||||||
|
## Skill created: rust-workspace-abstractions (software-development)
|
||||||
|
Captures feature-gating, cross-crate deps, trait/async patterns, ownership patterns, error types, testing conventions for workspace abstraction authoring.
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
# Implementation Spec: Round 9 — COMPLETE
|
||||||
|
|
||||||
|
## New Feature
|
||||||
|
|
||||||
|
### RetryExt (mytheclipse-core, resiliency)
|
||||||
|
File: `crates/mytheclipse/src/retry_ext.rs`
|
||||||
|
- `RetryExt` trait — `.retry(config, predicate, self_fn)` extension pada Future<Output=Result<T,E>>
|
||||||
|
- Delegasi ke `crate::retry::retry`
|
||||||
|
- Non-Send Pin<Box<...>> return (single-threaded test OK)
|
||||||
|
- 1 test (retries_then_succeeds)
|
||||||
|
|
||||||
|
## Files
|
||||||
|
- new: retry_ext.rs
|
||||||
|
- core/lib.rs: +module +pub use RetryExt
|
||||||
|
|
||||||
|
Build: exit 0. Tests: 0 FAILED. Clippy: 0 new warnings.
|
||||||
@@ -1,3 +1,59 @@
|
|||||||
|
# [1.14.0](https://github.com/asepharyana/mytheclipse/compare/v1.13.0...v1.14.0) (2026-08-29)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* round-11 abstractions — RuntimeConfig auto thread/core, ShutdownGuard RAII ([74abe71](https://github.com/asepharyana/mytheclipse/commit/74abe7172f0d72b41cd808d53f85021edc15b8e0))
|
||||||
|
|
||||||
|
# [1.13.0](https://github.com/asepharyana/mytheclipse/compare/v1.12.0...v1.13.0) (2026-08-29)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* round-10 abstractions — AutoMetricsServiceBuilder, RateLimitedWorkerPool ([2f445d3](https://github.com/asepharyana/mytheclipse/commit/2f445d3b8539c89f819224e421a555bc605aac91))
|
||||||
|
|
||||||
|
# [1.12.0](https://github.com/asepharyana/mytheclipse/compare/v1.11.0...v1.12.0) (2026-08-29)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* round-9 abstractions — RetryExt ergonomic retry, ResilientHttpClient ([7a063fa](https://github.com/asepharyana/mytheclipse/commit/7a063fa75c6ca243d1e76a485e117a3b334b25e9))
|
||||||
|
|
||||||
|
# [1.11.0](https://github.com/asepharyana/mytheclipse/compare/v1.10.0...v1.11.0) (2026-08-29)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* round-8 abstractions — ResilientHttpClient, MiddlewarePipeline, BgJoiner ([076b0bb](https://github.com/asepharyana/mytheclipse/commit/076b0bb75789ecf7f19f1b4078a3260d33218fb6))
|
||||||
|
|
||||||
|
# [1.10.0](https://github.com/asepharyana/mytheclipse/compare/v1.9.0...v1.10.0) (2026-08-29)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* round-7 abstractions — BgJoiner, MiddlewarePipeline, RateLimitedQueue ([851c8c4](https://github.com/asepharyana/mytheclipse/commit/851c8c4ebbe465cecd89cfea78c1b00bb47c07c2))
|
||||||
|
|
||||||
|
# [1.9.0](https://github.com/asepharyana/mytheclipse/compare/v1.8.0...v1.9.0) (2026-08-29)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* round-6 abstractions — HealthCheckedPool, HkdfKeyDeriver, BackpressureEnforcer ([a03db38](https://github.com/asepharyana/mytheclipse/commit/a03db38c5ccabead51fa49d2001b0cd94a9dd66e))
|
||||||
|
|
||||||
|
# [1.8.0](https://github.com/asepharyana/mytheclipse/compare/v1.7.0...v1.8.0) (2026-08-29)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* round-5 abstractions — BatchProcessor, CircuitBreakerHealthCheck, TypedKeyRegistry, MetricsHttpHandler ([97b5e02](https://github.com/asepharyana/mytheclipse/commit/97b5e02820674a5b61a2d396f95df07f2b4fd735))
|
||||||
|
|
||||||
|
# [1.7.0](https://github.com/asepharyana/mytheclipse/compare/v1.6.0...v1.7.0) (2026-08-29)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* round-5 abstractions — CircuitBreakerHealthCheck, TypedKeyRegistry, MetricsHttpHandler ([510aadc](https://github.com/asepharyana/mytheclipse/commit/510aadc066a428c1627a38bdb22e4f0440cc01b3))
|
||||||
|
|
||||||
# [1.6.0](https://github.com/asepharyana/mytheclipse/compare/v1.5.0...v1.6.0) (2026-08-29)
|
# [1.6.0](https://github.com/asepharyana/mytheclipse/compare/v1.5.0...v1.6.0) (2026-08-29)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Generated
+23
-10
@@ -2162,6 +2162,15 @@ version = "0.4.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
|
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hkdf"
|
||||||
|
version = "0.12.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7b5f8eb2ad728638ea2c7d47a21db23b7b58a72ed6a38256b8a1849f15fbbdf7"
|
||||||
|
dependencies = [
|
||||||
|
"hmac 0.12.1",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hmac"
|
name = "hmac"
|
||||||
version = "0.12.1"
|
version = "0.12.1"
|
||||||
@@ -2818,7 +2827,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mytheclipse"
|
name = "mytheclipse"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"num_cpus",
|
"num_cpus",
|
||||||
@@ -2832,7 +2841,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mytheclipse-cache"
|
name = "mytheclipse-cache"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"moka",
|
"moka",
|
||||||
@@ -2845,7 +2854,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mytheclipse-cli"
|
name = "mytheclipse-cli"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"tokio",
|
"tokio",
|
||||||
@@ -2854,7 +2863,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mytheclipse-config"
|
name = "mytheclipse-config"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
"notify",
|
"notify",
|
||||||
@@ -2869,13 +2878,14 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mytheclipse-crypto"
|
name = "mytheclipse-crypto"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aead",
|
"aead",
|
||||||
"aes-gcm",
|
"aes-gcm",
|
||||||
"argon2",
|
"argon2",
|
||||||
"base64 0.22.1",
|
"base64 0.22.1",
|
||||||
"hashbrown 0.15.5",
|
"hashbrown 0.15.5",
|
||||||
|
"hkdf",
|
||||||
"jsonwebtoken",
|
"jsonwebtoken",
|
||||||
"pasetors",
|
"pasetors",
|
||||||
"password-hash",
|
"password-hash",
|
||||||
@@ -2883,13 +2893,14 @@ dependencies = [
|
|||||||
"rand_core 0.6.4",
|
"rand_core 0.6.4",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"sha2 0.10.9",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mytheclipse-event"
|
name = "mytheclipse-event"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-nats",
|
"async-nats",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
@@ -2905,21 +2916,23 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mytheclipse-http"
|
name = "mytheclipse-http"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"axum",
|
"axum",
|
||||||
"hyper 1.11.1",
|
"hyper 1.11.1",
|
||||||
|
"mytheclipse",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"tower",
|
||||||
"tracing",
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mytheclipse-queue"
|
name = "mytheclipse-queue"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-nats",
|
"async-nats",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
@@ -2935,7 +2948,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mytheclipse-storage"
|
name = "mytheclipse-storage"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"aws-config",
|
"aws-config",
|
||||||
@@ -2951,7 +2964,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mytheclipse-tracing"
|
name = "mytheclipse-tracing"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"opentelemetry 0.25.0",
|
"opentelemetry 0.25.0",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mytheclipse-cache"
|
name = "mytheclipse-cache"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.75"
|
rust-version = "1.75"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mytheclipse-cli"
|
name = "mytheclipse-cli"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.75"
|
rust-version = "1.75"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mytheclipse-config"
|
name = "mytheclipse-config"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.75"
|
rust-version = "1.75"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mytheclipse-crypto"
|
name = "mytheclipse-crypto"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.75"
|
rust-version = "1.75"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
@@ -22,6 +22,8 @@ encryption = ["dep:aead", "dep:aes-gcm", "dep:rand_core", "dep:rand"]
|
|||||||
tokens = ["encryption", "dep:serde", "dep:serde_json", "dep:base64", "dep:jsonwebtoken"]
|
tokens = ["encryption", "dep:serde", "dep:serde_json", "dep:base64", "dep:jsonwebtoken"]
|
||||||
paseto = ["encryption", "dep:serde", "dep:serde_json", "dep:base64", "dep:pasetors"]
|
paseto = ["encryption", "dep:serde", "dep:serde_json", "dep:base64", "dep:pasetors"]
|
||||||
rate-limit = ["dep:hashbrown", "dep:tokio"]
|
rate-limit = ["dep:hashbrown", "dep:tokio"]
|
||||||
|
# HKDF-SHA256 key derivation (RFC 5869).
|
||||||
|
derivation = ["dep:hkdf", "dep:sha2"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
@@ -37,6 +39,8 @@ serde = { version = "1", optional = true, features = ["derive"] }
|
|||||||
serde_json = { version = "1", optional = true }
|
serde_json = { version = "1", optional = true }
|
||||||
rand = { version = "0.8", default-features = false, features = ["std", "std_rng"], optional = true }
|
rand = { version = "0.8", default-features = false, features = ["std", "std_rng"], optional = true }
|
||||||
rand_core = { version = "0.6", optional = true }
|
rand_core = { version = "0.6", optional = true }
|
||||||
|
hkdf = { version = "0.12", default-features = false, optional = true }
|
||||||
|
sha2 = { version = "0.10", optional = true }
|
||||||
pasetors = { version = "0.6", optional = true, default-features = false, features = ["v4"] }
|
pasetors = { version = "0.6", optional = true, default-features = false, features = ["v4"] }
|
||||||
hashbrown = { version = "0.15", optional = true }
|
hashbrown = { version = "0.15", optional = true }
|
||||||
tokio = { version = "1.53", features = ["sync", "time"], optional = true }
|
tokio = { version = "1.53", features = ["sync", "time"], optional = true }
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
//! HKDF-SHA256 key derivation (feature `derivation`).
|
||||||
|
//!
|
||||||
|
//! [`HkdfKeyDeriver`] wraps the HKDF construction (RFC 5869) to derive
|
||||||
|
//! domain-specific sub-keys from a single master secret. Each purpose
|
||||||
|
//! string acts as the `info` parameter for domain separation.
|
||||||
|
|
||||||
|
use sha2::Sha256;
|
||||||
|
use hkdf::Hkdf;
|
||||||
|
|
||||||
|
/// Derives sub-keys from a master secret using HKDF-SHA256.
|
||||||
|
pub struct HkdfKeyDeriver {
|
||||||
|
hk: Hkdf<Sha256>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HkdfKeyDeriver {
|
||||||
|
/// Creates a deriver from the given master secret (IKM).
|
||||||
|
pub fn new(master: &[u8]) -> Self {
|
||||||
|
let hk = Hkdf::<Sha256>::new(None, master);
|
||||||
|
Self { hk }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Derives a sub-key for the given `purpose` (used as the `info` parameter).
|
||||||
|
///
|
||||||
|
/// Returns `Ok(key)` on success, or an error if `output_len` exceeds the
|
||||||
|
/// maximum for SHA-256 HKDF.
|
||||||
|
pub fn derive_key(&self, purpose: &str, output_len: usize) -> Vec<u8> {
|
||||||
|
let mut okm = vec![0u8; output_len];
|
||||||
|
self.hk
|
||||||
|
.expand(purpose.as_bytes(), &mut okm)
|
||||||
|
.expect("HKDF expand failed — output_len too large");
|
||||||
|
okm
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convenience: derive a 32-byte AES-256 key for `purpose`.
|
||||||
|
pub fn derive_aes256_key(&self, purpose: &str) -> [u8; 32] {
|
||||||
|
let v = self.derive_key(purpose, 32);
|
||||||
|
let mut key = [0u8; 32];
|
||||||
|
key.copy_from_slice(&v);
|
||||||
|
key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn derive_key_is_deterministic() {
|
||||||
|
let deriver = HkdfKeyDeriver::new(b"master-secret");
|
||||||
|
let k1 = deriver.derive_key("encryption", 32);
|
||||||
|
let k2 = deriver.derive_key("encryption", 32);
|
||||||
|
assert_eq!(k1, k2);
|
||||||
|
assert_eq!(k1.len(), 32);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn derive_key_different_purposes_yield_different_keys() {
|
||||||
|
let deriver = HkdfKeyDeriver::new(b"master-secret");
|
||||||
|
let enc = deriver.derive_key("encryption", 32);
|
||||||
|
let auth = deriver.derive_key("auth", 32);
|
||||||
|
assert_ne!(enc, auth);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn derive_aes256_key_length() {
|
||||||
|
let deriver = HkdfKeyDeriver::new(b"master-secret");
|
||||||
|
let key = deriver.derive_aes256_key("signing");
|
||||||
|
assert_eq!(key.len(), 32);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
//! Typed key registry with ID-based lookup (feature `password`).
|
||||||
|
//!
|
||||||
|
//! [`TypedKeyRegistry`] extends [`KeyRing`] semantics: instead of a single
|
||||||
|
//! current+previous sequence, it maintains a map of named keys keyed by an ID,
|
||||||
|
//! with one designated "current" ID. This is useful when keys are rotated by ID
|
||||||
|
//! (e.g. JWT `kid` header) and you need to look up a verification key by ID
|
||||||
|
//! while only accepting tokens signed by the current key.
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use crate::CryptoError;
|
||||||
|
|
||||||
|
/// A registry of named keys with a single "current" key.
|
||||||
|
#[derive(Debug, Clone, Default)]
|
||||||
|
pub struct TypedKeyRegistry<T> {
|
||||||
|
keys: HashMap<String, T>,
|
||||||
|
current_id: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> TypedKeyRegistry<T> {
|
||||||
|
/// Creates an empty registry (no current key).
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self { keys: HashMap::new(), current_id: None }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Registers a key under `id`, making it the current key.
|
||||||
|
pub fn register(&mut self, id: impl Into<String>, key: T) {
|
||||||
|
let id = id.into();
|
||||||
|
self.keys.insert(id.clone(), key);
|
||||||
|
self.current_id = Some(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Looks up a key by ID (current or previous).
|
||||||
|
pub fn lookup(&self, id: &str) -> Option<&T> {
|
||||||
|
self.keys.get(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the current key, if any.
|
||||||
|
pub fn current(&self) -> Option<&T> {
|
||||||
|
self.current_id
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|id| self.keys.get(id))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the ID of the current key.
|
||||||
|
pub fn current_id(&self) -> Option<&str> {
|
||||||
|
self.current_id.as_deref()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Rotates to a new current key identified by `id`. The old current key
|
||||||
|
/// remains accessible via `lookup` but is no longer the active signing key.
|
||||||
|
pub fn rotate_current(&mut self, id: impl Into<String>, key: T) {
|
||||||
|
let id = id.into();
|
||||||
|
self.keys.insert(id.clone(), key);
|
||||||
|
self.current_id = Some(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Number of keys in the registry.
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.keys.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether the registry has any keys.
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.keys.is_empty()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns an error if no current key is registered.
|
||||||
|
pub fn require_current(&self) -> Result<&T, CryptoError> {
|
||||||
|
self.current()
|
||||||
|
.ok_or_else(|| CryptoError::Key("no current key registered".to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn register_and_lookup() {
|
||||||
|
let mut reg = TypedKeyRegistry::new();
|
||||||
|
reg.register("k1", [1u8; 32]);
|
||||||
|
assert_eq!(reg.current_id(), Some("k1"));
|
||||||
|
assert!(reg.lookup("k1").is_some());
|
||||||
|
assert_eq!(reg.lookup("k1"), Some(&[1u8; 32]));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn lookup_unknown_returns_none() {
|
||||||
|
let reg = TypedKeyRegistry::<[u8; 32]>::new();
|
||||||
|
assert!(reg.lookup("nope").is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rotate_preserves_previous() {
|
||||||
|
let mut reg = TypedKeyRegistry::new();
|
||||||
|
reg.register("k1", [1u8; 32]);
|
||||||
|
reg.rotate_current("k2", [2u8; 32]);
|
||||||
|
assert_eq!(reg.current_id(), Some("k2"));
|
||||||
|
assert!(reg.lookup("k1").is_some());
|
||||||
|
assert_eq!(reg.lookup("k1"), Some(&[1u8; 32]));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn require_current_errors_when_empty() {
|
||||||
|
let reg = TypedKeyRegistry::<[u8; 32]>::new();
|
||||||
|
assert!(matches!(reg.require_current(), Err(CryptoError::Key(_))));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn len_and_is_empty() {
|
||||||
|
let mut reg = TypedKeyRegistry::new();
|
||||||
|
assert!(reg.is_empty());
|
||||||
|
reg.register("a", 0u32);
|
||||||
|
assert_eq!(reg.len(), 1);
|
||||||
|
assert!(!reg.is_empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -42,6 +42,7 @@
|
|||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
pub mod key_ring;
|
pub mod key_ring;
|
||||||
|
pub mod key_registry;
|
||||||
|
|
||||||
#[cfg(feature = "password")]
|
#[cfg(feature = "password")]
|
||||||
pub mod password;
|
pub mod password;
|
||||||
@@ -54,6 +55,8 @@ pub mod token;
|
|||||||
|
|
||||||
#[cfg(feature = "paseto")]
|
#[cfg(feature = "paseto")]
|
||||||
pub mod paseto;
|
pub mod paseto;
|
||||||
|
#[cfg(feature = "derivation")]
|
||||||
|
pub mod hkdf;
|
||||||
|
|
||||||
#[cfg(feature = "password")]
|
#[cfg(feature = "password")]
|
||||||
pub use password::PasswordHasher;
|
pub use password::PasswordHasher;
|
||||||
@@ -68,6 +71,10 @@ pub use token::{Claims, TokenError, TokenSigner};
|
|||||||
pub use paseto::{PasetoSigner, PasetoClaims};
|
pub use paseto::{PasetoSigner, PasetoClaims};
|
||||||
|
|
||||||
pub use key_ring::KeyRing;
|
pub use key_ring::KeyRing;
|
||||||
|
pub use key_registry::TypedKeyRegistry;
|
||||||
|
|
||||||
|
#[cfg(feature = "derivation")]
|
||||||
|
pub use hkdf::HkdfKeyDeriver;
|
||||||
|
|
||||||
/// Errors returned across mytheclipse-crypto primitives.
|
/// Errors returned across mytheclipse-crypto primitives.
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mytheclipse-event"
|
name = "mytheclipse-event"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.75"
|
rust-version = "1.75"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mytheclipse-http"
|
name = "mytheclipse-http"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.75"
|
rust-version = "1.75"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
@@ -21,6 +21,10 @@ client = ["dep:reqwest", "dep:tokio"]
|
|||||||
server-hyper = ["dep:hyper", "dep:tokio"]
|
server-hyper = ["dep:hyper", "dep:tokio"]
|
||||||
# Server backed by axum.
|
# Server backed by axum.
|
||||||
server-axum = ["dep:axum", "dep:hyper", "dep:tokio"]
|
server-axum = ["dep:axum", "dep:hyper", "dep:tokio"]
|
||||||
|
# Metrics HTTP endpoint serving Prometheus text format from a MetricsCollector.
|
||||||
|
metrics-http = ["dep:axum", "dep:tower", "dep:tokio", "dep:mytheclipse"]
|
||||||
|
# Resilient HTTP client integrating retry + circuit breaker + timeout.
|
||||||
|
resilience = ["dep:reqwest", "dep:tokio", "dep:mytheclipse"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
@@ -29,8 +33,10 @@ tokio = { version = "1.53", features = ["sync", "time", "rt", "macros"], optiona
|
|||||||
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"], optional = true }
|
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"], optional = true }
|
||||||
hyper = { version = "1", features = ["full"], optional = true }
|
hyper = { version = "1", features = ["full"], optional = true }
|
||||||
axum = { version = "0.8", optional = true }
|
axum = { version = "0.8", optional = true }
|
||||||
|
tower = { version = "0.5", optional = true, default-features = false, features = ["util"] }
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
|
mytheclipse = { version = "1.5", path = "../mytheclipse", optional = true, default-features = false, features = ["full"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio = { version = "1.53", features = ["full"] }
|
tokio = { version = "1.53", features = ["full"] }
|
||||||
|
|||||||
@@ -9,6 +9,11 @@
|
|||||||
//! mytheclipse-http = { version = "0.2", features = ["client"] }
|
//! mytheclipse-http = { version = "0.2", features = ["client"] }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
#[cfg(feature = "resilience")]
|
||||||
|
pub mod resilient_client;
|
||||||
|
#[cfg(feature = "resilience")]
|
||||||
|
pub use resilient_client::{ResilientHttpClient, ResilientClientConfig};
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
pub mod client;
|
pub mod client;
|
||||||
|
|
||||||
@@ -17,3 +22,9 @@ pub use client::HttpClient;
|
|||||||
|
|
||||||
#[cfg(feature = "server-axum")]
|
#[cfg(feature = "server-axum")]
|
||||||
pub mod server;
|
pub mod server;
|
||||||
|
|
||||||
|
#[cfg(feature = "metrics-http")]
|
||||||
|
pub mod metrics_http;
|
||||||
|
|
||||||
|
#[cfg(feature = "metrics-http")]
|
||||||
|
pub use metrics_http::metrics_routes;
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
//! Prometheus metrics HTTP endpoint (feature `metrics-http`).
|
||||||
|
//!
|
||||||
|
//! [`metrics_routes`] returns an [`axum::Router`] that serves the
|
||||||
|
//! [`MetricsCollector`]'s Prometheus text exposition format at `/metrics`.
|
||||||
|
|
||||||
|
use axum::routing::get;
|
||||||
|
use axum::Router;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use mytheclipse::MetricsCollector;
|
||||||
|
|
||||||
|
/// Builds a small axum router exposing `/metrics` (Prometheus text) and
|
||||||
|
/// `/` (a one-line description).
|
||||||
|
pub fn metrics_routes(collector: MetricsCollector) -> Router {
|
||||||
|
let collector = Arc::new(collector);
|
||||||
|
Router::new()
|
||||||
|
.route("/", get(|| async { "mytheclipse metrics" }))
|
||||||
|
.route("/metrics", get(metrics_handler))
|
||||||
|
.with_state(collector)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Axum handler serving the Prometheus text format.
|
||||||
|
async fn metrics_handler(
|
||||||
|
axum::extract::State(collector): axum::extract::State<Arc<MetricsCollector>>,
|
||||||
|
) -> axum::response::Response {
|
||||||
|
let body = collector.export_prometheus();
|
||||||
|
axum::response::Response::builder()
|
||||||
|
.status(200)
|
||||||
|
.header("content-type", "text/plain; version=0.0.4")
|
||||||
|
.body(axum::body::Body::from(body))
|
||||||
|
.unwrap_or_else(|_| {
|
||||||
|
axum::response::Response::new(axum::body::Body::from(
|
||||||
|
"internal error",
|
||||||
|
))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use tower::util::ServiceExt;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn metrics_routes_serves_prometheus() {
|
||||||
|
let collector = MetricsCollector::new();
|
||||||
|
collector.inc_counter("test_reqs", 42);
|
||||||
|
let app = metrics_routes(collector);
|
||||||
|
|
||||||
|
let request = axum::extract::Request::get("/metrics")
|
||||||
|
.body(axum::body::Body::empty())
|
||||||
|
.unwrap();
|
||||||
|
let response = app.oneshot(request).await.unwrap();
|
||||||
|
assert_eq!(response.status(), 200);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
//! Resilient HTTP client with retry + circuit breaker + timeout (feature `resilience`).
|
||||||
|
//!
|
||||||
|
//! Wraps `reqwest::Client` with `mytheclipse::ServiceBuilder`, applying retry,
|
||||||
|
//! circuit-breaker, and timeout layers around every request.
|
||||||
|
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use reqwest::Client;
|
||||||
|
use reqwest::Method;
|
||||||
|
use reqwest::RequestBuilder;
|
||||||
|
use tracing::Instrument;
|
||||||
|
|
||||||
|
use mytheclipse::{CircuitBreaker, RunError, ServiceBuilder, ServiceConfig};
|
||||||
|
|
||||||
|
type HttpError = Box<dyn std::error::Error + Send + Sync>;
|
||||||
|
|
||||||
|
/// Configuration for [`ResilientHttpClient`].
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct ResilientClientConfig {
|
||||||
|
pub timeout: Duration,
|
||||||
|
pub max_attempts: u32,
|
||||||
|
pub rate_per_sec: f64,
|
||||||
|
pub rate_burst: u64,
|
||||||
|
pub circuit_breaker: Option<CircuitBreaker>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ResilientClientConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
timeout: Duration::from_secs(30),
|
||||||
|
max_attempts: 1,
|
||||||
|
rate_per_sec: 0.0,
|
||||||
|
rate_burst: 0,
|
||||||
|
circuit_breaker: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A reqwest client that runs every request through a `ServiceBuilder`
|
||||||
|
/// pipeline (retry + circuit breaker + timeout).
|
||||||
|
pub struct ResilientHttpClient {
|
||||||
|
inner: Client,
|
||||||
|
config: ResilientClientConfig,
|
||||||
|
builder: ServiceBuilder,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ResilientHttpClient {
|
||||||
|
/// Creates a new resilient client from the given config.
|
||||||
|
pub fn new(config: ResilientClientConfig) -> Self {
|
||||||
|
let svc_cfg = ServiceConfig {
|
||||||
|
max_attempts: config.max_attempts,
|
||||||
|
timeout: config.timeout,
|
||||||
|
rate_per_sec: config.rate_per_sec,
|
||||||
|
rate_burst: config.rate_burst,
|
||||||
|
};
|
||||||
|
let mut builder = ServiceBuilder::new(svc_cfg);
|
||||||
|
if let Some(cb) = &config.circuit_breaker {
|
||||||
|
builder = builder.with_circuit_breaker(cb.clone());
|
||||||
|
}
|
||||||
|
Self {
|
||||||
|
inner: Client::new(),
|
||||||
|
config,
|
||||||
|
builder,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the configured default timeout.
|
||||||
|
pub fn timeout(&self) -> Duration {
|
||||||
|
self.config.timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a `RequestBuilder` for `method` + `url`.
|
||||||
|
pub fn request(&self, method: Method, url: &str) -> RequestBuilder {
|
||||||
|
self.inner.request(method, url)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends a pre-built `RequestBuilder` through the resiliency pipeline.
|
||||||
|
/// Returns the response bytes on success.
|
||||||
|
pub async fn send(
|
||||||
|
&self,
|
||||||
|
req: RequestBuilder,
|
||||||
|
) -> Result<Vec<u8>, RunError<HttpError>> {
|
||||||
|
let span = tracing::info_span!("resilient_http_send");
|
||||||
|
let op = move || {
|
||||||
|
let req = req.try_clone().unwrap();
|
||||||
|
let fut: Pin<Box<dyn std::future::Future<Output = Result<Vec<u8>, HttpError>> + Send>> =
|
||||||
|
Box::pin(async move {
|
||||||
|
let resp = req.send().instrument(tracing::trace_span!("http_send")).await?;
|
||||||
|
let bytes = resp.bytes().await?;
|
||||||
|
Ok::<Vec<u8>, HttpError>(bytes.to_vec())
|
||||||
|
});
|
||||||
|
fut
|
||||||
|
};
|
||||||
|
self.builder
|
||||||
|
.run(op)
|
||||||
|
.instrument(span)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convenience: GET `url`, returning response bytes.
|
||||||
|
pub async fn get(&self, url: &str) -> Result<Vec<u8>, RunError<HttpError>> {
|
||||||
|
self.send(self.inner.get(url)).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convenience: POST `url`, returning response bytes.
|
||||||
|
pub async fn post(&self, url: &str, body: Vec<u8>) -> Result<Vec<u8>, RunError<HttpError>> {
|
||||||
|
self.send(self.inner.post(url).body(body)).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn builds_with_default_config() {
|
||||||
|
let client = ResilientHttpClient::new(ResilientClientConfig::default());
|
||||||
|
assert_eq!(client.timeout(), Duration::from_secs(30));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn config_default_values() {
|
||||||
|
let c = ResilientClientConfig::default();
|
||||||
|
assert_eq!(c.timeout, Duration::from_secs(30));
|
||||||
|
assert_eq!(c.max_attempts, 1);
|
||||||
|
assert!(c.circuit_breaker.is_none());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mytheclipse-queue"
|
name = "mytheclipse-queue"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.75"
|
rust-version = "1.75"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
|
|||||||
@@ -0,0 +1,147 @@
|
|||||||
|
//! Backpressure-aware enqueuer (in-memory backend).
|
||||||
|
//!
|
||||||
|
//! [`BackpressureEnforcer`] tracks in-flight jobs and caps the number of
|
||||||
|
//! pending enqueues per topic, returning [`BackpressureError`] instead of
|
||||||
|
//! blocking when the cap is exceeded.
|
||||||
|
|
||||||
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use tokio::sync::Semaphore;
|
||||||
|
|
||||||
|
use crate::traits::Queue;
|
||||||
|
|
||||||
|
/// Errors returned by [`BackpressureEnforcer`].
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum BackpressureError {
|
||||||
|
/// The configured in-flight cap was reached; enqueue rejected.
|
||||||
|
LimitReached { topic: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for BackpressureError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::LimitReached { topic } => {
|
||||||
|
write!(f, "backpressure: in-flight limit reached for topic {topic}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for BackpressureError {}
|
||||||
|
|
||||||
|
/// Enforces a maximum number of in-flight jobs per topic.
|
||||||
|
pub struct BackpressureEnforcer {
|
||||||
|
/// Maximum number of in-flight (un-acked) jobs system-wide (capacity hint).
|
||||||
|
#[allow(dead_code)]
|
||||||
|
max_inflight: usize,
|
||||||
|
/// Per-topic in-flight counter.
|
||||||
|
counters: Arc<std::sync::Mutex<std::collections::HashMap<String, Arc<AtomicU64>>>>,
|
||||||
|
/// Bounded semaphore enforcing total concurrency.
|
||||||
|
#[allow(dead_code)]
|
||||||
|
global: Arc<Semaphore>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BackpressureEnforcer {
|
||||||
|
/// Creates an enforcer with a global maximum of `max_inflight` concurrent
|
||||||
|
/// in-flight jobs.
|
||||||
|
pub fn new(max_inflight: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
max_inflight,
|
||||||
|
counters: Arc::new(std::sync::Mutex::new(std::collections::HashMap::new())),
|
||||||
|
global: Arc::new(Semaphore::new(max_inflight.max(1))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the per-topic in-flight count, creating zero if absent.
|
||||||
|
fn get_counter(&self, topic: &str) -> Arc<AtomicU64> {
|
||||||
|
let mut map = self.counters.lock().unwrap();
|
||||||
|
map.entry(topic.to_string())
|
||||||
|
.or_insert_with(|| Arc::new(AtomicU64::new(0)));
|
||||||
|
Arc::clone(map.get(topic).unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attempts to acquire a backpressure slot non-blockingly.
|
||||||
|
/// Returns Err if at capacity.
|
||||||
|
pub async fn try_enqueue<Q: Queue + ?Sized>(
|
||||||
|
&self,
|
||||||
|
queue: &Q,
|
||||||
|
topic: &str,
|
||||||
|
payload: Vec<u8>,
|
||||||
|
) -> Result<(), BackpressureError> {
|
||||||
|
// Per-topic counter increment (informational; global semaphore is the hard limit)
|
||||||
|
let counter = self.get_counter(topic);
|
||||||
|
counter.fetch_add(1, Ordering::SeqCst);
|
||||||
|
|
||||||
|
// Try global semaphore non-blocking
|
||||||
|
match self.global.clone().try_acquire_owned() {
|
||||||
|
Ok(_permit) => {
|
||||||
|
let _ = queue.enqueue(topic, payload).await;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
counter.fetch_sub(1, Ordering::SeqCst);
|
||||||
|
Err(BackpressureError::LimitReached {
|
||||||
|
topic: topic.to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Increments the in-flight counter when a job is delivered.
|
||||||
|
pub fn inc_delivered(&self, topic: &str) {
|
||||||
|
self.get_counter(topic).fetch_add(1, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Decrements the in-flight counter after a job is acked/nacked.
|
||||||
|
pub fn dec_finished(&self, topic: &str) {
|
||||||
|
self.get_counter(topic).fetch_sub(1, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Current in-flight count for a topic.
|
||||||
|
pub fn inflight(&self, topic: &str) -> u64 {
|
||||||
|
self.get_counter(topic).load(Ordering::SeqCst)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Helper: enqueue with backpressure, returning how many were rejected.
|
||||||
|
pub async fn enqueue_with_backpressure<Q: Queue + ?Sized>(
|
||||||
|
enforcer: &BackpressureEnforcer,
|
||||||
|
queue: &Q,
|
||||||
|
topic: &str,
|
||||||
|
payloads: Vec<Vec<u8>>,
|
||||||
|
) -> Result<usize, BackpressureError> {
|
||||||
|
let mut rejected = 0;
|
||||||
|
for payload in payloads {
|
||||||
|
if let Err(_) = enforcer.try_enqueue(queue, topic, payload).await {
|
||||||
|
rejected += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(rejected)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::in_memory::InMemoryQueue;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn try_enqueue_within_limit_succeeds() {
|
||||||
|
let reg = BackpressureEnforcer::new(2);
|
||||||
|
let queue = InMemoryQueue::new();
|
||||||
|
let result = reg.try_enqueue(&queue, "t", b"x".to_vec()).await;
|
||||||
|
assert!(result.is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn try_enqueue_rejects_when_full() {
|
||||||
|
let reg = BackpressureEnforcer::new(1);
|
||||||
|
let queue = InMemoryQueue::new();
|
||||||
|
|
||||||
|
// acquire the single global permit without releasing
|
||||||
|
let _first = reg.global.clone().acquire_owned().await.unwrap();
|
||||||
|
|
||||||
|
let result = reg.try_enqueue(&queue, "t", b"x".to_vec()).await;
|
||||||
|
assert!(matches!(result, Err(BackpressureError::LimitReached { .. })));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,245 @@
|
|||||||
|
//! Batch job processor for bulk processing of queued jobs.
|
||||||
|
//!
|
||||||
|
//! [`BatchProcessor`] wraps a [`Queue`] and accumulates jobs per topic until
|
||||||
|
//! either `batch_size` is reached or `batch_timeout` elapses, then dispatches
|
||||||
|
//! them to a [`BatchJobHandler`] for bulk processing (e.g. bulk DB insert,
|
||||||
|
//! bulk email send, batch index write).
|
||||||
|
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use tokio::sync::{mpsc, Semaphore};
|
||||||
|
|
||||||
|
use crate::error::JobError;
|
||||||
|
use crate::job::Job;
|
||||||
|
use crate::traits::Queue;
|
||||||
|
|
||||||
|
/// A handler that processes a batch of jobs atomically.
|
||||||
|
pub trait BatchJobHandler: Send + Sync {
|
||||||
|
fn handle_batch(&self, jobs: Vec<Job>) -> Pin<Box<dyn std::future::Future<Output = Result<(), JobError>> + Send>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F, Fut> BatchJobHandler for F
|
||||||
|
where
|
||||||
|
F: Fn(Vec<Job>) -> Fut + Send + Sync,
|
||||||
|
Fut: std::future::Future<Output = Result<(), JobError>> + Send + 'static,
|
||||||
|
{
|
||||||
|
fn handle_batch(&self, jobs: Vec<Job>) -> Pin<Box<dyn std::future::Future<Output = Result<(), JobError>> + Send>> {
|
||||||
|
Box::pin((self)(jobs))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Configuration for [`BatchProcessor`].
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct BatchConfig {
|
||||||
|
/// Max jobs per batch before flushing.
|
||||||
|
pub batch_size: usize,
|
||||||
|
/// Max time to wait before flushing a partial batch.
|
||||||
|
pub batch_timeout: Duration,
|
||||||
|
/// Max concurrent batch-processing tasks.
|
||||||
|
pub concurrency: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for BatchConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
batch_size: 100,
|
||||||
|
batch_timeout: Duration::from_secs(5),
|
||||||
|
concurrency: 4,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Result of a completed batch flush.
|
||||||
|
pub struct BatchFlush {
|
||||||
|
/// Number of jobs in the flushed batch.
|
||||||
|
pub count: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A processor that batches jobs before dispatching them.
|
||||||
|
pub struct BatchProcessor<Q: Queue + 'static> {
|
||||||
|
queue: Arc<Q>,
|
||||||
|
config: BatchConfig,
|
||||||
|
semaphore: Arc<Semaphore>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Q: Queue + 'static> BatchProcessor<Q> {
|
||||||
|
pub fn new(queue: Q, config: BatchConfig) -> Self {
|
||||||
|
let sem = Arc::new(Semaphore::new(config.concurrency.max(1)));
|
||||||
|
Self {
|
||||||
|
queue: Arc::new(queue),
|
||||||
|
config,
|
||||||
|
semaphore: sem,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Starts a batch processor for `topic` using `handler`.
|
||||||
|
pub fn start<H>(&self, topic: &str, handler: H)
|
||||||
|
where
|
||||||
|
H: BatchJobHandler + 'static,
|
||||||
|
{
|
||||||
|
let queue = Arc::clone(&self.queue);
|
||||||
|
let config = self.config.clone();
|
||||||
|
let semaphore = Arc::clone(&self.semaphore);
|
||||||
|
let handler: Arc<dyn BatchJobHandler> = Arc::new(handler);
|
||||||
|
let topic_owned = topic.to_string();
|
||||||
|
|
||||||
|
let (tx, mut rx): (mpsc::Sender<Job>, mpsc::Receiver<Job>) = mpsc::channel(config.batch_size);
|
||||||
|
|
||||||
|
// Dequeue loop → forward to channel
|
||||||
|
{
|
||||||
|
let q = Arc::clone(&queue);
|
||||||
|
let t = topic_owned.clone();
|
||||||
|
let tx2 = tx.clone();
|
||||||
|
let poll = config.poll_timeout();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
match q.dequeue(&t, poll).await {
|
||||||
|
Ok(Some(job)) => {
|
||||||
|
if tx2.send(job).await.is_err() {
|
||||||
|
// Processor dropped; re-enqueue remaining
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(None) => {}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!(queue_error = %e, "batch dequeue error");
|
||||||
|
tokio::time::sleep(poll).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Batch accumulation + flush loop
|
||||||
|
let h = handler;
|
||||||
|
tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
let mut batch: Vec<Job> = Vec::with_capacity(config.batch_size);
|
||||||
|
let deadline = tokio::time::sleep(config.batch_timeout);
|
||||||
|
tokio::pin!(deadline);
|
||||||
|
|
||||||
|
// Fill batch
|
||||||
|
loop {
|
||||||
|
if batch.len() >= config.batch_size {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
tokio::select! {
|
||||||
|
biased;
|
||||||
|
job = rx.recv() => match job {
|
||||||
|
Some(j) => batch.push(j),
|
||||||
|
None => {
|
||||||
|
// channel closed: drain remaining
|
||||||
|
while let Ok(j) = rx.try_recv() {
|
||||||
|
batch.push(j);
|
||||||
|
}
|
||||||
|
if !batch.is_empty() {
|
||||||
|
Self::flush(&h, &semaphore, batch).await;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ = &mut deadline => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !batch.is_empty() {
|
||||||
|
Self::flush(&h, &semaphore, batch).await;
|
||||||
|
}
|
||||||
|
deadline.as_mut().reset(tokio::time::Instant::now() + config.batch_timeout);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Keep tx alive for the dequeue loop (it was cloned)
|
||||||
|
let _keep = tx;
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn flush(handler: &Arc<dyn BatchJobHandler>, sem: &Arc<Semaphore>, batch: Vec<Job>) {
|
||||||
|
let permit = sem.clone().acquire_owned().await;
|
||||||
|
if permit.is_err() {
|
||||||
|
tracing::error!("batch semaphore closed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let _permit = permit.unwrap();
|
||||||
|
let h = Arc::clone(handler);
|
||||||
|
let batch_len = batch.len();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
match h.handle_batch(batch).await {
|
||||||
|
Ok(()) => tracing::debug!(count = batch_len, "batch processed"),
|
||||||
|
Err(e) => tracing::error!("batch handler error: {}", e),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BatchConfig {
|
||||||
|
fn poll_timeout(&self) -> Duration {
|
||||||
|
self.batch_timeout.min(Duration::from_millis(100))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::in_memory::InMemoryQueue;
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
use std::sync::Arc as StdArc;
|
||||||
|
|
||||||
|
fn make_queue() -> InMemoryQueue {
|
||||||
|
InMemoryQueue::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn flush_on_batch_size() {
|
||||||
|
let queue = make_queue();
|
||||||
|
let counter = StdArc::new(AtomicUsize::new(0));
|
||||||
|
let cfg = BatchConfig {
|
||||||
|
batch_size: 3,
|
||||||
|
batch_timeout: Duration::from_secs(10),
|
||||||
|
concurrency: 2,
|
||||||
|
};
|
||||||
|
let bp = BatchProcessor::new(queue, cfg);
|
||||||
|
let c2 = StdArc::clone(&counter);
|
||||||
|
bp.start("t", move |jobs: Vec<Job>| {
|
||||||
|
let c3 = StdArc::clone(&c2);
|
||||||
|
Box::pin(async move {
|
||||||
|
c3.fetch_add(jobs.len(), Ordering::SeqCst);
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
for i in 0..3 {
|
||||||
|
bp.queue.enqueue("t", format!("job{}", i).into_bytes()).await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
tokio::time::sleep(Duration::from_millis(300)).await;
|
||||||
|
assert_eq!(counter.load(Ordering::SeqCst), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn flush_on_timeout() {
|
||||||
|
let queue = make_queue();
|
||||||
|
let queue2 = queue.clone();
|
||||||
|
let counter = StdArc::new(AtomicUsize::new(0));
|
||||||
|
let cfg = BatchConfig {
|
||||||
|
batch_size: 100,
|
||||||
|
batch_timeout: Duration::from_millis(100),
|
||||||
|
concurrency: 2,
|
||||||
|
};
|
||||||
|
let bp = BatchProcessor::new(queue, cfg);
|
||||||
|
let c2 = StdArc::clone(&counter);
|
||||||
|
bp.start("t", move |jobs: Vec<Job>| {
|
||||||
|
let c3 = StdArc::clone(&c2);
|
||||||
|
Box::pin(async move {
|
||||||
|
c3.fetch_add(jobs.len(), Ordering::SeqCst);
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
queue2.enqueue("t", b"x".to_vec()).await.unwrap();
|
||||||
|
|
||||||
|
tokio::time::sleep(Duration::from_millis(300)).await;
|
||||||
|
assert_eq!(counter.load(Ordering::SeqCst), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,8 @@ pub enum QueueError {
|
|||||||
Serialization(String),
|
Serialization(String),
|
||||||
/// A timeout occurred while waiting for an operation.
|
/// A timeout occurred while waiting for an operation.
|
||||||
Timeout,
|
Timeout,
|
||||||
|
/// The operation was rejected because of a rate limit.
|
||||||
|
RateLimit(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for QueueError {
|
impl std::fmt::Display for QueueError {
|
||||||
@@ -20,6 +22,7 @@ impl std::fmt::Display for QueueError {
|
|||||||
Self::NotFound(s) => write!(f, "queue not found: {s}"),
|
Self::NotFound(s) => write!(f, "queue not found: {s}"),
|
||||||
Self::Serialization(s) => write!(f, "serialization error: {s}"),
|
Self::Serialization(s) => write!(f, "serialization error: {s}"),
|
||||||
Self::Timeout => write!(f, "queue operation timed out"),
|
Self::Timeout => write!(f, "queue operation timed out"),
|
||||||
|
Self::RateLimit(s) => write!(f, "queue rate limited: {s}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,43 +9,6 @@
|
|||||||
//! - **Redis** (`redis`) — LIST-based queue with atomic moves.
|
//! - **Redis** (`redis`) — LIST-based queue with atomic moves.
|
||||||
//! - **NATS JetStream** (`nats`) — durable consumer with ACK/NACK.
|
//! - **NATS JetStream** (`nats`) — durable consumer with ACK/NACK.
|
||||||
//! - **PostgreSQL** (`postgres`) — `SKIP LOCKED` polling.
|
//! - **PostgreSQL** (`postgres`) — `SKIP LOCKED` polling.
|
||||||
//!
|
|
||||||
//! ## Quick Start
|
|
||||||
//!
|
|
||||||
//! ```toml
|
|
||||||
//! [dependencies]
|
|
||||||
//! mytheclipse-queue = "0.2"
|
|
||||||
//! ```
|
|
||||||
//!
|
|
||||||
//! ```ignore
|
|
||||||
//! use mytheclipse_queue::{InMemoryQueue, WorkerPool, JobHandler, Job};
|
|
||||||
//! ...
|
|
||||||
//! let queue = InMemoryQueue::new();
|
|
||||||
//! queue.enqueue("email", b"hello".to_vec()).await?;
|
|
||||||
//!
|
|
||||||
//! fn make_handler() -> impl JobHandler {
|
|
||||||
//! struct PrintHandler;
|
|
||||||
//! impl JobHandler for PrintHandler {
|
|
||||||
//! fn handle(&self, job: Job) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), mytheclipse_queue::JobError>> + Send>> {
|
|
||||||
//! Box::pin(async move {
|
|
||||||
//! println!("payload: {:?}", job.payload);
|
|
||||||
//! Ok(())
|
|
||||||
//! })
|
|
||||||
//! }
|
|
||||||
//! }
|
|
||||||
//! PrintHandler
|
|
||||||
//! }
|
|
||||||
//!
|
|
||||||
//! # #[tokio::main]
|
|
||||||
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
//! let queue = InMemoryQueue::new();
|
|
||||||
//! queue.enqueue("email", b"hello".to_vec()).await?;
|
|
||||||
//!
|
|
||||||
//! let pool = WorkerPool::new(queue, 4);
|
|
||||||
//! pool.start("email", make_handler());
|
|
||||||
//! # Ok(())
|
|
||||||
//! # }
|
|
||||||
//! ```
|
|
||||||
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod job;
|
pub mod job;
|
||||||
@@ -55,12 +18,19 @@ pub mod traits;
|
|||||||
pub mod worker;
|
pub mod worker;
|
||||||
|
|
||||||
#[cfg(feature = "in-memory")]
|
#[cfg(feature = "in-memory")]
|
||||||
pub use in_memory::InMemoryQueue;
|
pub mod batch;
|
||||||
|
#[cfg(feature = "in-memory")]
|
||||||
pub use traits::Queue;
|
pub mod backpressure_enqueue;
|
||||||
pub use job::{Job, JobId};
|
#[cfg(feature = "in-memory")]
|
||||||
pub use worker::{WorkerPool, WorkerConfig, JobHandler, JobFuture};
|
pub mod rate_limited;
|
||||||
pub use error::{QueueError, JobError};
|
#[cfg(feature = "in-memory")]
|
||||||
|
pub mod worker_rate_limited;
|
||||||
|
#[cfg(feature = "in-memory")]
|
||||||
|
pub use backpressure_enqueue::{BackpressureEnforcer, BackpressureError, enqueue_with_backpressure};
|
||||||
|
#[cfg(feature = "in-memory")]
|
||||||
|
pub use rate_limited::{RateLimitedQueue, RateLimitQueueError};
|
||||||
|
#[cfg(feature = "in-memory")]
|
||||||
|
pub use worker_rate_limited::RateLimitedWorkerPool;
|
||||||
|
|
||||||
#[cfg(feature = "in-memory")]
|
#[cfg(feature = "in-memory")]
|
||||||
pub mod pipeline;
|
pub mod pipeline;
|
||||||
|
|||||||
@@ -0,0 +1,157 @@
|
|||||||
|
//! Rate-limited queue wrapper.
|
||||||
|
//!
|
||||||
|
//! [`RateLimitedQueue`] wraps any [`Queue`] implementation and applies a
|
||||||
|
//! token-bucket rate limiter before enqueuing. If the bucket is empty the
|
||||||
|
//! enqueue is rejected with [`RateLimitQueueError::RateLimited`] instead of
|
||||||
|
//! blocking.
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
use tokio::time::Instant;
|
||||||
|
|
||||||
|
use crate::error::QueueError;
|
||||||
|
use crate::traits::Queue;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
|
/// Error returned by [`RateLimitedQueue::enqueue`].
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum RateLimitQueueError {
|
||||||
|
RateLimited,
|
||||||
|
Other(QueueError),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for RateLimitQueueError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::RateLimited => write!(f, "rate limited: capacity exhausted"),
|
||||||
|
Self::Other(e) => write!(f, "queue error: {e}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for RateLimitQueueError {}
|
||||||
|
|
||||||
|
impl From<QueueError> for RateLimitQueueError {
|
||||||
|
fn from(e: QueueError) -> Self {
|
||||||
|
Self::Other(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<RateLimitQueueError> for QueueError {
|
||||||
|
fn from(e: RateLimitQueueError) -> Self {
|
||||||
|
match e {
|
||||||
|
RateLimitQueueError::RateLimited => Self::RateLimit("capacity exhausted".into()),
|
||||||
|
RateLimitQueueError::Other(e) => e,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Token-bucket rate limiter (no extra deps beyond tokio).
|
||||||
|
struct TokenBucket {
|
||||||
|
/// Maximum burst capacity.
|
||||||
|
capacity: u32,
|
||||||
|
/// Current tokens (float for fractional refill).
|
||||||
|
tokens: f64,
|
||||||
|
/// Refill rate (tokens per second).
|
||||||
|
rate: f64,
|
||||||
|
/// Last refill timestamp.
|
||||||
|
last: Instant,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TokenBucket {
|
||||||
|
fn new(rate_per_sec: f64, burst: u32) -> Self {
|
||||||
|
Self {
|
||||||
|
capacity: burst.max(1),
|
||||||
|
tokens: burst as f64,
|
||||||
|
rate: rate_per_sec.max(0.0),
|
||||||
|
last: Instant::now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attempts to consume one token. Returns true on success.
|
||||||
|
fn try_consume(&mut self) -> bool {
|
||||||
|
let now = Instant::now();
|
||||||
|
let elapsed = now.saturating_duration_since(self.last).as_secs_f64();
|
||||||
|
self.tokens = (self.tokens + elapsed * self.rate).min(self.capacity as f64);
|
||||||
|
self.last = now;
|
||||||
|
if self.tokens >= 1.0 {
|
||||||
|
self.tokens -= 1.0;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A queue decorator that enforces a rate limit on enqueue.
|
||||||
|
pub struct RateLimitedQueue<Q: ?Sized> {
|
||||||
|
inner: Arc<Q>,
|
||||||
|
bucket: Arc<Mutex<TokenBucket>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Q: Queue + 'static> RateLimitedQueue<Q> {
|
||||||
|
/// Creates a new rate-limited wrapper around `inner`.
|
||||||
|
pub fn new(inner: Q, rate_per_sec: f64, burst: u32) -> Self {
|
||||||
|
Self {
|
||||||
|
inner: Arc::new(inner),
|
||||||
|
bucket: Arc::new(Mutex::new(TokenBucket::new(rate_per_sec, burst))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl<Q: Queue + ?Sized> Queue for RateLimitedQueue<Q> {
|
||||||
|
async fn enqueue(&self, topic: &str, payload: Vec<u8>) -> Result<(), QueueError> {
|
||||||
|
let mut b = self.bucket.lock().await;
|
||||||
|
if !b.try_consume() {
|
||||||
|
return Err(RateLimitQueueError::RateLimited.into());
|
||||||
|
}
|
||||||
|
self.inner.enqueue(topic, payload).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn dequeue(&self, topic: &str, timeout: Duration) -> Result<Option<crate::job::Job>, QueueError> {
|
||||||
|
self.inner.dequeue(topic, timeout).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn ack(&self, job: &crate::job::Job) -> Result<(), crate::error::JobError> {
|
||||||
|
self.inner.ack(job).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn nack(&self, job: &crate::job::Job, requeue: bool) -> Result<(), crate::error::JobError> {
|
||||||
|
self.inner.nack(job, requeue).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn dlq_move(&self, topic: &str, job: crate::job::Job) -> Result<(), QueueError> {
|
||||||
|
self.inner.dlq_move(topic, job).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn len(&self, topic: &str) -> Result<u64, QueueError> {
|
||||||
|
self.inner.len(topic).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::in_memory::InMemoryQueue;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn allows_enqueue_within_rate() {
|
||||||
|
let inner = InMemoryQueue::new();
|
||||||
|
let rl = RateLimitedQueue::new(inner, 100.0, 10);
|
||||||
|
assert!(rl.enqueue("t", b"x".to_vec()).await.is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn rejects_when_bucket_empty() {
|
||||||
|
let inner = InMemoryQueue::new();
|
||||||
|
let rl = RateLimitedQueue::new(inner, 0.0, 1); // 0 tokens/sec, 1 burst
|
||||||
|
// consume the single burst token
|
||||||
|
let _ = rl.enqueue("t", b"x".to_vec()).await;
|
||||||
|
// next should be rate limited (no refill)
|
||||||
|
let result = rl.enqueue("t", b"y".to_vec()).await;
|
||||||
|
assert!(matches!(result, Err(QueueError::RateLimit(_))));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
//! Rate-limited worker pool (feature `in-memory`).
|
||||||
|
//!
|
||||||
|
//! [`RateLimitedWorkerPool`] wraps [`crate::worker::WorkerPool`] with a
|
||||||
|
//! [`crate::rate_limited::RateLimitedQueue`] to back-pressure dequeue when the
|
||||||
|
//! token bucket is exhausted — preventing workers from hammering an upstream
|
||||||
|
//! service faster than its rate limit allows.
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use crate::rate_limited::RateLimitedQueue;
|
||||||
|
use crate::worker::{JobHandler, WorkerConfig, WorkerPool};
|
||||||
|
use crate::traits::Queue;
|
||||||
|
|
||||||
|
/// A `WorkerPool` whose dequeue is rate-limited via a token bucket.
|
||||||
|
pub struct RateLimitedWorkerPool<Q: Queue + 'static> {
|
||||||
|
inner: WorkerPool<RateLimitedQueue<Q>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Q: Queue + 'static> RateLimitedWorkerPool<Q> {
|
||||||
|
/// Creates a rate-limited worker pool wrapping `queue` with the given
|
||||||
|
/// token-bucket rate (tokens/sec) and burst capacity.
|
||||||
|
pub fn new(queue: Q, worker_cfg: WorkerConfig, rate_per_sec: f64, burst: u32) -> Self {
|
||||||
|
let limited = RateLimitedQueue::new(queue, rate_per_sec, burst);
|
||||||
|
Self {
|
||||||
|
inner: WorkerPool::with_config(limited, worker_cfg),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Starts workers consuming from `topic` with the given handler.
|
||||||
|
pub fn start<H>(&self, topic: &str, handler: H)
|
||||||
|
where
|
||||||
|
H: JobHandler + 'static,
|
||||||
|
{
|
||||||
|
self.inner.start(topic, handler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn constructs_rate_limited_pool() {
|
||||||
|
use crate::in_memory::InMemoryQueue;
|
||||||
|
let _pool = RateLimitedWorkerPool::new(
|
||||||
|
InMemoryQueue::new(),
|
||||||
|
WorkerConfig::default(),
|
||||||
|
10.0,
|
||||||
|
5,
|
||||||
|
);
|
||||||
|
// smoke: just verifies construction
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mytheclipse-storage"
|
name = "mytheclipse-storage"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.75"
|
rust-version = "1.75"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mytheclipse-tracing"
|
name = "mytheclipse-tracing"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.75"
|
rust-version = "1.75"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mytheclipse"
|
name = "mytheclipse"
|
||||||
version = "1.6.0"
|
version = "1.14.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.75"
|
rust-version = "1.75"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
@@ -34,7 +34,7 @@ bg = ["dep:tokio"]
|
|||||||
resiliency = ["dep:tokio", "dep:rand"]
|
resiliency = ["dep:tokio", "dep:rand"]
|
||||||
traffic = ["dep:tokio"]
|
traffic = ["dep:tokio"]
|
||||||
lifecycle = ["dep:tokio"]
|
lifecycle = ["dep:tokio"]
|
||||||
observability = ["dep:tokio"]
|
observability = ["dep:tokio", "lifecycle"]
|
||||||
full = ["io", "compute", "bg", "resiliency", "traffic", "lifecycle", "observability"]
|
full = ["io", "compute", "bg", "resiliency", "traffic", "lifecycle", "observability"]
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
|
|||||||
@@ -0,0 +1,135 @@
|
|||||||
|
//! Auto-metrics service builder (feature `observability`).
|
||||||
|
//!
|
||||||
|
//! [`AutoMetricsServiceBuilder`] composes a [`crate::ServiceBuilder`] with a
|
||||||
|
//! [`crate::metrics::MetricsCollector`] so that every call recorded through
|
||||||
|
//! `.run()` automatically:
|
||||||
|
//!
|
||||||
|
//! - increments a `mytheclipse_service_calls_total` counter (labelled by
|
||||||
|
//! outcome `ok` / `err` / `timeout` / `circuit_open` / `rate_limited`),
|
||||||
|
//! - observes a `mytheclipse_service_duration_seconds` histogram,
|
||||||
|
//! - forwards the result to a [`crate::metrics_bridge::MetricsBridge`] when
|
||||||
|
//! one is attached (e.g. for OpenTelemetry export).
|
||||||
|
//!
|
||||||
|
//! This removes the need for callers to hand-wire tracing/metering at every
|
||||||
|
//! call site.
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use crate::metrics::MetricsCollector;
|
||||||
|
use crate::service_builder::{RunError, ServiceBuilder, ServiceConfig};
|
||||||
|
|
||||||
|
/// A [`ServiceBuilder`] wrapper that auto-records latency and outcome metrics.
|
||||||
|
pub struct AutoMetricsServiceBuilder {
|
||||||
|
inner: ServiceBuilder,
|
||||||
|
metrics: MetricsCollector,
|
||||||
|
#[cfg(feature = "resiliency")]
|
||||||
|
bridge: Option<crate::metrics_bridge::MetricsBridge>,
|
||||||
|
service_name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AutoMetricsServiceBuilder {
|
||||||
|
/// Creates a new auto-metrics builder around a base [`ServiceConfig`].
|
||||||
|
pub fn new(service_name: impl Into<String>, config: ServiceConfig) -> Self {
|
||||||
|
Self {
|
||||||
|
inner: ServiceBuilder::new(config),
|
||||||
|
metrics: MetricsCollector::new(),
|
||||||
|
#[cfg(feature = "resiliency")]
|
||||||
|
bridge: None,
|
||||||
|
service_name: service_name.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the underlying [`ServiceBuilder`] (e.g. to attach a circuit
|
||||||
|
/// breaker) and returns a fresh [`AutoMetricsServiceBuilder`].
|
||||||
|
pub fn with_builders(self, inner: ServiceBuilder) -> Self {
|
||||||
|
Self {
|
||||||
|
inner,
|
||||||
|
metrics: self.metrics,
|
||||||
|
#[cfg(feature = "resiliency")]
|
||||||
|
bridge: self.bridge,
|
||||||
|
service_name: self.service_name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attaches a [`MetricsCollector`] to share with the caller (so the caller
|
||||||
|
/// can scrape/export the same counters it records here).
|
||||||
|
pub fn with_collector(mut self, m: MetricsCollector) -> Self {
|
||||||
|
self.metrics = m;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attaches a [`MetricsBridge`] to forward snapshots downstream (requires
|
||||||
|
/// the `resiliency` feature which pulls in the bridge).
|
||||||
|
#[cfg(feature = "resiliency")]
|
||||||
|
pub fn with_bridge(mut self, bridge: crate::metrics_bridge::MetricsBridge) -> Self {
|
||||||
|
self.bridge = Some(bridge);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a shared [`MetricsCollector`] handle.
|
||||||
|
pub fn collector(&self) -> MetricsCollector {
|
||||||
|
self.metrics.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs a service call, auto-recording metrics around the outcome.
|
||||||
|
pub async fn run<F, T, E>(&self, f: F) -> Result<T, RunError<E>>
|
||||||
|
where
|
||||||
|
F: FnMut() -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<T, E>> + Send>>,
|
||||||
|
E: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
let start = std::time::Instant::now();
|
||||||
|
let result = self.inner.run(f).await;
|
||||||
|
let dur: Duration = start.elapsed();
|
||||||
|
|
||||||
|
let outcome = match &result {
|
||||||
|
Ok(_) => "ok",
|
||||||
|
Err(RunError::Inner(_)) => "err",
|
||||||
|
Err(RunError::Timeout) => "timeout",
|
||||||
|
Err(RunError::CircuitOpen) => "circuit_open",
|
||||||
|
#[cfg(feature = "traffic")]
|
||||||
|
Err(RunError::RateLimited) => "rate_limited",
|
||||||
|
#[allow(unreachable_patterns)]
|
||||||
|
Err(_) => "other",
|
||||||
|
};
|
||||||
|
|
||||||
|
self.metrics
|
||||||
|
.inc_counter(&format!("mytheclipse_service_calls_total{{service=\"{}\",outcome=\"{}\"}}", self.service_name, outcome), 1);
|
||||||
|
self.metrics
|
||||||
|
.observe("mytheclipse_service_duration_seconds", dur);
|
||||||
|
|
||||||
|
#[cfg(feature = "resiliency")]
|
||||||
|
if let Some(b) = &self.bridge {
|
||||||
|
b.emit_now();
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::atomic::{Ordering, AtomicU32};
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn auto_metrics_records_call() {
|
||||||
|
let mut cfg = ServiceConfig::default();
|
||||||
|
cfg.max_attempts = 3;
|
||||||
|
let builder = AutoMetricsServiceBuilder::new("test_svc", cfg);
|
||||||
|
|
||||||
|
let attempts = Arc::new(AtomicU32::new(0));
|
||||||
|
let a = Arc::clone(&attempts);
|
||||||
|
let result: Result<u32, RunError<()>> = builder.run(|| {
|
||||||
|
let a = Arc::clone(&a);
|
||||||
|
Box::pin(async move {
|
||||||
|
let n = a.fetch_add(1, Ordering::SeqCst);
|
||||||
|
if n < 2 { Err(()) } else { Ok(42u32) }
|
||||||
|
})
|
||||||
|
}).await;
|
||||||
|
assert_eq!(result.unwrap(), 42);
|
||||||
|
assert_eq!(attempts.load(Ordering::SeqCst), 3);
|
||||||
|
let snap = builder.collector().snapshot();
|
||||||
|
assert!(snap.counters.len() >= 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
//! Graceful task-joiner for background tasks (feature `lifecycle`).
|
||||||
|
//!
|
||||||
|
//! [`BgJoiner`] collects [`tokio::task::JoinHandle`]s returned by
|
||||||
|
//! [`crate::spawn_bg`] (or any manual `tokio::spawn`) and drains them in
|
||||||
|
//! aggregate on shutdown via [`BgJoiner::join_all`].
|
||||||
|
//!
|
||||||
|
//! This complements the bounded `spawn_bg` helper: while `spawn_bg` limits
|
||||||
|
//! *concurrency*, `BgJoiner` adds structured *lifetimes* so a service can wait
|
||||||
|
//! for all in-flight work to settle before terminating.
|
||||||
|
|
||||||
|
use std::future::Future;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
use tokio::task::JoinHandle;
|
||||||
|
use tokio::time::Instant;
|
||||||
|
|
||||||
|
/// A joiner that tracks background task handles for ordered shutdown.
|
||||||
|
#[derive(Default, Clone)]
|
||||||
|
pub struct BgJoiner {
|
||||||
|
inner: Arc<Mutex<Vec<JoinHandle<()>>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BgJoiner {
|
||||||
|
/// Creates an empty joiner.
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
inner: Arc::new(Mutex::new(Vec::new())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Spawns `future` as a background task and tracks its handle.
|
||||||
|
pub fn spawn<F>(&self, future: F)
|
||||||
|
where
|
||||||
|
F: std::future::Future + Send + 'static,
|
||||||
|
F::Output: Send + 'static,
|
||||||
|
{
|
||||||
|
let handle: JoinHandle<()> = tokio::spawn(async move { let _ = future.await; });
|
||||||
|
self.track(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Registers an externally-created `JoinHandle` for tracking.
|
||||||
|
pub fn track(&self, handle: JoinHandle<()>) {
|
||||||
|
// can't lock synchronously; defer to a spawned task
|
||||||
|
let inner = Arc::clone(&self.inner);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let mut set = inner.lock().await;
|
||||||
|
set.push(handle);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Number of currently-tracked tasks.
|
||||||
|
pub async fn len(&self) -> usize {
|
||||||
|
self.inner.lock().await.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Await every tracked task, dropping any that are still pending once
|
||||||
|
/// `deadline` elapses. Returns the count of tasks that had not completed
|
||||||
|
/// within the timeout.
|
||||||
|
pub async fn join_all(&self, deadline: Duration) -> usize {
|
||||||
|
let now = Instant::now();
|
||||||
|
let handles: Vec<JoinHandle<()>> = {
|
||||||
|
let mut guard = self.inner.lock().await;
|
||||||
|
std::mem::take(&mut *guard)
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut pending: Vec<JoinHandle<()>> = handles;
|
||||||
|
let mut dropped = 0usize;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if pending.is_empty() {
|
||||||
|
break 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if now.elapsed() >= deadline {
|
||||||
|
dropped = pending.len();
|
||||||
|
for h in pending.drain(..) {
|
||||||
|
h.abort();
|
||||||
|
}
|
||||||
|
return dropped;
|
||||||
|
}
|
||||||
|
|
||||||
|
let remaining = deadline.saturating_sub(now.elapsed());
|
||||||
|
let mut still = Vec::with_capacity(pending.len());
|
||||||
|
for mut handle in pending.drain(..) {
|
||||||
|
match tokio::time::timeout(remaining, &mut handle).await {
|
||||||
|
Ok(Ok(_)) => {}
|
||||||
|
Ok(Err(_)) => {}
|
||||||
|
Err(_) => still.push(handle),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pending = still;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Drops (aborts) all tracked tasks immediately without awaiting.
|
||||||
|
pub async fn abort_all(&self) {
|
||||||
|
let handles: Vec<JoinHandle<()>> = {
|
||||||
|
let mut guard = self.inner.lock().await;
|
||||||
|
std::mem::take(&mut *guard)
|
||||||
|
};
|
||||||
|
for h in handles {
|
||||||
|
h.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn spawn_and_join_completes() {
|
||||||
|
let joiner = BgJoiner::new();
|
||||||
|
joiner.spawn(async { tokio::task::yield_now().await });
|
||||||
|
// give the task a moment to register
|
||||||
|
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||||
|
let leftover = joiner.join_all(Duration::from_secs(1)).await;
|
||||||
|
assert_eq!(leftover, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn join_all_aborts_on_timeout() {
|
||||||
|
let joiner = BgJoiner::new();
|
||||||
|
joiner.spawn(async {
|
||||||
|
tokio::time::sleep(Duration::from_secs(10)).await;
|
||||||
|
});
|
||||||
|
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||||
|
let leftover = joiner.join_all(Duration::from_millis(50)).await;
|
||||||
|
assert!(leftover > 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,7 +36,7 @@ struct RegisteredCheck {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Registry of health checks for aggregated /health reporting.
|
/// Registry of health checks for aggregated /health reporting.
|
||||||
#[derive(Default)]
|
#[derive(Default, Clone)]
|
||||||
pub struct HealthRegistry {
|
pub struct HealthRegistry {
|
||||||
checks: Arc<RwLock<Vec<RegisteredCheck>>>,
|
checks: Arc<RwLock<Vec<RegisteredCheck>>>,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,14 @@ pub mod bg;
|
|||||||
#[cfg(feature = "resiliency")]
|
#[cfg(feature = "resiliency")]
|
||||||
pub mod retry;
|
pub mod retry;
|
||||||
#[cfg(feature = "resiliency")]
|
#[cfg(feature = "resiliency")]
|
||||||
|
pub mod retry_ext;
|
||||||
|
#[cfg(feature = "resiliency")]
|
||||||
|
pub use retry_ext::RetryExt;
|
||||||
|
#[cfg(feature = "observability")]
|
||||||
|
pub mod auto_metrics_service;
|
||||||
|
#[cfg(feature = "observability")]
|
||||||
|
pub use auto_metrics_service::AutoMetricsServiceBuilder;
|
||||||
|
#[cfg(feature = "resiliency")]
|
||||||
pub mod circuit_breaker;
|
pub mod circuit_breaker;
|
||||||
#[cfg(feature = "resiliency")]
|
#[cfg(feature = "resiliency")]
|
||||||
pub mod timeout;
|
pub mod timeout;
|
||||||
@@ -53,11 +61,28 @@ pub mod shutdown;
|
|||||||
pub mod cron;
|
pub mod cron;
|
||||||
#[cfg(feature = "lifecycle")]
|
#[cfg(feature = "lifecycle")]
|
||||||
pub mod health;
|
pub mod health;
|
||||||
|
#[cfg(all(feature = "observability", feature = "traffic"))]
|
||||||
|
pub mod pool_health;
|
||||||
#[cfg(feature = "lifecycle")]
|
#[cfg(feature = "lifecycle")]
|
||||||
pub mod leader;
|
pub mod leader;
|
||||||
#[cfg(feature = "lifecycle")]
|
#[cfg(feature = "lifecycle")]
|
||||||
pub mod lifecycle;
|
pub mod lifecycle;
|
||||||
|
|
||||||
|
#[cfg(feature = "lifecycle")]
|
||||||
|
pub mod bg_join;
|
||||||
|
#[cfg(feature = "lifecycle")]
|
||||||
|
pub mod runtime_auto;
|
||||||
|
#[cfg(feature = "lifecycle")]
|
||||||
|
pub use runtime_auto::{available_parallelism, RuntimeConfig};
|
||||||
|
|
||||||
|
#[cfg(feature = "lifecycle")]
|
||||||
|
pub mod shutdown_guard;
|
||||||
|
#[cfg(feature = "lifecycle")]
|
||||||
|
pub use shutdown_guard::ShutdownGuard;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "observability", feature = "resiliency"))]
|
||||||
|
pub mod middleware;
|
||||||
|
|
||||||
#[cfg(feature = "observability")]
|
#[cfg(feature = "observability")]
|
||||||
pub mod metrics;
|
pub mod metrics;
|
||||||
#[cfg(feature = "observability")]
|
#[cfg(feature = "observability")]
|
||||||
@@ -106,7 +131,7 @@ pub use health::{HealthCheck, HealthRegistry, HealthStatus};
|
|||||||
pub use leader::{InProcLeaderElection, LeaderElection};
|
pub use leader::{InProcLeaderElection, LeaderElection};
|
||||||
|
|
||||||
#[cfg(feature = "resiliency")]
|
#[cfg(feature = "resiliency")]
|
||||||
pub use service_builder::ServiceBuilder;
|
pub use service_builder::{RunError, ServiceBuilder, ServiceConfig};
|
||||||
|
|
||||||
#[cfg(feature = "lifecycle")]
|
#[cfg(feature = "lifecycle")]
|
||||||
pub use dlock::{DistributedLock, LockError, LockGuard, InProcLock};
|
pub use dlock::{DistributedLock, LockError, LockGuard, InProcLock};
|
||||||
@@ -117,6 +142,22 @@ pub use lifecycle::AsyncLifecycleManager;
|
|||||||
pub use metrics::{MetricsCollector, MetricsSnapshot};
|
pub use metrics::{MetricsCollector, MetricsSnapshot};
|
||||||
#[cfg(feature = "observability")]
|
#[cfg(feature = "observability")]
|
||||||
pub use metrics_bridge::{MetricsBridge, MetricsHealthCheck};
|
pub use metrics_bridge::{MetricsBridge, MetricsHealthCheck};
|
||||||
|
|
||||||
|
/// Re-export of [`metrics_bridge::CircuitBreakerHealthCheck`].
|
||||||
|
/// Only compiled when both `observability` and `resiliency` are enabled.
|
||||||
|
#[cfg(all(feature = "observability", feature = "resiliency"))]
|
||||||
|
pub use metrics_bridge::CircuitBreakerHealthCheck;
|
||||||
|
|
||||||
|
/// Re-export of [`pool_health::HealthCheckedPool`].
|
||||||
|
/// Only compiled when both `observability` and `traffic` are enabled.
|
||||||
|
#[cfg(all(feature = "observability", feature = "traffic"))]
|
||||||
|
pub use pool_health::HealthCheckedPool;
|
||||||
|
|
||||||
|
#[cfg(feature = "lifecycle")]
|
||||||
|
pub use bg_join::BgJoiner;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "observability", feature = "resiliency"))]
|
||||||
|
pub use middleware::{MiddlewarePipeline, PipelineError, BoxMiddleware, mw};
|
||||||
#[cfg(feature = "observability")]
|
#[cfg(feature = "observability")]
|
||||||
pub use panic_tracker::{PanicGuard, PanicInfo, PanicTracker};
|
pub use panic_tracker::{PanicGuard, PanicInfo, PanicTracker};
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,41 @@ use std::time::Duration;
|
|||||||
use crate::health::{HealthCheck, HealthStatus};
|
use crate::health::{HealthCheck, HealthStatus};
|
||||||
use crate::metrics::MetricsCollector;
|
use crate::metrics::MetricsCollector;
|
||||||
|
|
||||||
|
/// A health check backed by a [`CircuitBreaker`]: unhealthy if open,
|
||||||
|
/// degraded if half-open, ok otherwise.
|
||||||
|
///
|
||||||
|
/// Only available when both `resiliency` and `observability` features are
|
||||||
|
/// enabled (circuit breaker + health/metrics bridge).
|
||||||
|
#[cfg(feature = "resiliency")]
|
||||||
|
pub struct CircuitBreakerHealthCheck {
|
||||||
|
breaker: crate::circuit_breaker::CircuitBreaker,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "resiliency")]
|
||||||
|
impl CircuitBreakerHealthCheck {
|
||||||
|
pub fn new(breaker: crate::circuit_breaker::CircuitBreaker) -> Self {
|
||||||
|
Self { breaker }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "resiliency")]
|
||||||
|
impl HealthCheck for CircuitBreakerHealthCheck {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"circuit_breaker"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check(&self) -> std::pin::Pin<Box<dyn std::future::Future<Output = HealthStatus> + Send + '_>> {
|
||||||
|
let state = self.breaker.snapshot().state;
|
||||||
|
Box::pin(async move {
|
||||||
|
match state {
|
||||||
|
crate::circuit_breaker::CircuitState::Open => HealthStatus::Unhealthy,
|
||||||
|
crate::circuit_breaker::CircuitState::HalfOpen => HealthStatus::Degraded,
|
||||||
|
crate::circuit_breaker::CircuitState::Closed => HealthStatus::Ok,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A health check backed by a [`MetricsCollector`]: unhealthy if any registered
|
/// A health check backed by a [`MetricsCollector`]: unhealthy if any registered
|
||||||
/// "error" counter is non-zero, degraded if any gauge is below a configured
|
/// "error" counter is non-zero, degraded if any gauge is below a configured
|
||||||
/// threshold.
|
/// threshold.
|
||||||
@@ -142,6 +177,7 @@ mod tests {
|
|||||||
bridge.emit_now();
|
bridge.emit_now();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "lifecycle")]
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn lifecycle_manager_with_metrics_bridge() {
|
async fn lifecycle_manager_with_metrics_bridge() {
|
||||||
let collector = MetricsCollector::new();
|
let collector = MetricsCollector::new();
|
||||||
|
|||||||
@@ -0,0 +1,108 @@
|
|||||||
|
//! Composable middleware pipeline (feature `observability` + `resiliency`).
|
||||||
|
//!
|
||||||
|
//! [`MiddlewarePipeline`] is an ordered stack of boxed async functions. Each
|
||||||
|
//! stage receives the state, may transform or reject it, and returns control
|
||||||
|
//! to the next stage. The final state is delivered to a caller-supplied
|
||||||
|
//! service closure.
|
||||||
|
|
||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
/// Error returned by [`MiddlewarePipeline::apply`].
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct PipelineError {
|
||||||
|
pub msg: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for PipelineError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "pipeline error: {}", self.msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for PipelineError {}
|
||||||
|
|
||||||
|
/// A single async middleware stage.
|
||||||
|
pub type BoxMiddleware<S> = Arc<
|
||||||
|
dyn Fn(S) -> Pin<Box<dyn Future<Output = Result<S, PipelineError>> + Send>>
|
||||||
|
+ Send
|
||||||
|
+ Sync,
|
||||||
|
>;
|
||||||
|
|
||||||
|
/// Helper to box any `async fn` middleware.
|
||||||
|
pub fn mw<S, F, Fut>(f: F) -> BoxMiddleware<S>
|
||||||
|
where
|
||||||
|
S: Send + 'static,
|
||||||
|
F: Fn(S) -> Fut + Send + Sync + 'static,
|
||||||
|
Fut: Future<Output = Result<S, PipelineError>> + Send + 'static,
|
||||||
|
{
|
||||||
|
Arc::new(move |state| Box::pin(f(state)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A stack of ordered middleware stages.
|
||||||
|
#[derive(Clone, Default)]
|
||||||
|
pub struct MiddlewarePipeline<S> {
|
||||||
|
layers: Arc<Mutex<Vec<BoxMiddleware<S>>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S: Send + 'static> MiddlewarePipeline<S> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self { layers: Arc::new(Mutex::new(Vec::new())) }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Appends a middleware stage.
|
||||||
|
pub fn add(&self, m: BoxMiddleware<S>) {
|
||||||
|
self.layers.lock().unwrap().push(m);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Applies every layer in order, short-circuiting on the first error.
|
||||||
|
pub async fn apply(&self, state: S) -> Result<S, PipelineError> {
|
||||||
|
let layers = self.layers.lock().unwrap();
|
||||||
|
let mut current = state;
|
||||||
|
for layer in layers.iter() {
|
||||||
|
current = layer(current).await?;
|
||||||
|
}
|
||||||
|
Ok(current)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Applies every layer, then runs `svc` with the final state.
|
||||||
|
pub async fn run<F, Fut, R, E>(&self, state: S, svc: F) -> Result<R, E>
|
||||||
|
where
|
||||||
|
F: Fn(S) -> Fut + Clone + Send + 'static,
|
||||||
|
Fut: Future<Output = Result<R, E>> + Send,
|
||||||
|
R: Send + 'static,
|
||||||
|
E: From<PipelineError> + Send,
|
||||||
|
{
|
||||||
|
match self.apply(state).await {
|
||||||
|
Ok(s) => svc(s).await,
|
||||||
|
Err(e) => Err(E::from(e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn applies_two_layers_in_order() {
|
||||||
|
let p: MiddlewarePipeline<u32> = MiddlewarePipeline::new();
|
||||||
|
let inc = mw(|s: u32| async move { Ok::<_, PipelineError>(s + 1) });
|
||||||
|
let double = mw(|s: u32| async move { Ok::<_, PipelineError>(s * 2) });
|
||||||
|
p.add(inc);
|
||||||
|
p.add(double);
|
||||||
|
let out = p.apply(1).await.unwrap();
|
||||||
|
assert_eq!(out, 4); // (1+1)*2
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn short_circuits_on_error() {
|
||||||
|
let p: MiddlewarePipeline<String> = MiddlewarePipeline::new();
|
||||||
|
let reject = mw(|_s: String| async {
|
||||||
|
Err::<_, PipelineError>(PipelineError { msg: "rejected".into() })
|
||||||
|
});
|
||||||
|
p.add(reject);
|
||||||
|
assert!(matches!(p.apply("x".to_string()).await, Err(_)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,12 +3,15 @@
|
|||||||
//! Provides a `Pool` trait and a built-in `SemaphorePool<T>` implementation
|
//! Provides a `Pool` trait and a built-in `SemaphorePool<T>` implementation
|
||||||
//! that distributes items drawn from a `Vec<T>` under a counting semaphore.
|
//! that distributes items drawn from a `Vec<T>` under a counting semaphore.
|
||||||
|
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use tokio::sync::OwnedSemaphorePermit;
|
use tokio::sync::OwnedSemaphorePermit;
|
||||||
use tokio::sync::Semaphore;
|
use tokio::sync::Semaphore;
|
||||||
|
|
||||||
|
static ACQUIRE_COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||||
|
|
||||||
/// Errors returned by pool operations.
|
/// Errors returned by pool operations.
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum PoolError {
|
pub enum PoolError {
|
||||||
@@ -39,6 +42,11 @@ pub struct SemaphorePool<T: Clone> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Clone> SemaphorePool<T> {
|
impl<T: Clone> SemaphorePool<T> {
|
||||||
|
/// Returns the underlying items slice (read-only view).
|
||||||
|
pub fn items(&self) -> &[T] {
|
||||||
|
&self.items
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a new pool from a vector of items.
|
/// Creates a new pool from a vector of items.
|
||||||
pub fn new(items: Vec<T>) -> Self {
|
pub fn new(items: Vec<T>) -> Self {
|
||||||
let permits = items.len().max(1);
|
let permits = items.len().max(1);
|
||||||
@@ -54,7 +62,7 @@ impl<T: Clone + Send + Sync + 'static> Pool<T> for SemaphorePool<T> {
|
|||||||
async fn acquire(&self) -> Result<Pooled<T>, PoolError> {
|
async fn acquire(&self) -> Result<Pooled<T>, PoolError> {
|
||||||
let permit = self.semaphore.clone().acquire_owned().await
|
let permit = self.semaphore.clone().acquire_owned().await
|
||||||
.map_err(|_| PoolError::Exhausted)?;
|
.map_err(|_| PoolError::Exhausted)?;
|
||||||
let idx = rand::random::<usize>() % self.items.len();
|
let idx = ACQUIRE_COUNT.fetch_add(1, Ordering::Relaxed) % self.items.len();
|
||||||
Ok(Pooled {
|
Ok(Pooled {
|
||||||
resource: self.items[idx].clone(),
|
resource: self.items[idx].clone(),
|
||||||
_permit: permit,
|
_permit: permit,
|
||||||
|
|||||||
@@ -0,0 +1,141 @@
|
|||||||
|
//! Health-checked resource pool (feature `observability` + `traffic`).
|
||||||
|
//!
|
||||||
|
//! [`HealthCheckedPool`] composes a [`SemaphorePool`] with a [`HealthRegistry`]:
|
||||||
|
//! a background probe periodically validates pooled items, and a registered
|
||||||
|
//! `HealthCheck` reflects pool liveliness in the aggregated `/health` report.
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use crate::health::{HealthCheck, HealthRegistry, HealthStatus};
|
||||||
|
use crate::pool::{Pool, Pooled, PoolError, SemaphorePool};
|
||||||
|
|
||||||
|
/// A health check backed by a closure.
|
||||||
|
struct ClosureCheck {
|
||||||
|
name: String,
|
||||||
|
check: Arc<dyn Fn() -> HealthStatus + Send + Sync>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HealthCheck for ClosureCheck {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
&self.name
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check(&self) -> std::pin::Pin<Box<dyn std::future::Future<Output = HealthStatus> + Send + '_>> {
|
||||||
|
let status = (self.check)();
|
||||||
|
Box::pin(async move { status })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A resource pool with integrated health reporting.
|
||||||
|
pub struct HealthCheckedPool<T: Clone + Send + Sync + 'static> {
|
||||||
|
pub(crate) inner: SemaphorePool<T>,
|
||||||
|
#[allow(dead_code)]
|
||||||
|
registry: Arc<HealthRegistry>,
|
||||||
|
#[allow(dead_code)]
|
||||||
|
check_interval: Duration,
|
||||||
|
#[allow(dead_code)]
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Clone + Send + Sync + 'static + std::fmt::Debug> HealthCheckedPool<T> {
|
||||||
|
/// Creates a new health-checked pool.
|
||||||
|
///
|
||||||
|
/// `validator` is called on each item during the periodic background probe;
|
||||||
|
/// the registered health check reports `Ok` if any item validates.
|
||||||
|
pub async fn new(
|
||||||
|
items: Vec<T>,
|
||||||
|
registry: &HealthRegistry,
|
||||||
|
name: impl Into<String>,
|
||||||
|
check_interval: Duration,
|
||||||
|
validator: impl Fn(&T) -> bool + Send + Sync + 'static,
|
||||||
|
) -> Self {
|
||||||
|
let name_str = name.into();
|
||||||
|
let pool = SemaphorePool::new(items);
|
||||||
|
let registry = Arc::new(registry.clone());
|
||||||
|
|
||||||
|
let validator: Arc<dyn Fn(&T) -> bool + Send + Sync> = Arc::new(validator);
|
||||||
|
let check_items = pool.items().to_vec();
|
||||||
|
let v_check = Arc::clone(&validator);
|
||||||
|
let check = ClosureCheck {
|
||||||
|
name: format!("connection-pool:{}", name_str),
|
||||||
|
check: Arc::new(move || {
|
||||||
|
if check_items.iter().any(|i| v_check(i)) {
|
||||||
|
HealthStatus::Ok
|
||||||
|
} else {
|
||||||
|
HealthStatus::Unhealthy
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
let r = Arc::clone(®istry);
|
||||||
|
let check_name = check.name.clone();
|
||||||
|
tokio::task::spawn(async move {
|
||||||
|
r.register(check_name, check).await;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Background probe
|
||||||
|
let probe_items = pool.items().to_vec();
|
||||||
|
let probe_name = name_str.clone();
|
||||||
|
let v_probe = Arc::clone(&validator);
|
||||||
|
tokio::task::spawn(async move {
|
||||||
|
let mut ticker = tokio::time::interval(check_interval);
|
||||||
|
loop {
|
||||||
|
ticker.tick().await;
|
||||||
|
let up = probe_items.iter().filter(|i| v_probe(i)).count();
|
||||||
|
tracing::debug!(pool = %probe_name, up, total = probe_items.len(), "pool health probe");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Self {
|
||||||
|
inner: pool,
|
||||||
|
registry,
|
||||||
|
check_interval,
|
||||||
|
name: name_str,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Acquires a resource from the pool.
|
||||||
|
pub async fn acquire_healthy(&self) -> Result<Pooled<T>, PoolError> {
|
||||||
|
self.inner.acquire().await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Number of items in the pool.
|
||||||
|
pub fn size(&self) -> usize {
|
||||||
|
self.inner.items().len()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn acquires_resource() {
|
||||||
|
let reg = HealthRegistry::new();
|
||||||
|
let pool = HealthCheckedPool::new(
|
||||||
|
vec![42u32, 84u32],
|
||||||
|
®,
|
||||||
|
"test",
|
||||||
|
Duration::from_secs(5),
|
||||||
|
|_| true,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
let item = pool.acquire_healthy().await.unwrap();
|
||||||
|
assert!(item.resource == 42 || item.resource == 84);
|
||||||
|
assert_eq!(pool.size(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn validator_distinguishes_healthy() {
|
||||||
|
let reg = HealthRegistry::new();
|
||||||
|
let pool = HealthCheckedPool::new(
|
||||||
|
vec![0u32, 1u32, 2u32],
|
||||||
|
®,
|
||||||
|
"test",
|
||||||
|
Duration::from_secs(5),
|
||||||
|
|x| *x > 0,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
assert_eq!(pool.size(), 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
//! Ergonomic retry extension trait (feature `resiliency`).
|
||||||
|
//!
|
||||||
|
//! [`RetryExt`] extends any [`std::future::Future<Output = Result<T, E>>`]
|
||||||
|
//! with a `.retry()` method that delegates to [`crate::retry::retry`].
|
||||||
|
|
||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
|
||||||
|
use crate::retry::{retry, RetryConfig, RetryError};
|
||||||
|
|
||||||
|
/// Extension trait adding ergonomic `.retry()` to any fallible future.
|
||||||
|
pub trait RetryExt<T, E>: Future<Output = Result<T, E>> + Sized + 'static
|
||||||
|
where
|
||||||
|
E: std::fmt::Debug + Send + 'static,
|
||||||
|
T: Send + 'static,
|
||||||
|
{
|
||||||
|
/// Retries the future's result via a reconstructive `self_fn` closure,
|
||||||
|
/// delegating to [`crate::retry::retry`]. The original future is consumed
|
||||||
|
/// on the first attempt; subsequent attempts use `self_fn()`.
|
||||||
|
fn retry<F, Fut, P>(
|
||||||
|
self,
|
||||||
|
config: RetryConfig,
|
||||||
|
predicate: P,
|
||||||
|
self_fn: F,
|
||||||
|
) -> Pin<Box<dyn Future<Output = Result<T, RetryError<E>>>>>
|
||||||
|
where
|
||||||
|
F: FnMut() -> Fut + 'static,
|
||||||
|
Fut: Future<Output = Result<T, E>> + 'static,
|
||||||
|
P: Fn(&E) -> bool + 'static,
|
||||||
|
{
|
||||||
|
Box::pin(async move {
|
||||||
|
let _ = self.await;
|
||||||
|
retry(config, predicate, self_fn).await
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Fut, T, E> RetryExt<T, E> for Fut
|
||||||
|
where
|
||||||
|
Fut: Future<Output = Result<T, E>> + Send + 'static,
|
||||||
|
E: std::fmt::Debug + Send + 'static,
|
||||||
|
T: Send + 'static,
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::time::Duration;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::atomic::{AtomicU32, Ordering};
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn retry_ext_retries_then_succeeds() {
|
||||||
|
let attempts = Arc::new(AtomicU32::new(0));
|
||||||
|
let a = Arc::clone(&attempts);
|
||||||
|
let cfg = RetryConfig { max_attempts: 3, base_delay: Duration::from_millis(1), ..RetryConfig::default() };
|
||||||
|
|
||||||
|
let op = move || {
|
||||||
|
let a = Arc::clone(&a);
|
||||||
|
async move {
|
||||||
|
let n = a.fetch_add(1, Ordering::SeqCst);
|
||||||
|
if n < 2 { Err::<(), String>("transient".into()) } else { Ok(()) }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let fut = async { Err::<(), String>("first".into()) };
|
||||||
|
let result = fut.retry(cfg, |_: &String| true, op).await;
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(attempts.load(Ordering::SeqCst), 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
//! Auto thread/core allocation (feature `lifecycle`).
|
||||||
|
//!
|
||||||
|
//! Provides [`RuntimeConfig`] — a small builder that infers sensible thread
|
||||||
|
//! counts from the host CPU topology (via [`std::thread::available_parallelism`])
|
||||||
|
//! so callers don't have to hand-tune `worker_threads` / `max_blocking_threads`.
|
||||||
|
//!
|
||||||
|
//! The same helpers let you size a [`rayon::ThreadPoolBuilder`] or any other
|
||||||
|
//! pool builder without pulling in `num_cpus`.
|
||||||
|
//!
|
||||||
|
//! ## Rationale
|
||||||
|
//!
|
||||||
|
//! Most async/runtime configs default to *one* worker thread per core, which
|
||||||
|
//! is usually fine — but compute-heavy or blocking-heavy workloads want a
|
||||||
|
//! separate, explicit breakdown. [`RuntimeConfig`] centralises that logic so
|
||||||
|
//! you configure it once and reuse the counts everywhere.
|
||||||
|
|
||||||
|
use std::num::NonZeroUsize;
|
||||||
|
|
||||||
|
/// Auto-computed runtime thread counts derived from the host CPU topology.
|
||||||
|
///
|
||||||
|
/// Each field is a concrete `usize` (never zero) so it can be fed directly
|
||||||
|
/// into a tokio/rayon/std-thread builder with no further `max(1)` guards.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub struct RuntimeConfig {
|
||||||
|
/// Worker threads for the main async runtime (default: one per core).
|
||||||
|
pub worker_threads: usize,
|
||||||
|
/// Extra threads reserved for blocking (tokio `max_blocking_threads`).
|
||||||
|
pub max_blocking_threads: usize,
|
||||||
|
/// Rayon (compute) pool size.
|
||||||
|
pub compute_threads: usize,
|
||||||
|
/// Suggested background/IO pool size.
|
||||||
|
pub io_threads: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RuntimeConfig {
|
||||||
|
/// Sizes every pool to the host's available parallelism (one thread per
|
||||||
|
/// logical core), with a small reserved budget for blocking.
|
||||||
|
pub fn auto() -> Self {
|
||||||
|
let cores = available_parallelism();
|
||||||
|
Self {
|
||||||
|
worker_threads: cores,
|
||||||
|
max_blocking_threads: cores.saturating_add(cores / 2).max(4),
|
||||||
|
compute_threads: cores,
|
||||||
|
io_threads: cores.saturating_div(2).clamp(1, 8),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sizes pools heuristically from `cores` (useful in tests or when you
|
||||||
|
/// want to override the host topology).
|
||||||
|
pub fn from_cores(cores: usize) -> Self {
|
||||||
|
let cores = cores.max(1);
|
||||||
|
Self {
|
||||||
|
worker_threads: cores,
|
||||||
|
max_blocking_threads: cores.saturating_add(cores / 2).max(4),
|
||||||
|
compute_threads: cores,
|
||||||
|
io_threads: cores.saturating_div(2).clamp(1, 8),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A compact runtime: minimal worker + compute threads for constrained
|
||||||
|
/// environments (embedded, small containers).
|
||||||
|
pub fn compact() -> Self {
|
||||||
|
let cores = available_parallelism();
|
||||||
|
Self {
|
||||||
|
worker_threads: cores.max(2),
|
||||||
|
max_blocking_threads: 2,
|
||||||
|
compute_threads: 1,
|
||||||
|
io_threads: 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Number of the host's logical CPU cores, falling back to 1 on error.
|
||||||
|
pub fn available_parallelism() -> usize {
|
||||||
|
std::thread::available_parallelism()
|
||||||
|
.map(NonZeroUsize::get)
|
||||||
|
.unwrap_or(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Builds a `rayon::ThreadPool` sized with [`RuntimeConfig::compute_threads`].
|
||||||
|
///
|
||||||
|
/// Returns `None` when rayon isn't enabled — call this in code that's gated
|
||||||
|
/// on the `compute` feature to avoid a compile error.
|
||||||
|
#[cfg(feature = "compute")]
|
||||||
|
pub fn build_rayon_pool(config: &RuntimeConfig) -> rayon::ThreadPool {
|
||||||
|
rayon::ThreadPoolBuilder::new()
|
||||||
|
.num_threads(config.compute_threads)
|
||||||
|
.thread_name(|i| format!("mytheclipse-compute-{i}"))
|
||||||
|
.build()
|
||||||
|
.expect("failed to build rayon compute pool")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn auto_is_nonzero() {
|
||||||
|
let c = RuntimeConfig::auto();
|
||||||
|
assert!(c.worker_threads >= 1);
|
||||||
|
assert!(c.max_blocking_threads >= 4);
|
||||||
|
assert!(c.compute_threads >= 1);
|
||||||
|
assert!(c.io_threads >= 1);
|
||||||
|
assert!(c.io_threads <= 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn from_cores_clamps() {
|
||||||
|
let c = RuntimeConfig::from_cores(4);
|
||||||
|
assert_eq!(c.worker_threads, 4);
|
||||||
|
assert_eq!(c.max_blocking_threads, 6);
|
||||||
|
let one = RuntimeConfig::from_cores(0);
|
||||||
|
assert_eq!(one.worker_threads, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn compact_is_minimal() {
|
||||||
|
let c = RuntimeConfig::compact();
|
||||||
|
assert_eq!(c.compute_threads, 1);
|
||||||
|
assert_eq!(c.io_threads, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
//! RAII shutdown guard (feature `lifecycle`).
|
||||||
|
//!
|
||||||
|
//! [`ShutdownGuard`] gives background workers a *scope-based* way to announce
|
||||||
|
//! they have finished, without manually de-registering from a
|
||||||
|
//! [`crate::ShutdownManager`] — the guard fires its completion callback when
|
||||||
|
//! dropped (RAII), so even a `panic!` or an early `return` cannot leak a
|
||||||
|
//! dangling "task still running" registration.
|
||||||
|
//!
|
||||||
|
//! This complements [`crate::ShutdownManager::register`] (which tracks
|
||||||
|
//! `JoinHandle`s): a guard is useful when the work isn't behind a joinable
|
||||||
|
//! handle, or when you want to guarantee cleanup even on unwind.
|
||||||
|
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
/// An RAII guard that invokes a notification callback exactly once when
|
||||||
|
/// dropped.
|
||||||
|
///
|
||||||
|
/// The callback is wrapped in a `Mutex<Option<_>>` so it can be taken out and
|
||||||
|
/// run exactly once — even on a `panic!`-unwound drop — guaranteeing at-most-
|
||||||
|
/// once semantics (no double-shutdown race).
|
||||||
|
pub struct ShutdownGuard {
|
||||||
|
inner: Arc<Mutex<Option<Box<dyn FnOnce() + Send>>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ShutdownGuard {
|
||||||
|
/// Creates a guard that will run `on_drop` (once, on drop) to mark this
|
||||||
|
/// unit of work as complete.
|
||||||
|
pub fn new(on_drop: impl FnOnce() + Send + 'static) -> Self {
|
||||||
|
Self {
|
||||||
|
inner: Arc::new(Mutex::new(Some(Box::new(on_drop)))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Marks the work as complete immediately, invoking the callback once,
|
||||||
|
/// and disarms the guard so a later drop is a no-op.
|
||||||
|
pub fn finish(self) {
|
||||||
|
self.fire();
|
||||||
|
std::mem::forget(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fire(&self) {
|
||||||
|
let cb = self.inner.lock().unwrap().take();
|
||||||
|
if let Some(cb) = cb {
|
||||||
|
cb();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for ShutdownGuard {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.fire();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn fires_on_drop() {
|
||||||
|
let calls = Arc::new(AtomicUsize::new(0));
|
||||||
|
let c = Arc::clone(&calls);
|
||||||
|
{
|
||||||
|
let _guard = ShutdownGuard::new(move || {
|
||||||
|
c.fetch_add(1, Ordering::SeqCst);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
assert_eq!(calls.load(Ordering::SeqCst), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn finish_fires_once_and_disarms() {
|
||||||
|
let calls = Arc::new(AtomicUsize::new(0));
|
||||||
|
let c = Arc::clone(&calls);
|
||||||
|
let guard = ShutdownGuard::new(move || {
|
||||||
|
c.fetch_add(1, Ordering::SeqCst);
|
||||||
|
});
|
||||||
|
guard.finish();
|
||||||
|
assert_eq!(calls.load(Ordering::SeqCst), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn fires_exactly_once_despite_panic_path() {
|
||||||
|
let calls = Arc::new(AtomicUsize::new(0));
|
||||||
|
let c = Arc::clone(&calls);
|
||||||
|
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||||
|
let _guard = ShutdownGuard::new(move || {
|
||||||
|
c.fetch_add(1, Ordering::SeqCst);
|
||||||
|
});
|
||||||
|
panic!("boom");
|
||||||
|
}));
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(calls.load(Ordering::SeqCst), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user