strike48-connector 0.3.9

Rust SDK for the Strike48 Connector Framework
Documentation
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
//! Request-Response Connector Behavior
//!
//! Provides traits and types for implementing synchronous RPC-style operations
//! with concurrency control.
//!
//! # Examples
//!
//! ```rust,ignore
//! use strike48_connector::behaviors::{
//!     RequestResponseConnector, RequestResponseConfig, RequestContext, Response,
//! };
//!
//! struct DatabaseConnector {
//!     config: RequestResponseConfig,
//!     pool: sqlx::PgPool,
//! }
//!
//! #[async_trait]
//! impl RequestResponseConnector for DatabaseConnector {
//!     type Request = SqlQuery;
//!     type Response = QueryResult;
//!     type Error = sqlx::Error;
//!     
//!     async fn handle(&self, ctx: RequestContext<Self::Request>) -> Result<Self::Response, Self::Error> {
//!         let result = sqlx::query(&ctx.request.sql)
//!             .fetch_all(&self.pool)
//!             .await?;
//!         Ok(QueryResult { rows: result })
//!     }
//! }
//! ```

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};
use tokio::sync::Semaphore;

/// Configuration for RequestResponse connectors
#[derive(Debug, Clone)]
pub struct RequestResponseConfig {
    /// Maximum concurrent requests
    pub max_concurrent: usize,
    /// Default timeout for operations
    pub timeout: Duration,
    /// Whether to track metrics
    pub track_metrics: bool,
    /// Custom metadata
    pub metadata: HashMap<String, String>,
}

impl Default for RequestResponseConfig {
    fn default() -> Self {
        Self {
            max_concurrent: 1000,
            timeout: Duration::from_secs(5),
            track_metrics: true,
            metadata: HashMap::new(),
        }
    }
}

impl RequestResponseConfig {
    /// Create a new config with default values
    pub fn new() -> Self {
        Self::default()
    }

    /// Set maximum concurrent requests
    pub fn with_max_concurrent(mut self, max: usize) -> Self {
        self.max_concurrent = max;
        self
    }

    /// Set timeout
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Disable metrics tracking
    pub fn without_metrics(mut self) -> Self {
        self.track_metrics = false;
        self
    }

    /// Add custom metadata
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Build metadata map for connector registration
    pub fn to_metadata(&self) -> HashMap<String, String> {
        let mut meta = self.metadata.clone();
        meta.insert(
            "timeout_ms".to_string(),
            self.timeout.as_millis().to_string(),
        );
        meta.insert(
            "max_concurrent".to_string(),
            self.max_concurrent.to_string(),
        );
        meta
    }
}

/// Context provided to request handlers
#[derive(Debug, Clone)]
pub struct RequestContext<T> {
    /// The request payload
    pub request: T,
    /// Unique request ID
    pub request_id: String,
    /// Request headers/metadata
    pub headers: HashMap<String, String>,
    /// When the request was received
    pub received_at: Instant,
    /// Timeout for this request
    pub timeout: Duration,
}

impl<T> RequestContext<T> {
    /// Create a new request context
    pub fn new(request: T) -> Self {
        Self {
            request,
            request_id: uuid::Uuid::new_v4().to_string(),
            headers: HashMap::new(),
            received_at: Instant::now(),
            timeout: Duration::from_secs(5),
        }
    }

    /// Set the request ID
    pub fn with_id(mut self, id: impl Into<String>) -> Self {
        self.request_id = id.into();
        self
    }

    /// Add a header
    pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.insert(key.into(), value.into());
        self
    }

    /// Set timeout
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Get remaining time before timeout
    pub fn remaining_time(&self) -> Duration {
        let elapsed = self.received_at.elapsed();
        self.timeout.saturating_sub(elapsed)
    }

    /// Check if the request has timed out
    pub fn is_timed_out(&self) -> bool {
        self.received_at.elapsed() >= self.timeout
    }

    /// Map the request to a different type
    pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> RequestContext<U> {
        RequestContext {
            request: f(self.request),
            request_id: self.request_id,
            headers: self.headers,
            received_at: self.received_at,
            timeout: self.timeout,
        }
    }
}

/// Response wrapper with metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Response<T> {
    /// The response payload
    pub data: T,
    /// Response metadata
    pub metadata: HashMap<String, String>,
    /// Processing time in milliseconds
    pub processing_time_ms: u64,
}

