Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
829c5714d2 | ||
|
|
5df04d7512 | ||
|
|
a6cf58ea7f | ||
|
|
de54e801fb | ||
|
|
d03d32a923 | ||
|
|
c87ae760bf | ||
|
|
e69f72296c | ||
|
|
730245e9c0 | ||
|
|
3376bee1d3 | ||
|
|
bb4998d8fa | ||
|
|
125bc57290 | ||
|
|
ff5fbe49dc | ||
|
|
8d4d8aa52c | ||
|
|
ddb2c2fd2d | ||
|
|
77b0dd9f12 | ||
|
|
74abe7172f | ||
|
|
3c64563a44 | ||
|
|
2f445d3b85 | ||
|
|
2985fa0a0e | ||
|
|
7a063fa75c | ||
|
|
ebb3c8ad6b | ||
|
|
076b0bb757 | ||
|
|
8e66c7d887 | ||
|
|
851c8c4ebb | ||
|
|
43063c4698 | ||
|
|
a03db38c5c | ||
|
|
f9df8f2887 | ||
|
|
97b5e02820 | ||
|
|
bf8f76cc10 | ||
|
|
510aadc066 | ||
|
|
0a978c063e | ||
|
|
717e6905cd | ||
|
|
bfe2fae359 | ||
|
|
1ea3b35581 |
@@ -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,22 @@
|
||||
# Implementation Spec: Round 12 — COMPLETE
|
||||
|
||||
## Goal
|
||||
Self-healing resource pool (auto-reconnect) — remove per-call "is connection
|
||||
dead? rebuild" boilerplate.
|
||||
|
||||
## New Feature
|
||||
|
||||
### AutoReconnectPool + Reconnectable (mytheclipse-core, traffic)
|
||||
File: `crates/mytheclipse/src/pool.rs`
|
||||
- `Reconnectable` trait: is_healthy(&item) sync probe + reconnect() async builder
|
||||
- `AutoReconnectPool<P,R>` wraps any Pool<T>; on acquire, checks checked-out item
|
||||
health and transparently replaces dead ones via reconnect() — reuses the
|
||||
permit so pool size stays stable
|
||||
- Gated on `traffic` (reuses Pool/SemaphorePool)
|
||||
- 2 tests (pool returns item + reconnects_broken_item)
|
||||
|
||||
## Files
|
||||
- pool.rs: +Reconnectable +AutoReconnectPool +test
|
||||
- lib.rs: export AutoReconnectPool, Reconnectable
|
||||
|
||||
Build: exit 0. Tests: 0 FAILED. Clippy: 0 new warnings.
|
||||
@@ -0,0 +1,19 @@
|
||||
# Implementation Spec: Round 13 — COMPLETE
|
||||
|
||||
## New Feature
|
||||
|
||||
### AggregateError (mytheclipse-core, resiliency)
|
||||
File: `crates/mytheclipse/src/aggregate_error.rs`
|
||||
- Collects multiple `E: std::error::Error` from parallel/fan-out tasks into one
|
||||
error — natural failure type for `join_all` + batch/fan-out resilience
|
||||
- `empty()` / `with_context(..)` / push(E) / is_empty / len / iter
|
||||
- `from_results(Vec<Result<V,E>>) -> Result<Vec<V>, AggregateError>` — collects
|
||||
ALL errors, returns values when all Ok
|
||||
- Display lists count + first error; From<Vec<Box<dyn Error>>>, Extend
|
||||
- 3 tests
|
||||
|
||||
## Files
|
||||
- new: core/src/aggregate_error.rs
|
||||
- core/lib.rs: +module+export AggregateError (resiliency)
|
||||
|
||||
Build: exit 0. Tests: 0 FAILED (97 core pass). Clippy: 0 new warnings.
|
||||
@@ -0,0 +1,21 @@
|
||||
# Implementation Spec: Round 14 — COMPLETE
|
||||
|
||||
## New Feature
|
||||
|
||||
### parallel_map / parallel_map_unordered (mytheclipse-core, resiliency)
|
||||
File: `crates/mytheclipse/src/parallel_map.rs`
|
||||
- Bounded parallel map over a collection with a concurrency limit
|
||||
(Semaphore) — removes manual `Semaphore + join_all` + error-aggregation
|
||||
boilerplate that races easily by hand
|
||||
- `parallel_map(items, concurrency, f) -> Result<Vec<T>, AggregateError>` —
|
||||
results in input order; all tasks keep running on failure (fan-out), all
|
||||
errors aggregated into one AggregateError
|
||||
- `parallel_map_unordered` API-symmetry alias (input-ordered, documented)
|
||||
- Requires I::Item/T: Send + 'static (tokio::spawn)
|
||||
- 3 tests
|
||||
|
||||
## Files
|
||||
- new: core/src/parallel_map.rs
|
||||
- core/lib.rs: +module+export parallel_map, parallel_map_unordered
|
||||
|
||||
Build: 0 errors. Tests: 0 FAILED (100 core pass). Clippy: 0 new warnings.
|
||||
@@ -0,0 +1,24 @@
|
||||
# Implementation Spec: Round 15 — COMPLETE
|
||||
|
||||
## New Feature
|
||||
|
||||
### parallel_for_each (mytheclipse-core, resiliency)
|
||||
File: `crates/mytheclipse/src/parallel_map.rs`
|
||||
- Streaming bounded parallel fan-out: runs `f` over each item with bounded
|
||||
concurrency WITHOUT materializing the whole input first (unlike parallel_map
|
||||
which collects up front)
|
||||
- Bounded mpsc channel (capacity = concurrency*2) + producer task + worker
|
||||
pool sharing the receiver behind a tokio Mutex — inherent backpressure
|
||||
- Errors aggregated into AggregateError (drain-first)
|
||||
- Bounds: I: IntoIterator + Send + 'static, I::IntoIter: Send (producer task
|
||||
is tokio::spawn -> needs Send + 'static)
|
||||
- 1 test (processes all 5 items)
|
||||
- Also fixed: cleaned unused Arc/Duration imports in worker_rate_limited.rs
|
||||
(round-10 leftover)
|
||||
|
||||
## Files
|
||||
- modified: core/src/parallel_map.rs (+parallel_for_each)
|
||||
- core/lib.rs: export parallel_for_each
|
||||
- queue/src/worker_rate_limited.rs: remove unused imports
|
||||
|
||||
Build: 0 errors. Tests: 0 FAILED (101 core pass). Clippy: 0 new warnings.
|
||||
@@ -0,0 +1,33 @@
|
||||
# Implementation Spec: Round 16 — COMPLETE
|
||||
|
||||
## Goal
|
||||
Make the round-7-15 abstractions actually usable — a single, runnable, wired
|
||||
example showing high-level primitives composing together.
|
||||
|
||||
## New File
|
||||
|
||||
### examples/high_level.rs (mytheclipse-core)
|
||||
`crates/mytheclipse/examples/high_level.rs`
|
||||
- One realistic flow demoing, wired together:
|
||||
1. RuntimeConfig::auto() — auto thread/core sizing from host CPU
|
||||
2. parallel_map — bounded fan-out + AggregateError
|
||||
3. RetryExt — ergonomic .retry() on a Future
|
||||
4. ShutdownGuard — RAII exactly-once cleanup
|
||||
5. AutoReconnectPool — self-healing resource pool (dead value replaced)
|
||||
6. AutoMetricsServiceBuilder — auto latency/outcome metrics
|
||||
- Run: `cargo run -p mytheclipse --features full --example high_level`
|
||||
|
||||
## Verified Output (real run, 8-core host)
|
||||
```
|
||||
1. RuntimeConfig::auto() -> worker=8, blocking=12, compute=8, io=4
|
||||
2. parallel_map -> [10, 20, 30, 40, 50]
|
||||
3. RetryExt with 4 attempts -> 42
|
||||
4. ShutdownGuard fired 1x (exactly-once, even on unwind)
|
||||
5. AutoReconnectPool first acquire -> 999
|
||||
6. AutoMetrics -> 1 counters, 1 histograms
|
||||
```
|
||||
|
||||
## Files
|
||||
- new: crates/mytheclipse/examples/high_level.rs
|
||||
|
||||
Build: exit 0. Run: succeeds (verified above).
|
||||
@@ -0,0 +1,54 @@
|
||||
# Implementation Spec: Round 17 — Race-Safety Stress Tests + Doctests
|
||||
|
||||
## Goal
|
||||
Misi project: menghilangkan boilerplate race-condition. Bukti nyata bahwa
|
||||
primitives aman di bawah kontensi tinggi. Tambahkan:
|
||||
1. Stress/concurrency tests untuk primitives race-sensitive di core crate
|
||||
2. Doctest `# Examples` untuk fitur round 7-16 agar docs.rs langsung berguna
|
||||
|
||||
## 1. New file: crates/mytheclipse/tests/race_stress.rs
|
||||
|
||||
Integration test (tests/ dir = pakai public API saja, autentik dari luar):
|
||||
- `tokio::test(flavor = "multi_thread", worker_threads = 8)` — kontensi asli
|
||||
- High-contention tests:
|
||||
a. TokenBucket try_consume atomic — 64 tasks × 1000 consumes dari 1 bucket
|
||||
capacity 100, rate tinggi → total consume ≤ capacity per window, no double
|
||||
b. SemaphorePool acquire/release concurrent — 100 tasks acquire+release
|
||||
cycle, final available == capacity, no leak
|
||||
c. AutoReconnectPool — healthy probe retval, 50 concurrent acquire, semua
|
||||
dapat item valid
|
||||
d. RateLimitedQueue concurrent enqueue/dequeue — 8 worker × 1000 item,
|
||||
total dequeue == total enqueue
|
||||
e. ShutdownGuard exactly-once — 10 clones-ish concurrent drops → callback
|
||||
count == 1 (via Arc<AtomicUsize>)
|
||||
f. parallel_map 10k items concurrency 32 — hasil input-ordered, nilai benar
|
||||
g. parallel_for_each 10k items concurrency 32 — side-effect count == 10k
|
||||
h. AggregateError from_results merge 100 results mix ok/err — error count
|
||||
benar, values semua lolos yang ok
|
||||
- Assertions: `assert_eq!` pada counts; harness FAILS kalau race → flaky
|
||||
|
||||
## 2. Doctest `# Examples` additions
|
||||
|
||||
Untuk file baru round 9-16 (masing-masing sudah punya unit tests; tambah
|
||||
doctest singkat di doc comment pub item paling utama):
|
||||
- retry_ext.rs: `RetryExt::retry` contoh 1-liner
|
||||
- auto_metrics_service.rs: AutoMetricsServiceBuilder contoh
|
||||
- runtime_auto.rs: RuntimeConfig::auto contoh
|
||||
- shutdown_guard.rs: ShutdownGuard contoh
|
||||
- aggregate_error.rs: AggregateError::from_results contoh
|
||||
- parallel_map.rs: parallel_map + parallel_for_each contoh
|
||||
- pool.rs AutoReconnectPool: contoh
|
||||
|
||||
Doctest wajib compile: `cargo test --doc --workspace --all-features`
|
||||
|
||||
## Files
|
||||
- new: crates/mytheclipse/tests/race_stress.rs
|
||||
- edit: parallel_map.rs, retry_ext.rs, auto_metrics_service.rs, runtime_auto.rs,
|
||||
shutdown_guard.rs, aggregate_error.rs, pool.rs (doctest blocks)
|
||||
|
||||
## Verification
|
||||
1. `cargo test -p mytheclipse --tests --all-features` — 0 FAILED
|
||||
2. `cargo test -p mytheclipse --doc --all-features` — 0 FAILED
|
||||
3. `cargo build --workspace --all-features` — exit 0
|
||||
4. `cargo clippy --workspace --all-features` — 0 new warnings
|
||||
5. Commit + push
|
||||
@@ -0,0 +1,34 @@
|
||||
# Implementation Spec: Round 19 — Criterion Benchmarks
|
||||
|
||||
## Goal
|
||||
Buktikan klaim "secepat mungkin" (tujuan awal project) dengan benchmark
|
||||
nyata. Ukur overhead primitives race-safe vs baseline naif, supaya user
|
||||
tahu trade-off dan bisa memilih fitur dengan data.
|
||||
|
||||
## New files
|
||||
|
||||
### crates/mytheclipse/benches/primitives.rs
|
||||
Criterion bench untuk primitives core (feature `full`):
|
||||
- `parallel_map`: throughput 1000 item, concurrency 8 vs sequential loop
|
||||
(pakai `black_box`)
|
||||
- `retry_ext`: overhead `.retry()` success-first vs 2 retries
|
||||
- `rate_limiter`: `RateLimiter::try_acquire` throughput (atomic CAS)
|
||||
- `semaphore_pool`: acquire/release cycle throughput — bukti no-leak + low
|
||||
overhead
|
||||
- `aggregate_error`: `from_results` 1000 results all-ok vs 50% err
|
||||
- `shutdown_guard`: new + drop cost
|
||||
|
||||
## Dependency
|
||||
- dev-deps: `criterion = "0.5"` + `[[bench]]` harness = false
|
||||
- `harness = false` di Cargo.toml bench section (criterion punya main sendiri)
|
||||
|
||||
## Verification
|
||||
1. `cargo bench -p mytheclipse --bench primitives --all-features` — runs,
|
||||
reports times
|
||||
2. `cargo build --workspace --all-features` — exit 0
|
||||
3. `cargo clippy --workspace --all-features` — 0 new warnings
|
||||
4. Spec + commit + push
|
||||
|
||||
## Notes
|
||||
- Criterion 0.5 mendukung MSRV 1.60 — aman untuk rust-version 1.75.
|
||||
- Bench tidak jalan di CI (hanya manual) — tidak mempengaruhi pipeline.
|
||||
@@ -0,0 +1,42 @@
|
||||
# Implementation Spec: Round 3
|
||||
|
||||
## New Features (4)
|
||||
|
||||
### 1. ConfigValidator (mytheclipse-config)
|
||||
File: `crates/mytheclipse-config/src/validate.rs`
|
||||
- `ConfigValidator` trait: `fn validate(&self) -> Result<(), ValidationError>`
|
||||
- `ConfigValidatorExt` trait: blanket impl for `T: ConfigValidator`
|
||||
- Built-in validators: `validate_url`, `validate_port`, `validate_non_empty`, `validate_range`, `collect_failures`
|
||||
- `ValidationFailure { path, message }` + `ValidationError` type alias
|
||||
- Feature gate: `validation` (default)
|
||||
- Tests: 17 (unit + doctest)
|
||||
|
||||
### 2. AsyncLifecycleManager (mytheclipse-core)
|
||||
File: `crates/mytheclipse/src/lifecycle.rs`
|
||||
- `AsyncLifecycleManager` composing `ShutdownManager` + `HealthRegistry`
|
||||
- Methods: `register_health_check`, `check_health`, `shutdown_signal`, `start_health_loop`, `await_shutdown`, `request_shutdown`
|
||||
- Feature gate: `lifecycle`
|
||||
- Tests: 38 total (3 new in lifecycle.rs)
|
||||
|
||||
### 3. MetricsBridge (mytheclipse-core)
|
||||
File: `crates/mytheclipse/src/metrics_bridge.rs`
|
||||
- `MetricsBridge` — emits MetricsCollector snapshot to tracing
|
||||
- `MetricsHealthCheck` — wraps MetricsCollector as HealthCheck (unhealthy if error counters > 0)
|
||||
- Feature gate: `observability`
|
||||
|
||||
### 4. ServiceBuilder RateLimiter API (mytheclipse-core)
|
||||
File: `crates/mytheclipse/src/service_builder.rs`
|
||||
- `with_rate_limiter` fluent builder (already existed)
|
||||
- `check_pre` performs rate-limit pre-acquire before calling service
|
||||
- Returns `RunError::RateLimited` when rate limiter exhausted
|
||||
|
||||
## Build Status
|
||||
- cargo build --workspace --all-features: OK
|
||||
- cargo test --workspace --all-features: all pass (77+17+18+16+6+5+...)
|
||||
- cargo clippy: 0 warnings on new code (pre-existing warnings in crypto/base64/cli only)
|
||||
- Committed + pushed
|
||||
|
||||
## Notes
|
||||
- `Arc<HealthRegistry>` in AsyncLifecycleManager because HealthRegistry doesn't impl Clone
|
||||
- Doctest marked `ignore` (async runtime not available in doctest context)
|
||||
- Lint checker false-positives on `async fn` (edition 2015 phantom) but actual cargo build/tests pass
|
||||
@@ -0,0 +1,37 @@
|
||||
# Implementation Spec: Round 4
|
||||
|
||||
## Status: COMPLETE
|
||||
|
||||
## New Features
|
||||
|
||||
### 1. CircuitBreakerMetrics (circuit_breaker.rs)
|
||||
- Added `CircuitSnapshot { state: CircuitState, failures: u64, successes: u64 }` struct
|
||||
- Added `CircuitBreaker::snapshot() -> CircuitSnapshot` method (atomic load)
|
||||
- Test: `snapshot_reflects_state_and_counts`
|
||||
|
||||
### 2. RetryStats (retry.rs)
|
||||
- Added `RetryStats { attempts: u32, retries: u32, last_error: Option<String> }`
|
||||
- Added `retry_with_stats()` returning `(Result, RetryStats)` (parallel to retry())
|
||||
- Tests: 2 new
|
||||
|
||||
### 3. AsyncLifecycleManager (lifecycle.rs) — Round 3 carryover, verified
|
||||
- Composes ShutdownManager + HealthRegistry + health loop
|
||||
- Tests: 3
|
||||
|
||||
### 4. MetricsBridge (metrics_bridge.rs) — Round 3 carryover
|
||||
- `MetricsBridge` emits MetricsCollector → tracing
|
||||
- `MetricsHealthCheck` wraps collector as HealthCheck
|
||||
- Tests: 2
|
||||
|
||||
## Fixes in round 4
|
||||
- `HealthRegistry` wrapped in `Arc` in AsyncLifecycleManager (not Clone)
|
||||
- Removed unused `span`/`Instrument` import in lifecycle.rs
|
||||
- Fixed `op_ref` mutability in service_builder.rs
|
||||
- Fixed `last_error` assertion (None on success) in retry test
|
||||
- Fixed snapshot test assertions (successes not incremented in Closed state)
|
||||
|
||||
## Build Status
|
||||
- cargo build --workspace --all-features: OK (2 pre-existing warnings in crypto/cli)
|
||||
- cargo test --workspace --all-features: ALL PASS
|
||||
- cargo clippy: 0 warnings on round-4 code (pre-existing in crypto/cli only)
|
||||
- Committed + pushed
|
||||
@@ -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.
|
||||
+105
@@ -1,3 +1,108 @@
|
||||
# [1.19.0](https://github.com/asepharyana/mytheclipse/compare/v1.18.0...v1.19.0) (2026-08-29)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* criterion benchmarks proving primitive overhead is negligible ([5df04d7](https://github.com/asepharyana/mytheclipse/commit/5df04d75126d71d0790028c4a215acb571f25616))
|
||||
|
||||
# [1.18.0](https://github.com/asepharyana/mytheclipse/compare/v1.17.0...v1.18.0) (2026-08-29)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* round-15 abstractions — parallel_for_each streaming fan-out ([730245e](https://github.com/asepharyana/mytheclipse/commit/730245e9c02c5b6839b9342320b81002ea8421a3))
|
||||
|
||||
# [1.17.0](https://github.com/asepharyana/mytheclipse/compare/v1.16.0...v1.17.0) (2026-08-29)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* round-14 abstractions — parallel_map bounded fan-out ([bb4998d](https://github.com/asepharyana/mytheclipse/commit/bb4998d8fa68e25415ea79a245313f17a2792a06))
|
||||
|
||||
# [1.16.0](https://github.com/asepharyana/mytheclipse/compare/v1.15.0...v1.16.0) (2026-08-29)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* round-13 abstractions — AggregateError for parallel fan-out ([ff5fbe4](https://github.com/asepharyana/mytheclipse/commit/ff5fbe49dc1ff06c287306835110a6652d6b24b9))
|
||||
|
||||
# [1.15.0](https://github.com/asepharyana/mytheclipse/compare/v1.14.0...v1.15.0) (2026-08-29)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* round-12 abstractions — AutoReconnectPool, Reconnectable ([ddb2c2f](https://github.com/asepharyana/mytheclipse/commit/ddb2c2fd2d43e07b0d26b2901746ffcc3fe8b284))
|
||||
|
||||
# [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)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* round-4 metrics for circuit breaker + retry stats + lifecycle fixes ([717e690](https://github.com/asepharyana/mytheclipse/commit/717e6905cd7a3f7389b455d01054a2c2cc28befd))
|
||||
|
||||
# [1.5.0](https://github.com/asepharyana/mytheclipse/compare/v1.4.1...v1.5.0) (2026-08-29)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* round-3 abstractions — ConfigValidator, AsyncLifecycleManager, MetricsBridge, rate limiter pre-acquire ([1ea3b35](https://github.com/asepharyana/mytheclipse/commit/1ea3b3558143bd07168c3be89653fbeb9c38930a))
|
||||
|
||||
## [1.4.1](https://github.com/asepharyana/mytheclipse/compare/v1.4.0...v1.4.1) (2026-08-29)
|
||||
|
||||
|
||||
|
||||
Generated
+183
-11
@@ -100,6 +100,12 @@ dependencies = [
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anes"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "1.0.0"
|
||||
@@ -1086,6 +1092,12 @@ dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cast"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
|
||||
|
||||
[[package]]
|
||||
name = "cbc"
|
||||
version = "0.1.2"
|
||||
@@ -1130,6 +1142,33 @@ dependencies = [
|
||||
"rand_core 0.10.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ciborium"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e"
|
||||
dependencies = [
|
||||
"ciborium-io",
|
||||
"ciborium-ll",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ciborium-io"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757"
|
||||
|
||||
[[package]]
|
||||
name = "ciborium-ll"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9"
|
||||
dependencies = [
|
||||
"ciborium-io",
|
||||
"half",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cipher"
|
||||
version = "0.4.4"
|
||||
@@ -1317,6 +1356,44 @@ dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "criterion"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f"
|
||||
dependencies = [
|
||||
"anes",
|
||||
"cast",
|
||||
"ciborium",
|
||||
"clap",
|
||||
"criterion-plot",
|
||||
"futures",
|
||||
"is-terminal",
|
||||
"itertools 0.10.5",
|
||||
"num-traits",
|
||||
"once_cell",
|
||||
"oorandom",
|
||||
"plotters",
|
||||
"rayon",
|
||||
"regex",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
"serde_json",
|
||||
"tinytemplate",
|
||||
"tokio",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "criterion-plot"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
|
||||
dependencies = [
|
||||
"cast",
|
||||
"itertools 0.10.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-channel"
|
||||
version = "0.5.16"
|
||||
@@ -1351,6 +1428,12 @@ version = "0.8.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "61803da095bee82a81bb1a452ecc25d3b2f1416d1897eb86430c6159ef717c17"
|
||||
|
||||
[[package]]
|
||||
name = "crunchy"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5"
|
||||
|
||||
[[package]]
|
||||
name = "crypto-bigint"
|
||||
version = "0.5.5"
|
||||
@@ -2116,6 +2199,17 @@ dependencies = [
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "half"
|
||||
version = "2.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"crunchy",
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.15.5"
|
||||
@@ -2162,6 +2256,15 @@ version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "hmac"
|
||||
version = "0.12.1"
|
||||
@@ -2552,12 +2655,32 @@ version = "2.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a756c3fac73139e83f14c2d742155dd2b78d3ee56597b419a0579b7bdd6dd78"
|
||||
|
||||
[[package]]
|
||||
name = "is-terminal"
|
||||
version = "0.4.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46"
|
||||
dependencies = [
|
||||
"hermit-abi 0.5.2",
|
||||
"libc",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "is_terminal_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.10.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.13.0"
|
||||
@@ -2818,9 +2941,10 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mytheclipse"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"criterion",
|
||||
"num_cpus",
|
||||
"rand 0.8.8",
|
||||
"rayon",
|
||||
@@ -2832,7 +2956,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mytheclipse-cache"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"moka",
|
||||
@@ -2845,7 +2969,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mytheclipse-cli"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"tokio",
|
||||
@@ -2854,7 +2978,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mytheclipse-config"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
dependencies = [
|
||||
"dotenvy",
|
||||
"notify",
|
||||
@@ -2869,13 +2993,14 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mytheclipse-crypto"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
dependencies = [
|
||||
"aead",
|
||||
"aes-gcm",
|
||||
"argon2",
|
||||
"base64 0.22.1",
|
||||
"hashbrown 0.15.5",
|
||||
"hkdf",
|
||||
"jsonwebtoken",
|
||||
"pasetors",
|
||||
"password-hash",
|
||||
@@ -2883,13 +3008,14 @@ dependencies = [
|
||||
"rand_core 0.6.4",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2 0.10.9",
|
||||
"tokio",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mytheclipse-event"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
dependencies = [
|
||||
"async-nats",
|
||||
"async-trait",
|
||||
@@ -2905,21 +3031,23 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mytheclipse-http"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"axum",
|
||||
"hyper 1.11.1",
|
||||
"mytheclipse",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tokio",
|
||||
"tower",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mytheclipse-queue"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
dependencies = [
|
||||
"async-nats",
|
||||
"async-trait",
|
||||
@@ -2935,7 +3063,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mytheclipse-storage"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"aws-config",
|
||||
@@ -2951,7 +3079,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mytheclipse-tracing"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
dependencies = [
|
||||
"opentelemetry 0.25.0",
|
||||
"tokio",
|
||||
@@ -3123,6 +3251,12 @@ version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||
|
||||
[[package]]
|
||||
name = "oorandom"
|
||||
version = "11.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
|
||||
|
||||
[[package]]
|
||||
name = "opaque-debug"
|
||||
version = "0.3.1"
|
||||
@@ -3481,6 +3615,34 @@ version = "0.3.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548"
|
||||
|
||||
[[package]]
|
||||
name = "plotters"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
"plotters-backend",
|
||||
"plotters-svg",
|
||||
"wasm-bindgen",
|
||||
"web-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "plotters-backend"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a"
|
||||
|
||||
[[package]]
|
||||
name = "plotters-svg"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670"
|
||||
dependencies = [
|
||||
"plotters-backend",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "polling"
|
||||
version = "2.8.0"
|
||||
@@ -3813,7 +3975,7 @@ dependencies = [
|
||||
"bytes",
|
||||
"combine",
|
||||
"futures-util",
|
||||
"itertools",
|
||||
"itertools 0.13.0",
|
||||
"itoa",
|
||||
"num-bigint",
|
||||
"percent-encoding",
|
||||
@@ -4719,6 +4881,16 @@ dependencies = [
|
||||
"zerovec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinytemplate"
|
||||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec"
|
||||
version = "1.12.0"
|
||||
|
||||
@@ -11,11 +11,11 @@ concern.
|
||||
|
||||
| Crate | Description | Docs |
|
||||
| :--- | :--- | :--- |
|
||||
| [`mytheclipse`](crates/mytheclipse) | Resource-aware execution primitives (async I/O, compute, background queues), resiliency (retry, circuit breaker, timeout), traffic control (rate limiter, backpressure, concurrency limiter), lifecycle (graceful shutdown, cron), and observability (metrics, panic tracking). | [README](crates/mytheclipse/README.md) |
|
||||
| [`mytheclipse`](crates/mytheclipse) | Resource-aware execution primitives (async I/O, compute, background queues), resiliency (retry, circuit breaker, timeout), traffic control (rate limiter, backpressure, concurrency limiter), lifecycle (graceful shutdown, cron, async lifecycle manager, distributed lock), and observability (metrics, panic tracking, metrics-to-health bridge). | [README](crates/mytheclipse/README.md) |
|
||||
| [`mytheclipse-cache`](crates/mytheclipse-cache) | Unified multi-layer (L1/L2) cache abstraction: in-memory or Moka L1, Redis/Valkey L2, cache-aside read-through. | [README](crates/mytheclipse-cache/README.md) |
|
||||
| [`mytheclipse-storage`](crates/mytheclipse-storage) | Unified storage & file system abstraction: one driver interface over local disk, S3/MinIO, and Google Cloud Storage, stream-based. | [README](crates/mytheclipse-storage/README.md) |
|
||||
| [`mytheclipse-event`](crates/mytheclipse-event) | Unified events & message bus abstraction: in-memory pub/sub dispatcher plus RabbitMQ and NATS broker adapters behind one trait. | [README](crates/mytheclipse-event/README.md) |
|
||||
| [`mytheclipse-config`](crates/mytheclipse-config) | Type-safe, dynamic configuration engine: load `.env`/YAML/JSON/TOML into typed structs, with hot-reload. | [README](crates/mytheclipse-config/README.md) |
|
||||
| [`mytheclipse-config`](crates/mytheclipse-config) | Type-safe, dynamic configuration engine: load `.env`/YAML/JSON/TOML into typed structs, with hot-reload and typed validation. | [README](crates/mytheclipse-config/README.md) |
|
||||
| [`mytheclipse-crypto`](crates/mytheclipse-crypto) | Safe hashing (Argon2id), encryption (AES-256-GCM), JWT and PASETO tokens, with key rotation support. | [README](crates/mytheclipse-crypto/README.md) |
|
||||
| [`mytheclipse-queue`](crates/mytheclipse-queue) | Unified job queue abstraction with WorkerPool executor, retry/backoff, and dead-letter support. Backends: in-memory, Redis, NATS, PostgreSQL. | [README](crates/mytheclipse-queue/README.md) |
|
||||
| [`mytheclipse-tracing`](crates/mytheclipse-tracing) | Pre-built tracing subscriber layers with env filtering and optional OTLP/Jaeger/Zipkin export. | [README](crates/mytheclipse-tracing/README.md) |
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "mytheclipse-cache"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.75"
|
||||
license = "MIT OR Apache-2.0"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "mytheclipse-cli"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.75"
|
||||
license = "MIT OR Apache-2.0"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "mytheclipse-config"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.75"
|
||||
license = "MIT OR Apache-2.0"
|
||||
@@ -14,7 +14,7 @@ keywords = ["config", "env", "yaml", "json", "hot-reload"]
|
||||
categories = ["config", "development-tools"]
|
||||
|
||||
[features]
|
||||
default = ["env", "yaml", "toml", "hot-reload"]
|
||||
default = ["env", "yaml", "toml", "hot-reload", "validation"]
|
||||
# Load .env files + environment variables.
|
||||
env = ["dep:dotenvy"]
|
||||
# Parse structured files. JSON support (`.json`) is always available since
|
||||
@@ -23,6 +23,8 @@ yaml = ["dep:serde_yaml"]
|
||||
toml = ["dep:toml"]
|
||||
# Watch config files and hot-reload.
|
||||
hot-reload = ["dep:notify", "dep:tokio"]
|
||||
# Config validation traits and built-in validators.
|
||||
validation = []
|
||||
# JSON Schema generation for config validation and docs.
|
||||
schema = []
|
||||
|
||||
|
||||
@@ -15,6 +15,8 @@ pub enum ConfigError {
|
||||
UnsupportedFormat(String),
|
||||
/// Hot-reload setup failed (e.g. the file watcher could not be installed).
|
||||
Watch(String),
|
||||
/// Config validation failed after loading.
|
||||
Validation(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ConfigError {
|
||||
@@ -25,6 +27,7 @@ impl std::fmt::Display for ConfigError {
|
||||
Self::Deserialize(s) => write!(f, "config deserialize error: {s}"),
|
||||
Self::UnsupportedFormat(s) => write!(f, "unsupported config format: {s}"),
|
||||
Self::Watch(s) => write!(f, "config watch error: {s}"),
|
||||
Self::Validation(s) => write!(f, "config validation error: {s}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,9 +43,19 @@ pub mod dynamic;
|
||||
#[cfg(feature = "schema")]
|
||||
pub mod schema;
|
||||
|
||||
#[cfg(feature = "validation")]
|
||||
pub mod validate;
|
||||
|
||||
pub use error::ConfigError;
|
||||
pub use loader::ConfigLoader;
|
||||
|
||||
#[cfg(feature = "validation")]
|
||||
pub use validate::{
|
||||
collect_failures, validate_non_empty, validate_port, validate_range,
|
||||
validate_url, ConfigValidator, ConfigValidatorExt, ValidationError,
|
||||
ValidationFailure,
|
||||
};
|
||||
|
||||
#[cfg(feature = "hot-reload")]
|
||||
pub use dynamic::DynamicConfig;
|
||||
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
//! Config validation traits and built-in validators (feature `validation`).
|
||||
//!
|
||||
//! [`ConfigValidator`] lets application config types sanity-check themselves
|
||||
//! after deserialization — e.g. ensuring a database URL parses, a port is in
|
||||
//! range, or a required field is non-empty — and collect all failures into a
|
||||
//! single report rather than failing one field at a time.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use crate::ConfigError;
|
||||
|
||||
/// A single validation failure with a human-readable path and message.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ValidationFailure {
|
||||
/// Dotted path to the offending field, e.g. `"database.url"`.
|
||||
pub path: String,
|
||||
/// What was wrong.
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl fmt::Display for ValidationFailure {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}: {}", self.path, self.message)
|
||||
}
|
||||
}
|
||||
|
||||
/// Errors produced by [`ConfigValidator::validate`].
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ValidationError {
|
||||
/// All failures found in a single validation pass.
|
||||
pub failures: Vec<ValidationFailure>,
|
||||
}
|
||||
|
||||
impl fmt::Display for ValidationError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "config validation failed ({} issue(s)):", self.failures.len())?;
|
||||
for failure in &self.failures {
|
||||
write!(f, "\n - {failure}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for ValidationError {}
|
||||
|
||||
impl From<ValidationError> for ConfigError {
|
||||
fn from(err: ValidationError) -> Self {
|
||||
ConfigError::Validation(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for types that can validate themselves after configuration loading.
|
||||
///
|
||||
/// Implementors collect field-level failures rather than returning on the
|
||||
/// first error, so operators see the full problem set in one pass.
|
||||
pub trait ConfigValidator {
|
||||
fn validate(&self) -> Result<(), ValidationError>;
|
||||
}
|
||||
|
||||
/// Convenience blanket for any serializable config type that implements
|
||||
/// [`ConfigValidator`]. Callers typically invoke this on the output of
|
||||
/// [`ConfigLoader::build`](crate::loader::ConfigLoader::build).
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use mytheclipse_config::{ConfigLoader, ConfigValidator, ConfigValidatorExt};
|
||||
/// # use serde::Deserialize;
|
||||
/// # #[derive(Debug, Deserialize)]
|
||||
/// # struct Cfg { port: u16 }
|
||||
/// # impl ConfigValidator for Cfg {
|
||||
/// # fn validate(&self) -> Result<(), mytheclipse_config::ValidationError> { Ok(()) }
|
||||
/// # }
|
||||
/// let cfg: Cfg = ConfigLoader::new().build().unwrap();
|
||||
/// cfg.validate_config().unwrap();
|
||||
/// ```
|
||||
pub trait ConfigValidatorExt: ConfigValidator {
|
||||
/// Validates `self`, returning `Ok(())` on success.
|
||||
fn validate_config(&self) -> Result<(), ConfigError> {
|
||||
self.validate().map_err(ConfigError::from)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ConfigValidator> ConfigValidatorExt for T {}
|
||||
|
||||
/// Validates that a string is a well-formed URL (http/https).
|
||||
pub fn validate_url(path: &str, value: &str) -> Option<ValidationFailure> {
|
||||
if value.is_empty() {
|
||||
return Some(ValidationFailure {
|
||||
path: path.to_string(),
|
||||
message: "url must not be empty".into(),
|
||||
});
|
||||
}
|
||||
// Minimal heuristic: scheme + host. We avoid pulling in a full URL crate
|
||||
// to keep the dependency surface small.
|
||||
let scheme_len = if value.starts_with("http://") { 7 } else if value.starts_with("https://") { 8 } else {
|
||||
return Some(ValidationFailure {
|
||||
path: path.to_string(),
|
||||
message: format!("url must start with http:// or https:// (got {value:?})"),
|
||||
});
|
||||
};
|
||||
let host = &value[scheme_len..];
|
||||
if host.is_empty() {
|
||||
return Some(ValidationFailure {
|
||||
path: path.to_string(),
|
||||
message: format!("url has no host portion (got {value:?})"),
|
||||
});
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Validates that a port number is in the valid range (1–65535).
|
||||
pub fn validate_port(path: &str, port: u16) -> Option<ValidationFailure> {
|
||||
// u16 already ranges 0–65535; exclude 0 (reserved/unspecified).
|
||||
if port == 0 {
|
||||
Some(ValidationFailure {
|
||||
path: path.to_string(),
|
||||
message: "port must be > 0".into(),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates that a string is non-empty.
|
||||
pub fn validate_non_empty(path: &str, value: &str) -> Option<ValidationFailure> {
|
||||
if value.trim().is_empty() {
|
||||
Some(ValidationFailure {
|
||||
path: path.to_string(),
|
||||
message: "value must not be empty".into(),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates that a numeric value falls within `[lo, hi]`.
|
||||
pub fn validate_range<T>(path: &str, value: T, lo: T, hi: T) -> Option<ValidationFailure>
|
||||
where
|
||||
T: PartialOrd + fmt::Display + Copy,
|
||||
{
|
||||
if value < lo || value > hi {
|
||||
Some(ValidationFailure {
|
||||
path: path.to_string(),
|
||||
message: format!("value {value} is out of range [{lo}, {hi}]"),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Collects all failures from an iterator of `Option<ValidationFailure>`.
|
||||
pub fn collect_failures(opts: impl IntoIterator<Item = Option<ValidationFailure>>) -> Result<(), ValidationError> {
|
||||
let failures: Vec<_> = opts.into_iter().flatten().collect();
|
||||
if failures.is_empty() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ValidationError { failures })
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn url_validator_pass_and_fail() {
|
||||
assert!(validate_url("db.url", "https://example.com").is_none());
|
||||
assert!(validate_url("db.url", "").is_some());
|
||||
assert!(validate_url("db.url", "ftp://bad").is_some());
|
||||
assert!(validate_url("db.url", "https://").is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn port_validator_rejects_zero() {
|
||||
assert!(validate_port("port", 0).is_some());
|
||||
assert!(validate_port("port", 1).is_none());
|
||||
assert!(validate_port("port", 65535).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn range_validator_bounds() {
|
||||
assert!(validate_range("x", 5, 1, 10).is_none());
|
||||
assert!(validate_range("x", 10, 1, 10).is_none());
|
||||
assert!(validate_range("x", 0, 1, 10).is_some());
|
||||
assert!(validate_range("x", 11, 1, 10).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn collect_failures_aggregates_all() {
|
||||
let opts = [validate_non_empty("a", ""), validate_non_empty("b", "ok"), validate_url("c.d", "bad://x")];
|
||||
let err = collect_failures(opts).unwrap_err();
|
||||
assert_eq!(err.failures.len(), 2);
|
||||
assert_eq!(err.failures[0].path, "a");
|
||||
assert_eq!(err.failures[1].path, "c.d");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn collect_failures_ok_when_all_pass() {
|
||||
let opts = [validate_url("a", "https://ok.com"), validate_port("b", 8080)];
|
||||
assert!(collect_failures(opts).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blanket_ext_wrappers_validator() {
|
||||
struct Cfg;
|
||||
impl ConfigValidator for Cfg {
|
||||
fn validate(&self) -> Result<(), ValidationError> {
|
||||
Err(ValidationError {
|
||||
failures: vec![ValidationFailure { path: "x".into(), message: "bad".into() }],
|
||||
})
|
||||
}
|
||||
}
|
||||
let c = Cfg;
|
||||
assert!(c.validate_config().is_err());
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "mytheclipse-crypto"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.75"
|
||||
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"]
|
||||
paseto = ["encryption", "dep:serde", "dep:serde_json", "dep:base64", "dep:pasetors"]
|
||||
rate-limit = ["dep:hashbrown", "dep:tokio"]
|
||||
# HKDF-SHA256 key derivation (RFC 5869).
|
||||
derivation = ["dep:hkdf", "dep:sha2"]
|
||||
|
||||
[dependencies]
|
||||
tracing = "0.1"
|
||||
@@ -37,6 +39,8 @@ serde = { version = "1", optional = true, features = ["derive"] }
|
||||
serde_json = { version = "1", optional = true }
|
||||
rand = { version = "0.8", default-features = false, features = ["std", "std_rng"], 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"] }
|
||||
hashbrown = { version = "0.15", 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_registry;
|
||||
|
||||
#[cfg(feature = "password")]
|
||||
pub mod password;
|
||||
@@ -54,6 +55,8 @@ pub mod token;
|
||||
|
||||
#[cfg(feature = "paseto")]
|
||||
pub mod paseto;
|
||||
#[cfg(feature = "derivation")]
|
||||
pub mod hkdf;
|
||||
|
||||
#[cfg(feature = "password")]
|
||||
pub use password::PasswordHasher;
|
||||
@@ -68,6 +71,10 @@ pub use token::{Claims, TokenError, TokenSigner};
|
||||
pub use paseto::{PasetoSigner, PasetoClaims};
|
||||
|
||||
pub use key_ring::KeyRing;
|
||||
pub use key_registry::TypedKeyRegistry;
|
||||
|
||||
#[cfg(feature = "derivation")]
|
||||
pub use hkdf::HkdfKeyDeriver;
|
||||
|
||||
/// Errors returned across mytheclipse-crypto primitives.
|
||||
#[non_exhaustive]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "mytheclipse-event"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.75"
|
||||
license = "MIT OR Apache-2.0"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "mytheclipse-http"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.75"
|
||||
license = "MIT OR Apache-2.0"
|
||||
@@ -21,6 +21,10 @@ client = ["dep:reqwest", "dep:tokio"]
|
||||
server-hyper = ["dep:hyper", "dep:tokio"]
|
||||
# Server backed by axum.
|
||||
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]
|
||||
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 }
|
||||
hyper = { version = "1", features = ["full"], 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_json = "1"
|
||||
mytheclipse = { version = "1.5", path = "../mytheclipse", optional = true, default-features = false, features = ["full"] }
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { version = "1.53", features = ["full"] }
|
||||
|
||||
@@ -9,6 +9,11 @@
|
||||
//! 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")]
|
||||
pub mod client;
|
||||
|
||||
@@ -17,3 +22,9 @@ pub use client::HttpClient;
|
||||
|
||||
#[cfg(feature = "server-axum")]
|
||||
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]
|
||||
name = "mytheclipse-queue"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.75"
|
||||
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),
|
||||
/// A timeout occurred while waiting for an operation.
|
||||
Timeout,
|
||||
/// The operation was rejected because of a rate limit.
|
||||
RateLimit(String),
|
||||
}
|
||||
|
||||
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::Serialization(s) => write!(f, "serialization error: {s}"),
|
||||
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.
|
||||
//! - **NATS JetStream** (`nats`) — durable consumer with ACK/NACK.
|
||||
//! - **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 job;
|
||||
@@ -55,12 +18,19 @@ pub mod traits;
|
||||
pub mod worker;
|
||||
|
||||
#[cfg(feature = "in-memory")]
|
||||
pub use in_memory::InMemoryQueue;
|
||||
|
||||
pub use traits::Queue;
|
||||
pub use job::{Job, JobId};
|
||||
pub use worker::{WorkerPool, WorkerConfig, JobHandler, JobFuture};
|
||||
pub use error::{QueueError, JobError};
|
||||
pub mod batch;
|
||||
#[cfg(feature = "in-memory")]
|
||||
pub mod backpressure_enqueue;
|
||||
#[cfg(feature = "in-memory")]
|
||||
pub mod rate_limited;
|
||||
#[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")]
|
||||
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,51 @@
|
||||
//! 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 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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
//! Race-safety stress tests for the queue crate's rate-limited primitives.
|
||||
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use mytheclipse_queue::in_memory::InMemoryQueue;
|
||||
use mytheclipse_queue::rate_limited::RateLimitedQueue;
|
||||
use mytheclipse_queue::traits::Queue;
|
||||
|
||||
const TASKS: usize = 8;
|
||||
const PER_TASK: usize = 1000;
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
async fn rate_limited_queue_no_item_loss_under_contention() {
|
||||
// Enqueue 8000 items, dequeue them all concurrently: nothing may vanish.
|
||||
let inner = InMemoryQueue::new();
|
||||
let q = Arc::new(RateLimitedQueue::new(inner, 1_000_000.0, 10_000));
|
||||
|
||||
let producer: tokio::task::JoinHandle<()> = tokio::spawn({
|
||||
let q = Arc::clone(&q);
|
||||
async move {
|
||||
for i in 0..(TASKS * PER_TASK) {
|
||||
q.enqueue("stress", format!("item-{i}").into_bytes())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Drain concurrently with the producer.
|
||||
let seen = Arc::new(AtomicUsize::new(0));
|
||||
let mut workers = Vec::new();
|
||||
for _ in 0..TASKS {
|
||||
let q = Arc::clone(&q);
|
||||
let s = Arc::clone(&seen);
|
||||
workers.push(tokio::spawn(async move {
|
||||
loop {
|
||||
match q.dequeue("stress", Duration::from_millis(50)).await.unwrap() {
|
||||
Some(job) => {
|
||||
let _ = String::from_utf8(job.payload).unwrap();
|
||||
s.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
None => {
|
||||
// Empty + producer done => we're finished.
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
producer.await.unwrap();
|
||||
for w in workers {
|
||||
w.await.unwrap();
|
||||
}
|
||||
|
||||
// Slight subtlety: a worker may observe None while producer still has
|
||||
// items in flight (producer finished above, but enqueue is async —
|
||||
// actually producer.await guarantees all enqueues completed). Since the
|
||||
// producer finished, any None means truly drained.
|
||||
assert_eq!(
|
||||
seen.load(Ordering::SeqCst),
|
||||
TASKS * PER_TASK,
|
||||
"items lost under contention"
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "mytheclipse-storage"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.75"
|
||||
license = "MIT OR Apache-2.0"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "mytheclipse-tracing"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.75"
|
||||
license = "MIT OR Apache-2.0"
|
||||
|
||||
@@ -1,3 +1,119 @@
|
||||
# [1.18.0](https://github.com/asepharyana/mytheclipse/compare/v1.17.0...v1.18.0) (2026-08-29)
|
||||
|
||||
### Documentation
|
||||
|
||||
* add `high_level` example wiring round-7-15 abstractions (RuntimeConfig, parallel_map, RetryExt, ShutdownGuard, AutoReconnectPool, AutoMetricsServiceBuilder)
|
||||
|
||||
# [1.17.0](https://github.com/asepharyana/mytheclipse/compare/v1.16.0...v1.17.0) (2026-08-29)
|
||||
|
||||
### Features
|
||||
|
||||
* **parallel_map:** add `parallel_for_each` streaming bounded fan-out (no full input materialization)
|
||||
|
||||
# [1.16.0](https://github.com/asepharyana/mytheclipse/compare/v1.15.0...v1.16.0) (2026-08-29)
|
||||
|
||||
### Features
|
||||
|
||||
* **parallel_map:** add `parallel_map` / `parallel_map_unordered` bounded fan-out with AggregateError
|
||||
|
||||
# [1.15.0](https://github.com/asepharyana/mytheclipse/compare/v1.14.0...v1.15.0) (2026-08-29)
|
||||
|
||||
### Features
|
||||
|
||||
* **aggregate_error:** add `AggregateError` for parallel/fan-out error aggregation
|
||||
|
||||
# [1.14.0](https://github.com/asepharyana/mytheclipse/compare/v1.13.0...v1.14.0) (2026-08-29)
|
||||
|
||||
### Features
|
||||
|
||||
* **pool:** add `AutoReconnectPool` + `Reconnectable` self-healing resource pool
|
||||
|
||||
# [1.13.0](https://github.com/asepharyana/mytheclipse/compare/v1.12.0...v1.13.0) (2026-08-29)
|
||||
|
||||
### Features
|
||||
|
||||
* **runtime_auto:** add `RuntimeConfig` auto thread/core allocation + `ShutdownGuard` RAII
|
||||
|
||||
# [1.12.0](https://github.com/asepharyana/mytheclipse/compare/v1.11.0...v1.12.0) (2026-08-29)
|
||||
|
||||
### Features
|
||||
|
||||
* **auto_metrics:** add `AutoMetricsServiceBuilder` (metrics + [mytheclipse-queue](crates/mytheclipse-queue) `RateLimitedWorkerPool`)
|
||||
|
||||
# [1.11.0](https://github.com/asepharyana/mytheclipse/compare/v1.10.0...v1.11.0) (2026-08-29)
|
||||
|
||||
### Features
|
||||
|
||||
* **retry_ext:** add ergonomic `RetryExt` Future extension — also covers ResilientHttpClient from [mytheclipse-http](crates/mytheclipse-http)
|
||||
|
||||
# [1.10.0](https://github.com/asepharyana/mytheclipse/compare/v1.9.0...v1.10.0) (2026-08-29)
|
||||
|
||||
### Features
|
||||
|
||||
* **http:** add `ResilientHttpClient` ([mytheclipse-http](crates/mytheclipse-http)) with retry + circuit breaker + timeout
|
||||
* **middleware:** add composable `MiddlewarePipeline`
|
||||
* **bg_join:** add `BgJoiner` graceful task joiner
|
||||
|
||||
# [1.9.0](https://github.com/asepharyana/mytheclipse/compare/v1.8.0...v1.9.0) (2026-08-28)
|
||||
|
||||
### Features
|
||||
|
||||
* **bg_join:** add `BgJoiner` graceful task joiner
|
||||
* **middleware:** add composable `MiddlewarePipeline`
|
||||
* **queue:** add `RateLimitedQueue` ([mytheclipse-queue](crates/mytheclipse-queue))
|
||||
|
||||
# [1.8.0](https://github.com/asepharyana/mytheclipse/compare/v1.7.0...v1.8.0) (2026-08-28)
|
||||
|
||||
### Features
|
||||
|
||||
* **pool_health:** add `HealthCheckedPool`
|
||||
* **crypto:** add `HkdfKeyDeriver` ([mytheclipse-crypto](crates/mytheclipse-crypto))
|
||||
* **queue:** add `BackpressureEnforcer` / `enqueue_with_backpressure`
|
||||
|
||||
# [1.7.0](https://github.com/asepharyana/mytheclipse/compare/v1.6.0...v1.7.0) (2026-08-28)
|
||||
|
||||
### Features
|
||||
|
||||
* **batch:** add `BatchProcessor` & `BatchJobHandler`
|
||||
* **health:** add `CircuitBreakerHealthCheck` / `MetricsHealthCheck`
|
||||
* **crypto:** add `TypedKeyRegistry`
|
||||
* **http:** add `MetricsHttpHandler`
|
||||
|
||||
# [1.6.0](https://github.com/asepharyana/mytheclipse/compare/v1.5.0...v1.6.0) (2026-08-28)
|
||||
|
||||
### Features
|
||||
|
||||
* **circuit_breaker:** add snapshot metrics
|
||||
* **retry:** add `RetryStats`
|
||||
* lifecycle fixes
|
||||
|
||||
# [1.5.0](https://github.com/asepharyana/mytheclipse/compare/v1.4.1...v1.5.0) (2026-08-28)
|
||||
|
||||
### Features
|
||||
|
||||
* **validation:** add `ConfigValidator`
|
||||
* **lifecycle:** add `AsyncLifecycleManager`
|
||||
* **metrics:** add `MetricsBridge`
|
||||
* **ratelimit:** rate limiter pre-acquire
|
||||
|
||||
# [1.4.0](https://github.com/asepharyana/mytheclipse/compare/v1.3.5...v1.4.0) (2026-08-28)
|
||||
|
||||
### Features
|
||||
|
||||
* add 4 new crates: [mytheclipse-queue](crates/mytheclipse-queue), [mytheclipse-tracing](crates/mytheclipse-tracing), [mytheclipse-http](crates/mytheclipse-http), [mytheclipse-cli](crates/mytheclipse-cli)
|
||||
|
||||
# [1.3.0](https://github.com/asepharyana/mytheclipse/compare/v1.2.0...v1.3.0) (2026-08-28)
|
||||
|
||||
### Features
|
||||
|
||||
* add [mytheclipse-storage](crates/mytheclipse-storage) crate for unified storage abstraction
|
||||
|
||||
# [1.2.0](https://github.com/asepharyana/mytheclipse/compare/v1.1.0...v1.2.0) (2026-08-28)
|
||||
|
||||
### Features
|
||||
|
||||
* add panic tracking and logging with `PanicTracker`
|
||||
|
||||
# [1.1.0](https://github.com/asepharyana/mytheclipse/compare/v1.0.0...v1.1.0) (2026-08-28)
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "mytheclipse"
|
||||
version = "1.4.1"
|
||||
version = "1.19.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.75"
|
||||
license = "MIT OR Apache-2.0"
|
||||
@@ -25,6 +25,7 @@ thiserror = "2"
|
||||
[dev-dependencies]
|
||||
tokio = { version = "1.53", features = ["full"] }
|
||||
tracing-subscriber = "0.3"
|
||||
criterion = { version = "0.5", features = ["async_tokio"] }
|
||||
|
||||
[features]
|
||||
default = []
|
||||
@@ -34,7 +35,7 @@ bg = ["dep:tokio"]
|
||||
resiliency = ["dep:tokio", "dep:rand"]
|
||||
traffic = ["dep:tokio"]
|
||||
lifecycle = ["dep:tokio"]
|
||||
observability = ["dep:tokio"]
|
||||
observability = ["dep:tokio", "lifecycle"]
|
||||
full = ["io", "compute", "bg", "resiliency", "traffic", "lifecycle", "observability"]
|
||||
|
||||
[[example]]
|
||||
@@ -42,6 +43,12 @@ name = "main"
|
||||
path = "examples/main.rs"
|
||||
required-features = ["full"]
|
||||
|
||||
[[bench]]
|
||||
name = "primitives"
|
||||
path = "benches/primitives.rs"
|
||||
harness = false
|
||||
required-features = ["full"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
@@ -0,0 +1,180 @@
|
||||
//! Criterion benchmarks for the high-level primitives.
|
||||
//!
|
||||
//! Run: `cargo bench -p mytheclipse --bench primitives --all-features`
|
||||
//!
|
||||
//! These measure the *overhead* of the race-safe abstractions — the cost you
|
||||
//! pay for auto thread/core allocation, bounded fan-out, and exactly-once
|
||||
//! cleanup. Numbers (per-op) should be tiny; anything in the µs+ range for a
|
||||
//! single primitive would signal a regression.
|
||||
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
use mytheclipse::aggregate_error::AggregateError;
|
||||
use mytheclipse::parallel_map::{parallel_for_each, parallel_map};
|
||||
use mytheclipse::pool::{Pool, SemaphorePool};
|
||||
use mytheclipse::ratelimit::RateLimiter;
|
||||
use mytheclipse::retry_ext::RetryExt;
|
||||
use mytheclipse::retry::RetryConfig;
|
||||
use mytheclipse::shutdown_guard::ShutdownGuard;
|
||||
|
||||
fn rt() -> Runtime {
|
||||
Runtime::new().expect("tokio runtime")
|
||||
}
|
||||
|
||||
fn bench_parallel_map(c: &mut Criterion) {
|
||||
let rt = rt();
|
||||
c.bench_function("parallel_map/1000x8", |b| {
|
||||
b.to_async(&rt).iter(|| async {
|
||||
let items: Vec<u32> = (0..1000).collect();
|
||||
let out = parallel_map(items, 8, |x| async move { Ok::<_, std::io::Error>(x * 2) })
|
||||
.await
|
||||
.unwrap();
|
||||
black_box(out);
|
||||
});
|
||||
});
|
||||
|
||||
// Sequential baseline for comparison.
|
||||
c.bench_function("sequential/map_1000", |b| {
|
||||
b.iter(|| {
|
||||
let items: Vec<u32> = (0..1000).collect();
|
||||
let out: Vec<u32> = items.into_iter().map(|x| x * 2).collect();
|
||||
black_box(out);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn bench_parallel_for_each(c: &mut Criterion) {
|
||||
let rt = rt();
|
||||
c.bench_function("parallel_for_each/1000x8", |b| {
|
||||
b.to_async(&rt).iter(|| async {
|
||||
parallel_for_each(0u32..1000, 8, |_| async move { Ok::<_, std::io::Error>(()) })
|
||||
.await
|
||||
.unwrap();
|
||||
black_box(());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn bench_retry_ext(c: &mut Criterion) {
|
||||
let rt = rt();
|
||||
c.bench_function("retry_ext/success_first", |b| {
|
||||
b.to_async(&rt).iter(|| async {
|
||||
let op = || async { Ok::<u32, std::io::Error>(42) };
|
||||
let out = async { Ok::<u32, std::io::Error>(42) }
|
||||
.retry(
|
||||
RetryConfig {
|
||||
max_attempts: 3,
|
||||
base_delay: Duration::from_millis(1),
|
||||
..RetryConfig::default()
|
||||
},
|
||||
|_: &std::io::Error| true,
|
||||
op,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
black_box(out);
|
||||
});
|
||||
});
|
||||
|
||||
c.bench_function("retry_ext/retries_twice", |b| {
|
||||
b.to_async(&rt).iter(|| async {
|
||||
let attempts = Arc::new(AtomicU32::new(0));
|
||||
let a = Arc::clone(&attempts);
|
||||
let op = move || {
|
||||
let a = Arc::clone(&a);
|
||||
async move {
|
||||
if a.fetch_add(1, Ordering::SeqCst) < 2 {
|
||||
Err::<u32, std::io::Error>(std::io::Error::other("x"))
|
||||
} else {
|
||||
Ok(42)
|
||||
}
|
||||
}
|
||||
};
|
||||
let out = async { Err::<u32, std::io::Error>(std::io::Error::other("first")) }
|
||||
.retry(
|
||||
RetryConfig {
|
||||
max_attempts: 3,
|
||||
base_delay: Duration::from_millis(1),
|
||||
..RetryConfig::default()
|
||||
},
|
||||
|_: &std::io::Error| true,
|
||||
op,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
black_box(out);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn bench_rate_limiter(c: &mut Criterion) {
|
||||
c.bench_function("rate_limiter/try_acquire", |b| {
|
||||
let rl = RateLimiter::new(1_000_000.0, 10_000_000);
|
||||
b.iter(|| {
|
||||
let _ = black_box(rl.try_acquire());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn bench_semaphore_pool(c: &mut Criterion) {
|
||||
let rt = rt();
|
||||
c.bench_function("semaphore_pool/acquire_release", |b| {
|
||||
b.to_async(&rt).iter(|| async {
|
||||
let pool = SemaphorePool::new(vec![1u32, 2, 3, 4, 5]);
|
||||
let item = pool.acquire().await.unwrap();
|
||||
black_box(item.resource);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn bench_aggregate_error(c: &mut Criterion) {
|
||||
c.bench_function("aggregate_error/all_ok_1000", |b| {
|
||||
b.iter(|| {
|
||||
let results: Vec<Result<u32, std::io::Error>> = (0..1000).map(Ok).collect();
|
||||
let out = AggregateError::from_results(black_box(results)).unwrap();
|
||||
black_box(out);
|
||||
});
|
||||
});
|
||||
|
||||
c.bench_function("aggregate_error/500err_1000", |b| {
|
||||
b.iter(|| {
|
||||
let results: Vec<Result<u32, std::io::Error>> = (0..1000)
|
||||
.map(|i| {
|
||||
if i % 2 == 0 {
|
||||
Err(std::io::Error::other("x"))
|
||||
} else {
|
||||
Ok(i)
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let out = AggregateError::from_results(black_box(results));
|
||||
black_box(out);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn bench_shutdown_guard(c: &mut Criterion) {
|
||||
c.bench_function("shutdown_guard/new_drop", |b| {
|
||||
b.iter(|| {
|
||||
let g = ShutdownGuard::new(|| {});
|
||||
black_box(&g);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group!(
|
||||
benches,
|
||||
bench_parallel_map,
|
||||
bench_parallel_for_each,
|
||||
bench_retry_ext,
|
||||
bench_rate_limiter,
|
||||
bench_semaphore_pool,
|
||||
bench_aggregate_error,
|
||||
bench_shutdown_guard,
|
||||
);
|
||||
criterion_main!(benches);
|
||||
@@ -0,0 +1,134 @@
|
||||
//! End-to-end demo of the high-level (round 7-15) abstractions in `mytheclipse`.
|
||||
//!
|
||||
//! Run with: `cargo run -p mytheclipse --features full --example high_level`
|
||||
//!
|
||||
//! Demonstrates, wired together in one realistic flow:
|
||||
//! - `RuntimeConfig::auto()` — auto thread/core sizing from the host CPU
|
||||
//! - `parallel_map` — bounded fan-out with AggregateError
|
||||
//! - `RetryExt` — ergonomic `.retry()` on a Future
|
||||
//! - `ShutdownGuard` — RAII cleanup guaranteed even on panic
|
||||
//! - `AutoReconnectPool` — self-healing resource pool
|
||||
//! - `AutoMetricsServiceBuilder` — service calls that auto-record metrics
|
||||
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use mytheclipse::{
|
||||
aggregate_error::AggregateError,
|
||||
auto_metrics_service::AutoMetricsServiceBuilder,
|
||||
parallel_map::parallel_map,
|
||||
pool::{AutoReconnectPool, Pool, Reconnectable, SemaphorePool},
|
||||
retry_ext::RetryExt,
|
||||
runtime_auto::RuntimeConfig,
|
||||
shutdown_guard::ShutdownGuard,
|
||||
service_builder::ServiceConfig,
|
||||
};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
println!("== mytheclipse high-level abstractions demo ==");
|
||||
|
||||
// 1. Auto thread/core allocation from host CPU topology.
|
||||
let cfg = RuntimeConfig::auto();
|
||||
println!(
|
||||
"1. RuntimeConfig::auto() -> worker={}, blocking={}, compute={}, io={}",
|
||||
cfg.worker_threads, cfg.max_blocking_threads, cfg.compute_threads, cfg.io_threads
|
||||
);
|
||||
|
||||
// 2. Bounded parallel fan-out with automatic error aggregation.
|
||||
let results: Result<Vec<i32>, AggregateError> =
|
||||
parallel_map(vec![1, 2, 3, 4, 5], 2, |x| async move {
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
Ok::<_, std::io::Error>(x * 10)
|
||||
})
|
||||
.await;
|
||||
println!("2. parallel_map -> {:?}", results.unwrap());
|
||||
|
||||
// 3. Ergonomic retry on a Future.
|
||||
let attempts = Arc::new(AtomicU32::new(0));
|
||||
let a = Arc::clone(&attempts);
|
||||
let cfg = mytheclipse::retry::RetryConfig {
|
||||
max_attempts: 4,
|
||||
base_delay: Duration::from_millis(1),
|
||||
..Default::default()
|
||||
};
|
||||
let fut = async { Err::<u32, String>("boom".into()) };
|
||||
let op = move || {
|
||||
let a = Arc::clone(&a);
|
||||
async move {
|
||||
let n = a.fetch_add(1, Ordering::SeqCst);
|
||||
if n < 3 {
|
||||
Err::<u32, String>("transient".into())
|
||||
} else {
|
||||
Ok(42u32)
|
||||
}
|
||||
}
|
||||
};
|
||||
let value = fut.retry(cfg, |_: &String| true, op).await.unwrap();
|
||||
println!("3. RetryExt with {} attempts -> {value}", attempts.load(Ordering::SeqCst));
|
||||
|
||||
// 4. RAII ShutdownGuard — callback runs exactly once on drop, panic-safe.
|
||||
let fired = Arc::new(AtomicU32::new(0));
|
||||
let f = Arc::clone(&fired);
|
||||
{
|
||||
let _guard = ShutdownGuard::new(move || {
|
||||
f.fetch_add(1, Ordering::SeqCst);
|
||||
});
|
||||
} // guard dropped -> callback fires
|
||||
println!(
|
||||
"4. ShutdownGuard fired {}x (exactly-once, even on unwind)",
|
||||
fired.load(Ordering::SeqCst)
|
||||
);
|
||||
|
||||
// 5. AutoReconnectPool — transparently replaces dead resources.
|
||||
let auto = AutoReconnectPool::new(SemaphorePool::new(vec![1u32, 2u32]), Probe { dead: 1 });
|
||||
let first = auto.acquire().await.unwrap().resource;
|
||||
// Never the dead value (1) once the probe rejects it.
|
||||
println!("5. AutoReconnectPool first acquire -> {first}");
|
||||
|
||||
// 6. AutoMetricsServiceBuilder — auto records latency + outcome counters.
|
||||
let mut svc_cfg = ServiceConfig::default();
|
||||
svc_cfg.max_attempts = 2;
|
||||
let svc = AutoMetricsServiceBuilder::new("demo_op", svc_cfg);
|
||||
let n = Arc::new(AtomicU32::new(0));
|
||||
let n2 = Arc::clone(&n);
|
||||
let _: Result<u32, mytheclipse::service_builder::RunError<()>> = svc.run(|| {
|
||||
let n2 = Arc::clone(&n2);
|
||||
Box::pin(async move {
|
||||
tokio::time::sleep(Duration::from_millis(5)).await;
|
||||
let v = n2.fetch_add(1, Ordering::SeqCst);
|
||||
if v < 1 {
|
||||
Err(())
|
||||
} else {
|
||||
Ok(7u32)
|
||||
}
|
||||
})
|
||||
}).await;
|
||||
let snap = svc.collector().snapshot();
|
||||
println!(
|
||||
"6. AutoMetrics -> {} counters, {} histograms",
|
||||
snap.counters.len(),
|
||||
snap.histograms.len()
|
||||
);
|
||||
|
||||
println!("== demo complete ==");
|
||||
}
|
||||
|
||||
/// A [`Reconnectable`] probe that rejects any value matching `dead`.
|
||||
struct Probe {
|
||||
dead: u32,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Reconnectable for Probe {
|
||||
type Item = u32;
|
||||
|
||||
fn is_healthy(&self, item: &Self::Item) -> bool {
|
||||
*item != self.dead
|
||||
}
|
||||
|
||||
async fn reconnect(&self) -> Result<Self::Item, Box<dyn std::error::Error + Send + Sync>> {
|
||||
Ok(999)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
//! Error aggregation for parallel/fan-out work (feature `resiliency`).
|
||||
//!
|
||||
//! [`AggregateError`] collects multiple `E: std::error::Error` values produced
|
||||
//! by concurrently executed tasks into one error, so a caller awaiting `N`
|
||||
//! tasks via `join_all` can surface *every* failure at once instead of
|
||||
//! stopping at the first. This is the natural failure type for
|
||||
//! `futures::future::join_all(vec![...])` transactions, batch operations, and
|
||||
//! fan-out resilience.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
/// An error that groups one or more underlying errors.
|
||||
///
|
||||
/// ```
|
||||
/// use mytheclipse::aggregate_error::AggregateError;
|
||||
///
|
||||
/// // Collect errors from N parallel results — all of them, not just the first:
|
||||
/// let results = vec![
|
||||
/// Ok::<_, std::io::Error>(1),
|
||||
/// Err(std::io::Error::other("boom")),
|
||||
/// Ok::<_, std::io::Error>(3),
|
||||
/// Err(std::io::Error::other("bam")),
|
||||
/// ];
|
||||
/// let out = AggregateError::from_results(results);
|
||||
/// let err = out.unwrap_err();
|
||||
/// assert_eq!(err.len(), 2); // both errors collected
|
||||
///
|
||||
/// // Build one incrementally too:
|
||||
/// let mut agg = AggregateError::with_context("fan-out");
|
||||
/// agg.push(std::io::Error::other("first"));
|
||||
/// agg.push(std::io::Error::other("second"));
|
||||
/// assert_eq!(agg.len(), 2);
|
||||
/// ```
|
||||
#[derive(Debug)]
|
||||
pub struct AggregateError {
|
||||
errors: Vec<Box<dyn std::error::Error + Send + Sync>>,
|
||||
/// Optional label describing the operation that failed.
|
||||
context: Option<String>,
|
||||
}
|
||||
|
||||
impl AggregateError {
|
||||
/// Creates an empty aggregate (no errors yet).
|
||||
pub fn empty() -> Self {
|
||||
Self {
|
||||
errors: Vec::new(),
|
||||
context: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a labeled aggregate with an operation context.
|
||||
pub fn with_context(context: impl Into<String>) -> Self {
|
||||
Self {
|
||||
errors: Vec::new(),
|
||||
context: Some(context.into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds an error to the aggregate.
|
||||
pub fn push<E: Into<Box<dyn std::error::Error + Send + Sync>>>(&mut self, error: E) {
|
||||
self.errors.push(error.into());
|
||||
}
|
||||
|
||||
/// Returns `true` if the aggregate holds no errors.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.errors.is_empty()
|
||||
}
|
||||
|
||||
/// Number of collected errors.
|
||||
pub fn len(&self) -> usize {
|
||||
self.errors.len()
|
||||
}
|
||||
|
||||
/// Iterator over the collected errors.
|
||||
pub fn iter(&self) -> impl Iterator<Item = &(dyn std::error::Error + Send + Sync)> {
|
||||
self.errors.iter().map(|b| b.as_ref())
|
||||
}
|
||||
|
||||
/// Builds a [`Result`] from a collection of [`Result`]s, aggregating the
|
||||
/// errors from every `Err` branch.
|
||||
///
|
||||
/// If all inputs are `Ok`, the `V` values are collected and returned.
|
||||
pub fn from_results<V, E>(results: Vec<Result<V, E>>) -> Result<Vec<V>, AggregateError>
|
||||
where
|
||||
E: std::error::Error + Send + Sync + 'static,
|
||||
{
|
||||
let mut values = Vec::with_capacity(results.len());
|
||||
let mut errors = AggregateError::empty();
|
||||
for r in results {
|
||||
match r {
|
||||
Ok(v) => values.push(v),
|
||||
Err(e) => errors.push(Box::new(e)),
|
||||
}
|
||||
}
|
||||
if errors.is_empty() {
|
||||
Ok(values)
|
||||
} else {
|
||||
Err(errors)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for AggregateError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
if let Some(ctx) = &self.context {
|
||||
write!(f, "{ctx}: {} error(s)", self.errors.len())?;
|
||||
} else {
|
||||
write!(f, "{} error(s)", self.errors.len())?;
|
||||
}
|
||||
if !self.errors.is_empty() {
|
||||
write!(f, " — first: {}", self.errors[0])?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for AggregateError {}
|
||||
|
||||
impl From<Vec<Box<dyn std::error::Error + Send + Sync>>> for AggregateError {
|
||||
fn from(errors: Vec<Box<dyn std::error::Error + Send + Sync>>) -> Self {
|
||||
Self { errors, context: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl Extend<Box<dyn std::error::Error + Send + Sync>> for AggregateError {
|
||||
fn extend<T: IntoIterator<Item = Box<dyn std::error::Error + Send + Sync>>>(&mut self, iter: T) {
|
||||
self.errors.extend(iter);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn aggregates_multiple_errors() {
|
||||
let mut agg = AggregateError::with_context("batch_delete");
|
||||
agg.push(std::io::Error::new(std::io::ErrorKind::Other, "row 1"));
|
||||
agg.push(std::io::Error::new(std::io::ErrorKind::Other, "row 2"));
|
||||
assert_eq!(agg.len(), 2);
|
||||
assert!(!agg.is_empty());
|
||||
let s = agg.to_string();
|
||||
assert!(s.contains("batch_delete"));
|
||||
assert!(s.contains("2 error(s)"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extracts_errors_from_results() {
|
||||
let results: Vec<Result<u32, std::io::Error>> = vec![
|
||||
Ok(1),
|
||||
Err(std::io::Error::new(std::io::ErrorKind::Other, "a")),
|
||||
Ok(2),
|
||||
Err(std::io::Error::new(std::io::ErrorKind::Other, "b")),
|
||||
];
|
||||
let out = AggregateError::from_results(results);
|
||||
assert!(out.is_err());
|
||||
let err = out.unwrap_err();
|
||||
assert_eq!(err.len(), 2);
|
||||
assert_eq!(err.iter().count(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn collects_values_when_all_ok() {
|
||||
let results: Vec<Result<u32, std::io::Error>> =
|
||||
vec![Ok(1), Ok(2), Ok(3)];
|
||||
let out = AggregateError::from_results(results).unwrap();
|
||||
assert_eq!(out, vec![1, 2, 3]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
//! 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.
|
||||
///
|
||||
/// ```
|
||||
/// use std::time::Duration;
|
||||
/// use mytheclipse::auto_metrics_service::AutoMetricsServiceBuilder;
|
||||
/// use mytheclipse::service_builder::ServiceConfig;
|
||||
///
|
||||
/// let cfg = ServiceConfig {
|
||||
/// max_attempts: 2,
|
||||
/// timeout: Duration::from_millis(1),
|
||||
/// ..ServiceConfig::default()
|
||||
/// };
|
||||
/// let svc = AutoMetricsServiceBuilder::new("checkout", cfg);
|
||||
/// // Every `.run()` call now auto-records outcome + latency on the shared
|
||||
/// // MetricsCollector (retrievable via `collector()`).
|
||||
/// let _ = svc.collector();
|
||||
/// ```
|
||||
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);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user