ringkernel-ecosystem 1.1.0

Ecosystem integrations for RingKernel - actors, web frameworks, data processing, ML
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
//! Tower service integration for RingKernel.
//!
//! This module provides Tower service implementations that wrap GPU Ring Kernels,
//! enabling use of Tower middleware (timeouts, rate limiting, load shedding, etc.)
//! with GPU-accelerated backends.
//!
//! # Example
//!
//! ```ignore
//! use tower::ServiceBuilder;
//! use ringkernel_ecosystem::tower::RingKernelService;
//!
//! let service = ServiceBuilder::new()
//!     .timeout(Duration::from_secs(5))
//!     .rate_limit(1000, Duration::from_secs(1))
//!     .service(RingKernelService::new(runtime, "vector_add"));
//! ```

use futures::future::BoxFuture;
use parking_lot::RwLock;
use std::marker::PhantomData;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;

use ringkernel_core::message::RingMessage;
use ringkernel_core::runtime::KernelId;
use tower_service::Service;

use crate::error::{EcosystemError, Result};

/// Configuration for RingKernel Tower service.
#[derive(Debug, Clone)]
pub struct ServiceConfig {
    /// Default timeout for operations.
    pub timeout: Duration,
    /// Maximum concurrent requests.
    pub max_concurrency: usize,
    /// Enable request buffering.
    pub buffer_requests: bool,
    /// Buffer size if buffering is enabled.
    pub buffer_size: usize,
}

impl Default for ServiceConfig {
    fn default() -> Self {
        Self {
            timeout: Duration::from_secs(30),
            max_concurrency: 1000,
            buffer_requests: false,
            buffer_size: 1024,
        }
    }
}

/// Runtime handle trait for abstracting the RingKernel runtime.
#[async_trait::async_trait]
pub trait RuntimeHandle: Send + Sync + Clone + 'static {
    /// Send a message to a kernel.
    async fn send_message(&self, kernel_id: &str, data: Vec<u8>) -> Result<()>;

    /// Receive a message from a kernel with timeout.
    async fn receive_message(&self, kernel_id: &str, timeout: Duration) -> Result<Vec<u8>>;

    /// Check if service is ready.
    fn is_ready(&self, kernel_id: &str) -> bool;
}

/// Ring Kernel as a Tower service.
///
/// This service wraps a GPU kernel and implements the Tower `Service` trait,
/// enabling use of Tower middleware and the broader Tower ecosystem.
pub struct RingKernelService<R, Req, Resp> {
    /// Runtime handle.
    runtime: R,
    /// Target kernel ID.
    kernel_id: KernelId,
    /// Configuration.
    config: ServiceConfig,
    /// Active request count.
    active_requests: Arc<AtomicU64>,
    /// Service state.
    state: Arc<RwLock<ServiceState>>,
    /// Type markers.
    _marker: PhantomData<(Req, Resp)>,
}

/// Internal service state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ServiceState {
    /// Service is ready to accept requests.
    Ready,
    /// Service is temporarily overloaded.
    #[allow(dead_code)]
    Overloaded,
    /// Service is shutting down.
    ShuttingDown,
}

impl<R, Req, Resp> RingKernelService<R, Req, Resp>
where
    R: RuntimeHandle,
{
    /// Create a new RingKernel service.
    pub fn new(runtime: R, kernel_id: impl Into<String>) -> Self {
        Self::with_config(runtime, kernel_id, ServiceConfig::default())
    }

    /// Create a new RingKernel service with custom configuration.
    pub fn with_config(runtime: R, kernel_id: impl Into<String>, config: ServiceConfig) -> Self {
        Self {
            runtime,
            kernel_id: KernelId::new(kernel_id),
            config,
            active_requests: Arc::new(AtomicU64::new(0)),
            state: Arc::new(RwLock::new(ServiceState::Ready)),
            _marker: PhantomData,
        }
    }

    /// Get the kernel ID.
    pub fn kernel_id(&self) -> &KernelId {
        &self.kernel_id
    }

    /// Get current active request count.
    pub fn active_requests(&self) -> u64 {
        self.active_requests.load(Ordering::Relaxed)
    }

    /// Check if service is accepting requests.
    pub fn is_accepting(&self) -> bool {
        *self.state.read() == ServiceState::Ready
            && (self.active_requests.load(Ordering::Relaxed) as usize) < self.config.max_concurrency
    }

    /// Gracefully shut down the service.
    pub fn shutdown(&self) {
        *self.state.write() = ServiceState::ShuttingDown;
    }
}

