1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! Builder for composing service layers.
use super::concurrency_limit::ConcurrencyLimitLayer;
use super::load_shed::LoadShedLayer;
use super::rate_limit::RateLimitLayer;
use super::retry::RetryLayer;
use super::timeout::TimeoutLayer;
use super::{Identity, Layer, Stack};
use std::sync::Arc;
use std::time::Duration;
/// Builder for stacking layers around a service.
#[derive(Debug, Clone)]
pub struct ServiceBuilder<L> {
layer: L,
}
impl ServiceBuilder<Identity> {
/// Creates a new builder with the identity layer.
#[must_use]
pub fn new() -> Self {
Self { layer: Identity }
}
}
impl Default for ServiceBuilder<Identity> {
fn default() -> Self {
Self::new()
}
}
impl<L> ServiceBuilder<L> {
/// Adds a new layer to the builder.
#[must_use]
pub fn layer<T>(self, layer: T) -> ServiceBuilder<Stack<L, T>> {
ServiceBuilder {
layer: Stack::new(self.layer, layer),
}
}
/// Wraps the given service with the configured layers.
#[must_use]
pub fn service<S>(self, service: S) -> L::Service
where
L: Layer<S>,
{
self.layer.layer(service)
}
/// Returns a reference to the composed layer stack.
#[must_use]
pub fn layer_ref(&self) -> &L {
&self.layer
}
// =========================================================================
// Middleware convenience methods
// =========================================================================
/// Adds a timeout layer with the given duration.
///
/// Requests that take longer than `timeout` will fail with a timeout error.
///
/// # Example
///
/// ```ignore
/// use asupersync::service::ServiceBuilder;
/// use std::time::Duration;
///
/// let svc = ServiceBuilder::new()
/// .timeout(Duration::from_secs(30))
/// .service(my_service);
/// ```
#[must_use]
pub fn timeout(self, timeout: Duration) -> ServiceBuilder<Stack<L, TimeoutLayer>> {
self.layer(TimeoutLayer::new(timeout))
}
/// Adds a load shedding layer.
///
/// When the inner service is not ready (backpressure), requests are
/// immediately rejected instead of being queued.
///
/// # Example
///
/// ```ignore
/// use asupersync::service::ServiceBuilder;
///
/// let svc = ServiceBuilder::new()
/// .load_shed()
/// .service(my_service);
/// ```
#[must_use]
pub fn load_shed(self) -> ServiceBuilder<Stack<L, LoadShedLayer>> {
self.layer(LoadShedLayer::new())
}
/// Adds a concurrency limit layer.
///
/// Limits the number of concurrent in-flight requests.
///
/// # Example
///
/// ```ignore
/// use asupersync::service::ServiceBuilder;
///
/// let svc = ServiceBuilder::new()
/// .concurrency_limit(10) // Max 10 concurrent requests
/// .service(my_service);
/// ```
#[must_use]
pub fn concurrency_limit(self, max: usize) -> ServiceBuilder<Stack<L, ConcurrencyLimitLayer>> {
self.layer(ConcurrencyLimitLayer::new(max))
}
/// Adds a concurrency limit layer with a shared semaphore.
///
/// This is useful when you want multiple services to share the same
/// concurrency limit.
#[must_use]
pub fn concurrency_limit_with_semaphore(
self,
semaphore: Arc<crate::sync::Semaphore>,
) -> ServiceBuilder<Stack<L, ConcurrencyLimitLayer>> {
self.layer(ConcurrencyLimitLayer::with_semaphore(semaphore))
}
/// Adds a rate limiting layer.
///
/// Limits requests to `rate` per `period` using a token bucket algorithm.
///
/// # Example
///
/// ```ignore
/// use asupersync::service::ServiceBuilder;
/// use std::time::Duration;
///
/// let svc = ServiceBuilder::new()
/// .rate_limit(100, Duration::from_secs(1)) // 100 req/sec
/// .service(my_service);
/// ```
#[must_use]
pub fn rate_limit(
self,
rate: u64,
period: Duration,
) -> ServiceBuilder<Stack<L, RateLimitLayer>> {
self.layer(RateLimitLayer::new(rate, period))
}
/// Adds a retry layer with the given policy.
///
/// Failed requests will be retried according to the policy.
///
/// # Example
///
/// ```ignore
/// use asupersync::service::{ServiceBuilder, LimitedRetry};
///
/// let svc = ServiceBuilder::new()
/// .retry(LimitedRetry::new(3)) // Retry up to 3 times
/// .service(my_service);
/// ```
#[must_use]
pub fn retry<P>(self, policy: P) -> ServiceBuilder<Stack<L, RetryLayer<P>>>
where
P: Clone,
{
self.layer(RetryLayer::new(policy))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::service::{Identity, Service, Stack};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
/// Trivial service for testing layer composition.
#[derive(Debug, Clone)]
struct Echo;
impl Service<String> for Echo {
type Response = String;
type Error = std::convert::Infallible;
type Future = Pin<Box<dyn Future<Output = Result<String, Self::Error>> + Send>>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: String) -> Self::Future {
Box::pin(async move { Ok(req) })
}
}
#[test]
fn test_new_creates_identity_builder() {
let builder = ServiceBuilder::new();
// layer_ref should return Identity
let _: &Identity = builder.layer_ref();
}
#[test]
fn test_default_same_as_new() {
let _builder: ServiceBuilder<Identity> = ServiceBuilder::default();
}
#[test]
fn test_service_with_identity_returns_inner() {
let mut svc = ServiceBuilder::new().service(Echo);
let fut = svc.call("hello".to_string());
// Just verify it compiles and produces the right type
drop(fut);
}
#[test]
fn test_layer_adds_stack() {
let builder = ServiceBuilder::new().layer(Identity);
let _: &Stack<Identity, Identity> = builder.layer_ref();
}
#[test]
fn test_timeout_convenience() {
let builder = ServiceBuilder::new().timeout(Duration::from_secs(5));
let _ = builder.layer_ref();
}
#[test]
fn test_load_shed_convenience() {
let builder = ServiceBuilder::new().load_shed();
let _ = builder.layer_ref();
}
#[test]
fn test_concurrency_limit_convenience() {
let builder = ServiceBuilder::new().concurrency_limit(10);
let _ = builder.layer_ref();
}
#[test]
fn test_concurrency_limit_with_semaphore() {
let sem = Arc::new(crate::sync::Semaphore::new(5));
let builder = ServiceBuilder::new().concurrency_limit_with_semaphore(sem);
let _ = builder.layer_ref();
}
#[test]
fn test_rate_limit_convenience() {
let builder = ServiceBuilder::new().rate_limit(100, Duration::from_secs(1));
let _ = builder.layer_ref();
}
#[test]
fn test_retry_convenience() {
use crate::service::retry::LimitedRetry;
let builder = ServiceBuilder::new().retry(LimitedRetry::<String>::new(3));
let _ = builder.layer_ref();
}
#[test]
fn test_chaining_multiple_layers() {
let builder = ServiceBuilder::new()
.timeout(Duration::from_secs(30))
.concurrency_limit(50)
.load_shed()
.rate_limit(1000, Duration::from_secs(1));
let _ = builder.layer_ref();
}
#[test]
fn test_builder_is_clone() {
fn assert_clone<T: Clone>(_value: &T) {}
let builder = ServiceBuilder::new().timeout(Duration::from_secs(1));
assert_clone(&builder);
let clone = builder.clone();
let _ = builder.layer_ref();
let _ = clone.layer_ref();
}
#[test]
fn test_builder_is_debug() {
let builder = ServiceBuilder::new();
let debug = format!("{builder:?}");
assert!(debug.contains("ServiceBuilder"));
}
#[test]
fn test_concurrency_limit_zero() {
// Zero concurrency limit should still compile
let builder = ServiceBuilder::new().concurrency_limit(0);
let _ = builder.layer_ref();
}
#[test]
fn test_rate_limit_zero_rate() {
let builder = ServiceBuilder::new().rate_limit(0, Duration::from_secs(1));
let _ = builder.layer_ref();
}
#[test]
fn test_timeout_zero_duration() {
let builder = ServiceBuilder::new().timeout(Duration::ZERO);
let _ = builder.layer_ref();
}
#[test]
fn test_retry_with_no_retry_policy() {
use crate::service::retry::NoRetry;
let builder = ServiceBuilder::new().retry(NoRetry);
let _ = builder.layer_ref();
}
}