impl<T> Response<T> {
    /// Create a new response
    pub fn new(data: T) -> Self {
        Self {
            data,
            metadata: HashMap::new(),
            processing_time_ms: 0,
        }
    }

    /// Add metadata
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Set processing time
    pub fn with_processing_time(mut self, duration: Duration) -> Self {
        self.processing_time_ms = duration.as_millis() as u64;
        self
    }

    /// Map the response data
    pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Response<U> {
        Response {
            data: f(self.data),
            metadata: self.metadata,
            processing_time_ms: self.processing_time_ms,
        }
    }
}

/// Metrics for request/response operations
#[derive(Debug, Default)]
pub struct RequestMetrics {
    /// Total requests processed
    pub total_requests: AtomicU64,
    /// Successful requests
    pub successful_requests: AtomicU64,
    /// Failed requests
    pub failed_requests: AtomicU64,
    /// Total processing time in microseconds
    pub total_processing_time_us: AtomicU64,
    /// Currently in-flight requests
    pub in_flight: AtomicU64,
}

impl RequestMetrics {
    /// Create new metrics
    pub fn new() -> Self {
        Self::default()
    }

    /// Record a request starting
    pub fn start_request(&self) {
        self.total_requests.fetch_add(1, Ordering::Relaxed);
        self.in_flight.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a successful request completion
    pub fn complete_success(&self, duration: Duration) {
        self.successful_requests.fetch_add(1, Ordering::Relaxed);
        self.total_processing_time_us
            .fetch_add(duration.as_micros() as u64, Ordering::Relaxed);
        self.in_flight.fetch_sub(1, Ordering::Relaxed);
    }

    /// Record a failed request
    pub fn complete_failure(&self, duration: Duration) {
        self.failed_requests.fetch_add(1, Ordering::Relaxed);
        self.total_processing_time_us
            .fetch_add(duration.as_micros() as u64, Ordering::Relaxed);
        self.in_flight.fetch_sub(1, Ordering::Relaxed);
    }

    /// Get average processing time in milliseconds
    pub fn avg_processing_time_ms(&self) -> f64 {
        let total = self.total_requests.load(Ordering::Relaxed);
        if total == 0 {
            return 0.0;
        }
        let total_us = self.total_processing_time_us.load(Ordering::Relaxed);
        (total_us as f64 / total as f64) / 1000.0
    }

    /// Get success rate as percentage
    pub fn success_rate(&self) -> f64 {
        let total = self.total_requests.load(Ordering::Relaxed);
        if total == 0 {
            return 100.0;
        }
        let successful = self.successful_requests.load(Ordering::Relaxed);
        (successful as f64 / total as f64) * 100.0
    }

    /// Export metrics as a map
    pub fn to_map(&self) -> HashMap<String, String> {
        let mut map = HashMap::new();
        map.insert(
            "total_requests".to_string(),
            self.total_requests.load(Ordering::Relaxed).to_string(),
        );
        map.insert(
            "successful_requests".to_string(),
            self.successful_requests.load(Ordering::Relaxed).to_string(),
        );
        map.insert(
            "failed_requests".to_string(),
            self.failed_requests.load(Ordering::Relaxed).to_string(),
        );
        map.insert(
            "avg_processing_time_ms".to_string(),
            format!("{:.2}", self.avg_processing_time_ms()),
        );
        map.insert(
            "success_rate".to_string(),
            format!("{:.2}", self.success_rate()),
        );
        map.insert(
            "in_flight".to_string(),
            self.in_flight.load(Ordering::Relaxed).to_string(),
        );
        map
    }
}

/// RequestResponse connector trait
///
/// Implement this trait to create connectors that handle synchronous
/// RPC-style operations with built-in concurrency control.
#[async_trait]
pub trait RequestResponseConnector: Send + Sync {
    /// The request type this connector handles
    type Request: Send + Sync + 'static;

    /// The response type this connector returns
    type Response: Send + Sync + 'static;

    /// The error type for connector operations
    type Error: std::error::Error + Send + Sync + 'static;

    /// Get the connector configuration
    fn config(&self) -> &RequestResponseConfig;

    /// Handle a request
    ///
    /// This is the main method to implement. It receives a request context
    /// and should return a response or error.
    async fn handle(
        &self,
        ctx: RequestContext<Self::Request>,
    ) -> Result<Self::Response, Self::Error>;