impl<R, Req, Resp> Clone for RingKernelService<R, Req, Resp>
where
    R: Clone,
{
    fn clone(&self) -> Self {
        Self {
            runtime: self.runtime.clone(),
            kernel_id: self.kernel_id.clone(),
            config: self.config.clone(),
            active_requests: self.active_requests.clone(),
            state: self.state.clone(),
            _marker: PhantomData,
        }
    }
}

/// Request wrapper for Tower service.
pub struct ServiceRequest<T> {
    /// The inner request.
    pub inner: T,
    /// Optional request ID for tracing.
    pub request_id: Option<String>,
}

impl<T> ServiceRequest<T> {
    /// Create a new service request.
    pub fn new(inner: T) -> Self {
        Self {
            inner,
            request_id: None,
        }
    }

    /// Create a service request with a request ID.
    pub fn with_id(inner: T, request_id: impl Into<String>) -> Self {
        Self {
            inner,
            request_id: Some(request_id.into()),
        }
    }
}

/// Response wrapper from Tower service.
#[derive(Debug, Clone)]
pub struct ServiceResponse<T> {
    /// The inner response.
    pub inner: T,
    /// Processing latency in microseconds.
    pub latency_us: u64,
    /// Request ID if provided.
    pub request_id: Option<String>,
}

impl<R, Req, Resp> Service<ServiceRequest<Req>> for RingKernelService<R, Req, Resp>
where
    R: RuntimeHandle,
    Req: RingMessage + Send + 'static,
    Resp: RingMessage + Send + 'static,
{
    type Response = ServiceResponse<Resp>;
    type Error = EcosystemError;
    type Future = BoxFuture<'static, Result<Self::Response>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<()>> {
        let state = *self.state.read();
        match state {
            ServiceState::ShuttingDown => Poll::Ready(Err(EcosystemError::ServiceUnavailable(
                "Service is shutting down".to_string(),
            ))),
            ServiceState::Overloaded => Poll::Ready(Err(EcosystemError::ServiceUnavailable(
                "Service is overloaded".to_string(),
            ))),
            ServiceState::Ready => {
                if self.runtime.is_ready(self.kernel_id.as_str()) {
                    Poll::Ready(Ok(()))
                } else {
                    Poll::Pending
                }
            }
        }
    }

    fn call(&mut self, req: ServiceRequest<Req>) -> Self::Future {
        let runtime = self.runtime.clone();
        let kernel_id = self.kernel_id.as_str().to_string();
        let timeout = self.config.timeout;
        let active_requests = self.active_requests.clone();
        let request_id = req.request_id.clone();

        // Increment active requests
        active_requests.fetch_add(1, Ordering::Relaxed);

        Box::pin(async move {
            let start = std::time::Instant::now();

            // Serialize request
            let data = req.inner.serialize();

            // Send to kernel
            let send_result = runtime.send_message(&kernel_id, data).await;
            if let Err(e) = send_result {
                active_requests.fetch_sub(1, Ordering::Relaxed);
                return Err(e);
            }

            // Wait for response
            let response_data = match runtime.receive_message(&kernel_id, timeout).await {
                Ok(data) => data,
                Err(e) => {
                    active_requests.fetch_sub(1, Ordering::Relaxed);
                    return Err(e);
                }
            };

            // Decrement active requests
            active_requests.fetch_sub(1, Ordering::Relaxed);

            // Deserialize response
            let response = Resp::deserialize(&response_data).map_err(|e| {
                EcosystemError::Deserialization(format!("Failed to deserialize response: {}", e))
            })?;

            let latency_us = start.elapsed().as_micros() as u64;

            Ok(ServiceResponse {
                inner: response,
                latency_us,
                request_id,
            })
        })
    }
}

/// Builder for constructing RingKernel services with middleware.
pub struct ServiceBuilder<R> {
    runtime: R,
    config: ServiceConfig,
}

impl<R: RuntimeHandle> ServiceBuilder<R> {
    /// Create a new service builder.
    pub fn new(runtime: R) -> Self {
        Self {
            runtime,
            config: ServiceConfig::default(),
        }
    }

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

    /// Set maximum concurrency.
    pub fn max_concurrency(mut self, max: usize) -> Self {
        self.config.max_concurrency = max;
        self
    }

    /// Enable request buffering.
    pub fn buffer(mut self, size: usize) -> Self {
        self.config.buffer_requests = true;
        self.config.buffer_size = size;
        self
    }

    /// Build a service for the specified kernel.
    pub fn build<Req, Resp>(self, kernel_id: impl Into<String>) -> RingKernelService<R, Req, Resp> {
        RingKernelService::with_config(self.runtime, kernel_id, self.config)
    }
}

/// Layer for creating RingKernel services.
///
/// Type parameters:
/// - `R`: The runtime handle type
/// - `Req`: The request message type
/// - `Resp`: The response message type
#[derive(Clone)]
pub struct RingKernelLayer<R, Req, Resp> {
    runtime: R,
    kernel_id: KernelId,
    config: ServiceConfig,
    _marker: PhantomData<(Req, Resp)>,
}

impl<R: RuntimeHandle, Req, Resp> RingKernelLayer<R, Req, Resp> {
    /// Create a new layer.
    pub fn new(runtime: R, kernel_id: impl Into<String>) -> Self {
        Self {
            runtime,
            kernel_id: KernelId::new(kernel_id),
            config: ServiceConfig::default(),
            _marker: PhantomData,
        }
    }

    /// Create a new layer with custom configuration.
    pub fn with_config(runtime: R, kernel_id: impl Into<String>, config: ServiceConfig) -> Self {
        Self {
            runtime,
            kernel_id: KernelId::new(kernel_id),
            config,
            _marker: PhantomData,
        }
    }
}

impl<R, S, Req, Resp> tower::Layer<S> for RingKernelLayer<R, Req, Resp>
where
    R: RuntimeHandle,
    Req: Send + 'static,
    Resp: Send + 'static,
{
    type Service = RingKernelService<R, Req, Resp>;

    fn layer(&self, _inner: S) -> Self::Service {
        RingKernelService::with_config(
            self.runtime.clone(),
            self.kernel_id.as_str(),
            self.config.clone(),
        )
    }
}

// ============================================================================
// PERSISTENT KERNEL SERVICE INTEGRATION
// ============================================================================

#[cfg(feature = "persistent")]
pub use persistent_service::*;

#[cfg(feature = "persistent")]
mod persistent_service {
    use super::*;
    use crate::persistent::{
        CommandId, PersistentCommand, PersistentConfig, PersistentHandle, PersistentStats,
    };

    /// Configuration for persistent Tower service.
    #[derive(Debug, Clone, Default)]
    pub struct PersistentServiceConfig {
        /// Base service config.
        pub base: ServiceConfig,
        /// Persistent kernel config.
        pub persistent: PersistentConfig,
        /// Whether to wait for command acknowledgment.
        pub wait_for_ack: bool,
    }

    impl PersistentServiceConfig {
        /// Create a new configuration with default values.
        pub fn new() -> Self {
            Self::default()
        }

        /// Set whether to wait for command acknowledgment.
        pub fn with_wait_for_ack(mut self, wait: bool) -> Self {
            self.wait_for_ack = wait;
            self
        }
    }

    /// Tower service wrapping a persistent GPU kernel.
    ///
    /// Unlike `RingKernelService` which uses launch-per-request, this service
    /// maintains a single long-running GPU kernel and injects commands
    /// with ~0.03µs latency (vs ~317µs for traditional kernel launches).
    ///
    /// # Example
    ///
    /// ```ignore
    /// use tower::ServiceBuilder;
    /// use ringkernel_ecosystem::tower::PersistentKernelService;
    ///
    /// let service = ServiceBuilder::new()
    ///     .timeout(Duration::from_secs(5))
    ///     .rate_limit(10000, Duration::from_secs(1))
    ///     .service(PersistentKernelService::new(handle));
    /// ```
    pub struct PersistentKernelService<H: PersistentHandle> {
        /// Handle to the persistent kernel.
        handle: Arc<H>,
        /// Configuration.
        config: PersistentServiceConfig,
        /// Active request count.
        active_requests: Arc<AtomicU64>,
        /// Service state.
        state: Arc<RwLock<ServiceState>>,
    }

    impl<H: PersistentHandle> PersistentKernelService<H> {
        /// Create a new persistent kernel service.
        pub fn new(handle: Arc<H>) -> Self {
            Self::with_config(handle, PersistentServiceConfig::default())
        }