    /// Called before each request (for logging, validation, etc.)
    ///
    /// Return `Err` to reject the request before processing.
    async fn before_request(
        &self,
        _ctx: &RequestContext<Self::Request>,
    ) -> Result<(), Self::Error> {
        Ok(())
    }

    /// Called after each successful request (for logging, cleanup, etc.)
    async fn after_success(&self, _response: &Self::Response) {
        // Default: no-op
    }

    /// Called after a failed request (for logging, cleanup, etc.)
    async fn after_failure(&self, _error: &Self::Error) {
        // Default: no-op
    }

    /// Check if the connector is healthy
    async fn health_check(&self) -> Result<bool, Self::Error> {
        Ok(true)
    }
}

/// A wrapper that adds concurrency control to any RequestResponse connector
pub struct ConcurrentRequestHandler<C: RequestResponseConnector> {
    connector: C,
    semaphore: Arc<Semaphore>,
    metrics: Arc<RequestMetrics>,
}

impl<C: RequestResponseConnector> ConcurrentRequestHandler<C> {
    /// Create a new concurrent handler wrapping the given connector
    pub fn new(connector: C) -> Self {
        let max_concurrent = connector.config().max_concurrent;
        Self {
            connector,
            semaphore: Arc::new(Semaphore::new(max_concurrent)),
            metrics: Arc::new(RequestMetrics::new()),
        }
    }

    /// Create with custom semaphore permits
    pub fn with_concurrency(connector: C, max_concurrent: usize) -> Self {
        Self {
            connector,
            semaphore: Arc::new(Semaphore::new(max_concurrent)),
            metrics: Arc::new(RequestMetrics::new()),
        }
    }

    /// Handle a request with concurrency control
    pub async fn handle(
        &self,
        ctx: RequestContext<C::Request>,
    ) -> Result<Response<C::Response>, RequestError<C::Error>> {
        // Acquire permit
        let _permit = self
            .semaphore
            .acquire()
            .await
            .map_err(|_| RequestError::Shutdown)?;

        let start = Instant::now();
        self.metrics.start_request();

        // Apply timeout
        let timeout_duration = ctx.timeout;
        let result = tokio::time::timeout(timeout_duration, async {
            // Before hook
            self.connector.before_request(&ctx).await?;

            // Handle request

            self.connector.handle(ctx).await
        })
        .await;

        let duration = start.elapsed();

        match result {
            Ok(Ok(response)) => {
                self.connector.after_success(&response).await;
                self.metrics.complete_success(duration);
                Ok(Response::new(response).with_processing_time(duration))
            }
            Ok(Err(e)) => {
                self.connector.after_failure(&e).await;
                self.metrics.complete_failure(duration);
                Err(RequestError::Handler(e))
            }
            Err(_) => {
                self.metrics.complete_failure(duration);
                Err(RequestError::Timeout)
            }
        }
    }

    /// Get current metrics
    pub fn metrics(&self) -> &RequestMetrics {
        &self.metrics
    }

    /// Get available permits
    pub fn available_permits(&self) -> usize {
        self.semaphore.available_permits()
    }

    /// Get access to the underlying connector
    pub fn connector(&self) -> &C {
        &self.connector
    }
}

/// Errors that can occur during request handling
#[derive(Debug)]
pub enum RequestError<E> {
    /// The request timed out
    Timeout,
    /// The connector is shutting down
    Shutdown,
    /// Error from the handler
    Handler(E),
}

impl<E: std::fmt::Display> std::fmt::Display for RequestError<E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Timeout => write!(f, "Request timed out"),
            Self::Shutdown => write!(f, "Connector is shutting down"),
            Self::Handler(e) => write!(f, "Handler error: {e}"),
        }
    }
}

impl<E: std::error::Error + 'static> std::error::Error for RequestError<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Handler(e) => Some(e),
            _ => None,
        }
    }
}

/// A batch request for processing multiple items
#[derive(Debug, Clone)]
pub struct BatchRequest<T> {
    /// The items to process
    pub items: Vec<T>,
    /// Whether to stop on first error
    pub stop_on_error: bool,
    /// Maximum concurrent items
    pub max_concurrent: usize,
}

impl<T> BatchRequest<T> {
    /// Create a new batch request
    pub fn new(items: Vec<T>) -> Self {
        Self {
            items,
            stop_on_error: false,
            max_concurrent: 10,
        }
    }

    /// Stop processing on first error
    pub fn stop_on_error(mut self) -> Self {
        self.stop_on_error = true;
        self
    }