        /// Create a new persistent kernel service with custom configuration.
        pub fn with_config(handle: Arc<H>, config: PersistentServiceConfig) -> Self {
            Self {
                handle,
                config,
                active_requests: Arc::new(AtomicU64::new(0)),
                state: Arc::new(RwLock::new(ServiceState::Ready)),
            }
        }

        /// Get the kernel ID.
        pub fn kernel_id(&self) -> &str {
            self.handle.kernel_id()
        }

        /// Check if the kernel is running.
        pub fn is_running(&self) -> bool {
            self.handle.is_running()
        }

        /// Get current statistics.
        pub fn stats(&self) -> PersistentStats {
            self.handle.stats()
        }

        /// Get current active request count.
        pub fn active_requests(&self) -> u64 {
            self.active_requests.load(Ordering::Relaxed)
        }

        /// Check if service is accepting requests.
        pub fn is_accepting(&self) -> bool {
            *self.state.read() == ServiceState::Ready
                && self.handle.is_running()
                && (self.active_requests.load(Ordering::Relaxed) as usize)
                    < self.config.base.max_concurrency
        }

        /// Gracefully shut down the service.
        pub fn shutdown(&self) {
            *self.state.write() = ServiceState::ShuttingDown;
        }
    }

    impl<H: PersistentHandle> Clone for PersistentKernelService<H> {
        fn clone(&self) -> Self {
            Self {
                handle: self.handle.clone(),
                config: self.config.clone(),
                active_requests: self.active_requests.clone(),
                state: self.state.clone(),
            }
        }
    }

    /// Request types for persistent kernel service.
    #[derive(Debug, Clone)]
    pub enum PersistentServiceRequest {
        /// Run simulation steps.
        RunSteps {
            /// Number of steps to run.
            count: u64,
        },
        /// Pause simulation.
        Pause,
        /// Resume simulation.
        Resume,
        /// Inject impulse at position.
        Inject {
            /// Position (x, y, z) to inject at.
            position: (u32, u32, u32),
            /// Value to inject.
            value: f32,
        },
        /// Get current statistics.
        GetStats,
        /// Custom command.
        Custom {
            /// Custom command type ID.
            type_id: u32,
            /// Custom command payload.
            payload: Vec<u8>,
        },
    }

    impl From<PersistentServiceRequest> for PersistentCommand {
        fn from(req: PersistentServiceRequest) -> Self {
            match req {
                PersistentServiceRequest::RunSteps { count } => {
                    PersistentCommand::RunSteps { count }
                }
                PersistentServiceRequest::Pause => PersistentCommand::Pause,
                PersistentServiceRequest::Resume => PersistentCommand::Resume,
                PersistentServiceRequest::Inject { position, value } => {
                    PersistentCommand::Inject { position, value }
                }
                PersistentServiceRequest::GetStats => PersistentCommand::GetStats,
                PersistentServiceRequest::Custom { type_id, payload } => {
                    PersistentCommand::Custom { type_id, payload }
                }
            }
        }
    }

    /// Response types for persistent kernel service.
    #[derive(Debug, Clone)]
    pub struct PersistentServiceResponse {
        /// Command ID for tracking.
        pub command_id: CommandId,
        /// Processing latency in nanoseconds.
        pub latency_ns: u64,
    }

    impl<H: PersistentHandle + 'static> Service<PersistentServiceRequest>
        for PersistentKernelService<H>
    {
        type Response = PersistentServiceResponse;
        type Error = EcosystemError;
        type Future = BoxFuture<'static, Result<Self::Response>>;

        fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<()>> {
            let state = *self.state.read();
            match state {
                ServiceState::ShuttingDown => Poll::Ready(Err(EcosystemError::ServiceUnavailable(
                    "Service is shutting down".to_string(),
                ))),
                ServiceState::Overloaded => Poll::Ready(Err(EcosystemError::ServiceUnavailable(
                    "Service is overloaded".to_string(),
                ))),
                ServiceState::Ready => {
                    if self.handle.is_running() {
                        Poll::Ready(Ok(()))
                    } else {
                        Poll::Ready(Err(EcosystemError::KernelNotRunning(
                            self.handle.kernel_id().to_string(),
                        )))
                    }
                }
            }
        }

        fn call(&mut self, req: PersistentServiceRequest) -> Self::Future {
            let handle = self.handle.clone();
            let active_requests = self.active_requests.clone();
            let wait_for_ack = self.config.wait_for_ack;
            let timeout = self.config.base.timeout;

            // Increment active requests
            active_requests.fetch_add(1, Ordering::Relaxed);

            Box::pin(async move {
                let start = std::time::Instant::now();

                // Convert to persistent command and send
                let cmd: PersistentCommand = req.into();
                let cmd_id = match handle.send_command(cmd) {
                    Ok(id) => id,
                    Err(e) => {
                        active_requests.fetch_sub(1, Ordering::Relaxed);
                        return Err(e);
                    }
                };

                // Optionally wait for acknowledgment
                if wait_for_ack {
                    if let Err(e) = handle.wait_for_command(cmd_id, timeout).await {
                        active_requests.fetch_sub(1, Ordering::Relaxed);
                        return Err(e);
                    }
                }

                // Decrement active requests
                active_requests.fetch_sub(1, Ordering::Relaxed);

                let latency_ns = start.elapsed().as_nanos() as u64;

                Ok(PersistentServiceResponse {
                    command_id: cmd_id,
                    latency_ns,
                })
            })
        }
    }

    /// Builder for constructing persistent kernel services.
    pub struct PersistentServiceBuilder<H: PersistentHandle> {
        handle: Arc<H>,
        config: PersistentServiceConfig,
    }

    impl<H: PersistentHandle> PersistentServiceBuilder<H> {
        /// Create a new service builder.
        pub fn new(handle: Arc<H>) -> Self {
            Self {
                handle,
                config: PersistentServiceConfig::default(),
            }
        }

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

        /// Set maximum concurrency.
        pub fn max_concurrency(mut self, max: usize) -> Self {
            self.config.base.max_concurrency = max;
            self
        }

        /// Set whether to wait for acknowledgment.
        pub fn wait_for_ack(mut self, wait: bool) -> Self {
            self.config.wait_for_ack = wait;
            self
        }

        /// Build the service.
        pub fn build(self) -> PersistentKernelService<H> {
            PersistentKernelService::with_config(self.handle, self.config)
        }
    }

    /// Layer for creating persistent kernel services.
    #[derive(Clone)]
    pub struct PersistentKernelLayer<H: PersistentHandle> {
        handle: Arc<H>,
        config: PersistentServiceConfig,
    }

    impl<H: PersistentHandle> PersistentKernelLayer<H> {
        /// Create a new layer.
        pub fn new(handle: Arc<H>) -> Self {
            Self {
                handle,
                config: PersistentServiceConfig::default(),
            }
        }

        /// Create a new layer with custom configuration.
        pub fn with_config(handle: Arc<H>, config: PersistentServiceConfig) -> Self {
            Self { handle, config }
        }
    }

    impl<H: PersistentHandle + 'static, S> tower::Layer<S> for PersistentKernelLayer<H> {
        type Service = PersistentKernelService<H>;

        fn layer(&self, _inner: S) -> Self::Service {
            PersistentKernelService::with_config(self.handle.clone(), self.config.clone())
        }
    }
}

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

    #[derive(Clone)]
    struct MockRuntime;

    #[async_trait::async_trait]
    impl RuntimeHandle for MockRuntime {
        async fn send_message(&self, _kernel_id: &str, _data: Vec<u8>) -> Result<()> {
            Ok(())
        }

        async fn receive_message(&self, _kernel_id: &str, _timeout: Duration) -> Result<Vec<u8>> {
            Ok(vec![1, 2, 3, 4])
        }

        fn is_ready(&self, _kernel_id: &str) -> bool {
            true
        }
    }

    #[test]
    fn test_service_config_default() {
        let config = ServiceConfig::default();
        assert_eq!(config.timeout, Duration::from_secs(30));
        assert_eq!(config.max_concurrency, 1000);
        assert!(!config.buffer_requests);
    }

    #[test]
    fn test_service_builder() {
        let runtime = MockRuntime;
        let service: RingKernelService<_, Vec<u8>, Vec<u8>> = ServiceBuilder::new(runtime)
            .timeout(Duration::from_secs(5))
            .max_concurrency(100)
            .build("test_kernel");

        assert_eq!(service.kernel_id().as_str(), "test_kernel");
        assert_eq!(service.active_requests(), 0);
        assert!(service.is_accepting());
    }

    #[test]
    fn test_service_shutdown() {
        let runtime = MockRuntime;
        let service: RingKernelService<_, Vec<u8>, Vec<u8>> =
            RingKernelService::new(runtime, "test_kernel");

        assert!(service.is_accepting());
        service.shutdown();
        assert!(!service.is_accepting());
    }
}