    /// Set max concurrent items
    pub fn with_concurrency(mut self, max: usize) -> Self {
        self.max_concurrent = max;
        self
    }
}

/// Result of a batch operation
#[derive(Debug)]
pub struct BatchResponse<T, E> {
    /// Successful results
    pub successes: Vec<T>,
    /// Failed items with errors
    pub failures: Vec<(usize, E)>,
    /// Total processing time
    pub processing_time: Duration,
}

impl<T, E> BatchResponse<T, E> {
    /// Check if all items succeeded
    pub fn all_succeeded(&self) -> bool {
        self.failures.is_empty()
    }

    /// Get success count
    pub fn success_count(&self) -> usize {
        self.successes.len()
    }

    /// Get failure count
    pub fn failure_count(&self) -> usize {
        self.failures.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_request_response_config() {
        let config = RequestResponseConfig::new()
            .with_max_concurrent(500)
            .with_timeout(Duration::from_secs(10))
            .with_metadata("env", "test");

        assert_eq!(config.max_concurrent, 500);
        assert_eq!(config.timeout, Duration::from_secs(10));
        assert_eq!(config.metadata.get("env"), Some(&"test".to_string()));

        let meta = config.to_metadata();
        assert_eq!(meta.get("max_concurrent"), Some(&"500".to_string()));
    }

    #[test]
    fn test_request_context() {
        let ctx = RequestContext::new("test request")
            .with_id("req-123")
            .with_header("trace-id", "abc")
            .with_timeout(Duration::from_secs(30));

        assert_eq!(ctx.request, "test request");
        assert_eq!(ctx.request_id, "req-123");
        assert_eq!(ctx.headers.get("trace-id"), Some(&"abc".to_string()));
        assert_eq!(ctx.timeout, Duration::from_secs(30));
        assert!(!ctx.is_timed_out());
    }

    #[test]
    fn test_response() {
        let response = Response::new("success")
            .with_metadata("cached", "true")
            .with_processing_time(Duration::from_millis(50));

        assert_eq!(response.data, "success");
        assert_eq!(response.metadata.get("cached"), Some(&"true".to_string()));
        assert_eq!(response.processing_time_ms, 50);

        // Test map
        let mapped = response.map(|s| s.to_uppercase());
        assert_eq!(mapped.data, "SUCCESS");
    }

    #[test]
    fn test_request_metrics() {
        let metrics = RequestMetrics::new();

        metrics.start_request();
        metrics.complete_success(Duration::from_millis(100));

        metrics.start_request();
        metrics.complete_failure(Duration::from_millis(50));

        assert_eq!(metrics.total_requests.load(Ordering::Relaxed), 2);
        assert_eq!(metrics.successful_requests.load(Ordering::Relaxed), 1);
        assert_eq!(metrics.failed_requests.load(Ordering::Relaxed), 1);
        assert_eq!(metrics.success_rate(), 50.0);
    }

    #[test]
    fn test_batch_request() {
        let batch = BatchRequest::new(vec![1, 2, 3, 4, 5])
            .stop_on_error()
            .with_concurrency(3);

        assert_eq!(batch.items.len(), 5);
        assert!(batch.stop_on_error);
        assert_eq!(batch.max_concurrent, 3);
    }

    #[tokio::test]
    async fn test_concurrent_handler() {
        struct EchoConnector {
            config: RequestResponseConfig,
        }

        #[async_trait]
        impl RequestResponseConnector for EchoConnector {
            type Request = String;
            type Response = String;
            type Error = std::io::Error;

            fn config(&self) -> &RequestResponseConfig {
                &self.config
            }

            async fn handle(
                &self,
                ctx: RequestContext<Self::Request>,
            ) -> Result<Self::Response, Self::Error> {
                Ok(format!("Echo: {}", ctx.request))
            }
        }

        let connector = EchoConnector {
            config: RequestResponseConfig::new().with_max_concurrent(10),
        };
        let handler = ConcurrentRequestHandler::new(connector);

        let ctx = RequestContext::new("hello".to_string());
        let response = handler.handle(ctx).await.unwrap();

        assert_eq!(response.data, "Echo: hello");
        assert!(response.processing_time_ms < 1000);

        assert_eq!(handler.metrics().total_requests.load(Ordering::Relaxed), 1);
        assert_eq!(
            handler
                .metrics()
                .successful_requests
                .load(Ordering::Relaxed),
            1
        );
    }
}