batched-queue 0.1.0-alpha.1

A high-performance, highly-concurrent batched queue implementation for Rust.
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
//! # `batched-queue`
//!
//! A high-performance, highly-concurrent batched queue implementation for Rust.
//!
//! `batched-queue` provides an efficient way to collect individual items into batches
//! for processing, which can significantly improve throughput in high-volume systems.
//!
//! ## Features
//!
//! - **Batching**: Automatically collects items into batches of a configurable size
//! - **Thread-safe**: Designed for concurrent usage with multiple producers and consumers
//! - **Backpressure**: Optional bounded queue to control memory usage
//! - **Flexible retrieval**: Blocking, non-blocking, and timeout-based batch retrieval
//! - **Multiple implementations**: Synchronous (default) and asynchronous modes via feature flags
//!
//! ## Usage
//!
//! By default, the crate provides a synchronous implementation:
//!
//! ```rust
//! use batched_queue::{BatchedQueue, BatchedQueueTrait};
//!
//! // Create a queue with batch size of 10
//! let queue = BatchedQueue::new(10).expect("Failed to create queue");
//!
//! // Create a sender that can be shared across threads
//! let sender = queue.create_sender();
//!
//! // Push items to the queue (in real usage, this would be in another thread)
//! for i in 0..25 {
//!     sender.push(i).expect("Failed to push item");
//! }
//!
//! // Flush any remaining items that haven't formed a complete batch
//! sender.flush().expect("Failed to flush");
//!
//! // Process batches
//! while let Some(batch) = queue.try_next_batch().expect("Failed to get batch") {
//!     println!("Processing batch of {} items", batch.len());
//!     for item in batch {
//!         // Process each item
//!         println!("  Item: {}", item);
//!     }
//! }
//! ```
//!
//! ## Feature Flags
//!
//! - [`sync`] (default): Enables the synchronous implementation using [`parking_lot`] and [`crossbeam_channel`]
//! - `async`: Enables the asynchronous implementation using tokio

use std::time::Duration;
use thiserror::Error;

// Export the sync BatchedQueue by default
#[cfg(feature = "sync")]
pub use sync::BatchedQueue;

/// Error type for a batched queue.
#[derive(Error, Debug, Clone)]
pub enum BatchedQueueError {
    #[error("Channel is full (backpressure limit reached)")]
    ChannelFull,

    #[error("Channel is disconnected (all receivers dropped)")]
    Disconnected,

    #[error("Operation timed out after {0:?}")]
    Timeout(Duration),

    #[error("Queue capacity exceeded: tried to add more than {max_capacity} items")]
    CapacityExceeded { max_capacity: usize },

    #[error("Invalid batch size: {0}")]
    InvalidBatchSize(usize),

    #[error("Failed to send batch: {0}")]
    SendError(String),

    #[error("Failed to receive batch: {0}")]
    ReceiveError(String),
}

/// Contextual information about [`BatchedQueueError`].
#[derive(Debug, Clone)]
pub struct ErrorContext {
    pub operation: String,
    pub queue_info: String,
}

impl BatchedQueueError {
    pub fn timeout(duration: Duration) -> Self {
        BatchedQueueError::Timeout(duration)
    }

    pub fn capacity_exceeded(max_capacity: usize) -> Self {
        BatchedQueueError::CapacityExceeded { max_capacity }
    }
}

/// Defines the common interface for batched queue implementations.
///
/// This trait provides methods for adding items to a queue, retrieving
/// batches of items, and checking queue status. All implementations must
/// handle the buffering of items until they form complete batches, and
/// provide mechanisms for flushing partial batches when needed.
///
/// # Examples
///
/// ```
/// use batched_queue::{BatchedQueue, BatchedQueueTrait};
///
/// // Create a queue with batch size of 10
/// let queue = BatchedQueue::new(10).expect("Failed to create queue");
///
/// // Create a sender that can be shared across threads
/// let sender = queue.create_sender();
///
/// // Push items to the queue (in real usage, this would be in another thread)
/// for i in 0..25 {
///     sender.push(i).expect("Failed to push item");
/// }
///
/// // Flush any remaining items that haven't formed a complete batch
/// sender.flush().expect("Failed to flush");
///
/// // Process batches
/// while let Some(batch) = queue.try_next_batch().expect("Failed to get batch") {
///     println!("Processing batch of {} items", batch.len());
///     for item in batch {
///         // Process each item
///         println!("  Item: {}", item);
///     }
/// }
/// ```
pub trait BatchedQueueTrait<T> {
    /// Returns the current number of items in the queue.
    fn len(&self) -> usize;

    /// Returns the maximum number of items a batch can hold.
    fn capacity(&self) -> usize;

    /// Returns `true` if the queue has no items waiting to be processed.
    fn is_empty(&self) -> bool;

    /// Adds an item to the queue.
    ///
    /// If adding this item causes the current batch to reach the configured
    /// batch size, the batch will be automatically sent for processing.
    ///
    /// # Arguments
    ///
    /// * `item` - The item to add to the queue
    ///
    /// # Errors
    ///
    /// Returns `BatchedQueueError::Disconnected` if the receiving end has been dropped,
    /// or other implementation-specific errors.
    fn push(&self, item: T) -> Result<(), BatchedQueueError>;

    /// Attempts to retrieve the next batch of items without blocking.
    ///
    /// # Returns
    ///
    /// * `Ok(Some(batch))` - A batch of items is available
    /// * `Ok(None)` - No batch is currently available
    ///
    /// # Errors
    ///
    /// Returns `BatchedQueueError::Disconnected` if the sending end has been dropped,
    /// or other implementation-specific errors.
    fn try_next_batch(&self) -> Result<Option<Vec<T>>, BatchedQueueError>;

    /// Retrieves the next batch of items, blocking until one is available.
    ///
    /// # Errors
    ///
    /// Returns `BatchedQueueError::Disconnected` if the sending end has been dropped,
    /// or other implementation-specific errors.
    fn next_batch(&self) -> Result<Vec<T>, BatchedQueueError>;

    /// Retrieves the next batch of items, blocking until one is available or until the timeout expires.
    ///
    /// # Arguments
    ///
    /// * `timeout` - Maximum time to wait for a batch to become available
    ///
    /// # Errors
    ///
    /// Returns:
    /// * `BatchedQueueError::Timeout` if no batch becomes available within the timeout period
    /// * `BatchedQueueError::Disconnected` if the sending end has been dropped
    /// * Other implementation-specific errors
    fn next_batch_timeout(&self, timeout: std::time::Duration)
    -> Result<Vec<T>, BatchedQueueError>;

    /// Flushes any pending items into a batch, even if the batch is not full.
    ///
    /// This is useful for ensuring that all items are processed, especially
    /// during shutdown or when batches need to be processed on demand.
    ///
    /// # Errors
    ///
    /// Returns `BatchedQueueError::Disconnected` if the receiving end has been dropped,
    /// or other implementation-specific errors.
    fn flush(&self) -> Result<(), BatchedQueueError>;
}

#[cfg(feature = "sync")]
pub mod sync {
    //! Synchronous implementation of the batched queue.
    //!
    //! This module provides a thread-safe implementation using [`crossbeam_channel`]
    //! for communication and [`parking_lot::Mutex`] for low-contention locking.
    //! It is designed for high-throughput scenarios where items need to be
    //! processed in batches.

    use super::*;
    use crossbeam_channel as channel;
    use parking_lot::Mutex;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    /// A thread-safe, high-performance queue that automatically batches items.
    ///
    /// [`BatchedQueue`] collects individual items until reaching the configured batch size,
    /// then automatically makes the batch available for processing. This batching approach
    /// can significantly improve throughput in high-volume systems by reducing overhead.
    ///
    /// # Examples
    ///
    /// ```
    /// use batched_queue::{BatchedQueue, BatchedQueueTrait};
    ///
    /// use std::thread;
    /// use std::time::Duration;
    ///
    /// // Create a queue with batch size of 5
    /// let queue = BatchedQueue::new(5).expect("Failed to create queue");
    ///
    /// // Create a sender that can be shared across threads
    /// let sender = queue.create_sender();
    ///
    /// // Producer thread
    /// let producer = thread::spawn(move || {
    ///     for i in 0..20 {
    ///         sender.push(i).expect("Failed to push item");
    ///         thread::sleep(Duration::from_millis(10));
    ///     }
    ///     sender.flush().expect("Failed to flush"); // Send any remaining items
    /// });
    ///
    /// // Consumer thread
    /// let consumer = thread::spawn(move || {
    ///     let mut all_items = Vec::new();
    ///     
    ///     // Process batches as they become available
    ///     while all_items.len() < 20 {
    ///         if let Ok(batch) = queue.next_batch_timeout(Duration::from_millis(100)) {
    ///             all_items.extend(batch);
    ///         }
    ///     }
    ///     
    ///     all_items
    /// });
    ///
    /// // Wait for threads to complete
    /// producer.join().unwrap();
    /// let result = consumer.join().unwrap();
    ///
    /// assert_eq!(result.len(), 20);
    /// ```
    pub struct BatchedQueue<T> {
        batch_size: usize,
        current_batch: Arc<Mutex<Vec<T>>>,
        batch_receiver: channel::Receiver<Vec<T>>,
        batch_sender: channel::Sender<Vec<T>>,
        item_count: Arc<AtomicUsize>,
    }

    impl<T: Send + 'static> BatchedQueue<T> {
        /// Creates a new batched queue with the specified batch size and an unbounded channel.
        ///
        /// # Arguments
        ///
        /// * `batch_size` - The number of items to collect before forming a batch
        ///
        /// # Examples
        ///
        /// ```
        /// use batched_queue::BatchedQueue;
        ///
        /// let queue = BatchedQueue::<String>::new(10).expect("Failed to create queue");
        /// ```
        pub fn new(batch_size: usize) -> Result<Self, BatchedQueueError> {
            if batch_size == 0 {
                return Err(BatchedQueueError::InvalidBatchSize(batch_size));
            }

            let (batch_sender, batch_receiver) = channel::unbounded();
            Ok(Self {
                batch_size,
                current_batch: Arc::new(Mutex::new(Vec::with_capacity(batch_size))),
                batch_receiver,
                batch_sender,
                item_count: Arc::new(AtomicUsize::new(0)),
            })
        }

        /// Creates a new batched queue with a bounded channel for backpressure.
        ///
        /// Using a bounded channel helps control memory usage by limiting the number
        /// of batches that can be queued at once. When the channel is full, producers
        /// will block when attempting to send a full batch.
        ///
        /// # Arguments
        ///
        /// * `batch_size` - The number of items to collect before forming a batch
        /// * `max_batches` - The maximum number of batches that can be queued
        ///
        /// # Examples
        ///
        /// ```
        /// use batched_queue::BatchedQueue;
        ///
        /// // Create a queue with batch size 10 and at most 5 batches in the channel
        /// let queue = BatchedQueue::<i32>::new_bounded(10, 5).expect("Failed to create queue");
        /// ```
        pub fn new_bounded(
            batch_size: usize,
            max_batches: usize,
        ) -> Result<Self, BatchedQueueError> {
            if batch_size == 0 {
                return Err(BatchedQueueError::InvalidBatchSize(batch_size));
            }
            if max_batches == 0 {
                return Err(BatchedQueueError::InvalidBatchSize(max_batches));
            }

            let (batch_sender, batch_receiver) = channel::bounded(max_batches);
            Ok(Self {
                batch_size,
                current_batch: Arc::new(Mutex::new(Vec::with_capacity(batch_size))),
                batch_receiver,
                batch_sender,
                item_count: Arc::new(AtomicUsize::new(0)),
            })
        }

        /// Creates a sender for this queue that can be shared across threads.
        ///
        /// Multiple senders can be created from a single queue, allowing
        /// for concurrent producers.
        ///
        /// # Returns
        ///
        /// A new [`BatchedQueueSender`] linked to this queue
        ///
        /// # Examples
        ///
        /// ```
        /// use batched_queue::BatchedQueue;
        /// use std::thread;
        ///
        /// let queue = BatchedQueue::<i32>::new(10).expect("Failed to create queue");
        ///
        /// // Create multiple senders for different threads
        /// let sender1 = queue.create_sender();
        /// let sender2 = queue.create_sender();
        ///
        /// // Use senders in different threads
        /// let t1 = thread::spawn(move || {
        ///     for i in 0..10 {
        ///         sender1.push(i).expect("Failed to push item");
        ///     }
        /// });
        ///
        /// let t2 = thread::spawn(move || {
        ///     for i in 10..20 {
        ///         sender2.push(i).expect("Failed to push item");
        ///     }
        /// });
        ///
        /// // Wait for producers to finish
        /// t1.join().unwrap();
        /// t2.join().unwrap();
        /// ```
        pub fn create_sender(&self) -> BatchedQueueSender<T> {
            BatchedQueueSender {
                batch_size: self.batch_size,
                current_batch: self.current_batch.clone(),
                batch_sender: self.batch_sender.clone(),
                item_count: self.item_count.clone(),
            }
        }

        /// Takes any items left in the current batch and returns them when shutting down.
        ///
        /// This method is useful during controlled shutdown to collect any remaining items
        /// that haven't formed a complete batch.
        ///
        /// # Returns
        ///
        /// A vector containing any items that were in the current batch
        ///
        /// # Examples
        ///
        /// ```
        /// use batched_queue::BatchedQueue;
        ///
        /// let queue = BatchedQueue::<i32>::new(10).expect("Failed to create queue");
        /// let sender = queue.create_sender();
        ///
        /// // Add some items, but not enough to form a complete batch
        /// for i in 0..3 {
        ///     sender.push(i).expect("Failed to push item");
        /// }
        ///
        /// // Close the queue and get remaining items
        /// let remaining = queue.close_queue();
        /// assert_eq!(remaining.len(), 3);
        /// ```
        pub fn close_queue(&self) -> Vec<T> {
            // Take any items left in the current batch
            let mut batch = self.current_batch.lock();
            std::mem::take(&mut *batch)
        }
    }

    impl<T: Send + 'static> BatchedQueueTrait<T> for BatchedQueue<T> {
        fn push(&self, item: T) -> Result<(), BatchedQueueError> {
            let mut batch = self.current_batch.lock();
            batch.push(item);

            let count = self.item_count.fetch_add(1, Ordering::SeqCst);

            if count % self.batch_size == self.batch_size - 1 {
                let full_batch =
                    std::mem::replace(&mut *batch, Vec::with_capacity(self.batch_size));

                self.batch_sender
                    .send(full_batch)
                    .map_err(|_| BatchedQueueError::Disconnected)?;
            }

            Ok(())
        }

        fn try_next_batch(&self) -> Result<Option<Vec<T>>, BatchedQueueError> {
            match self.batch_receiver.try_recv() {
                Ok(batch) => Ok(Some(batch)),
                Err(channel::TryRecvError::Empty) => Ok(None),
                Err(channel::TryRecvError::Disconnected) => Err(BatchedQueueError::Disconnected),
            }
        }

        fn next_batch(&self) -> Result<Vec<T>, BatchedQueueError> {
            self.batch_receiver
                .recv()
                .map_err(|_| BatchedQueueError::Disconnected)
        }

        fn next_batch_timeout(
            &self,
            timeout: std::time::Duration,
        ) -> Result<Vec<T>, BatchedQueueError> {
            match self.batch_receiver.recv_timeout(timeout) {
                Ok(batch) => Ok(batch),
                Err(channel::RecvTimeoutError::Timeout) => Err(BatchedQueueError::Timeout(timeout)),
                Err(channel::RecvTimeoutError::Disconnected) => {
                    Err(BatchedQueueError::Disconnected)
                }
            }
        }

        fn len(&self) -> usize {
            self.item_count.load(Ordering::SeqCst)
        }

        fn capacity(&self) -> usize {
            self.batch_size
        }

        fn flush(&self) -> Result<(), BatchedQueueError> {
            let mut batch = self.current_batch.lock();
            if !batch.is_empty() {
                let partial_batch =
                    std::mem::replace(&mut *batch, Vec::with_capacity(self.batch_size));
                self.batch_sender
                    .send(partial_batch)
                    .map_err(|_| BatchedQueueError::Disconnected)?;
            }
            Ok(())
        }

        fn is_empty(&self) -> bool {
            self.batch_receiver.is_empty() && self.current_batch.lock().is_empty()
        }
    }

    /// A sender handle for adding items to a batched queue.
    ///
    /// `BatchedQueueSender` provides methods to add items to a batched queue from multiple
    /// threads. It handles the details of batch management and automatic flushing of batches
    /// when they reach the configured size.
    ///
    /// # Examples
    ///
    /// ```
    /// use batched_queue::BatchedQueue;
    /// use std::thread;
    ///
    /// let queue = BatchedQueue::<String>::new(5).expect("Failed to create queue");
    /// let sender = queue.create_sender();
    ///
    /// // Share the sender with another thread
    /// thread::spawn(move || {
    ///     for i in 0..10 {
    ///         sender.push(format!("Item {}", i)).expect("Failed to push item");
    ///     }
    ///     
    ///     // Ensure any remaining items are sent
    ///     sender.flush().expect("Failed to flush");
    /// });
    /// ```
    pub struct BatchedQueueSender<T> {
        batch_size: usize,
        current_batch: Arc<Mutex<Vec<T>>>,
        batch_sender: channel::Sender<Vec<T>>,
        item_count: Arc<AtomicUsize>,
    }

    impl<T: Send + 'static> Clone for BatchedQueueSender<T> {
        fn clone(&self) -> Self {
            Self {
                batch_size: self.batch_size,
                current_batch: self.current_batch.clone(),
                batch_sender: self.batch_sender.clone(),
                item_count: self.item_count.clone(),
            }
        }
    }

    impl<T: Send + Clone + 'static> BatchedQueueSender<T> {
        /// Adds an item to the queue.
        ///
        /// If adding this item causes the current batch to reach the configured
        /// batch size, the batch will be automatically sent for processing.
        /// This method will block if the channel is bounded and full.
        ///
        /// # Arguments
        ///
        /// * `item` - The item to add to the queue
        ///
        /// # Examples
        ///
        /// ```
        /// use batched_queue::BatchedQueue;
        ///
        /// let queue = BatchedQueue::<i32>::new(5).expect("Failed to create queue");
        /// let sender = queue.create_sender();
        ///
        /// for i in 0..10 {
        ///     sender.push(i).expect("Failed to push item");
        /// }
        /// ```
        pub fn push(&self, item: T) -> Result<(), BatchedQueueError> {
            let should_send_batch;
            let mut full_batch = None;

            {
                let mut batch = self.current_batch.lock();
                batch.push(item);

                let count = self.item_count.fetch_add(1, Ordering::SeqCst);
                should_send_batch = count % self.batch_size == self.batch_size - 1;

                if should_send_batch {
                    // Take the batch but minimize time in the critical section
                    full_batch = Some(std::mem::replace(
                        &mut *batch,
                        Vec::with_capacity(self.batch_size),
                    ));
                }
            }

            if let Some(batch) = full_batch {
                self.batch_sender
                    .send(batch)
                    .map_err(|_| BatchedQueueError::Disconnected)?;
            }

            Ok(())
        }

        /// Attempts to add an item to the queue without blocking.
        ///
        /// This method is similar to `push`, but it will not block if the channel
        /// is bounded and full. Instead, if a full batch cannot be sent because
        /// the channel is full, the batch is kept in the current batch and will
        /// be sent on a future push or flush operation.
        ///
        /// # Arguments
        ///
        /// * `item` - The item to add to the queue
        ///
        /// # Examples
        ///
        /// ```
        /// use batched_queue::BatchedQueue;
        ///
        /// // Create a queue with limited capacity
        /// let queue = BatchedQueue::<i32>::new_bounded(5, 1).expect("Failed to create queue");
        /// let sender = queue.create_sender();
        ///
        /// for i in 0..20 {
        ///     sender.try_push(i);
        /// }
        /// ```
        pub fn try_push(&self, item: T) -> Result<(), BatchedQueueError> {
            let should_send_batch;
            let mut full_batch = None;

            {
                let mut batch = self.current_batch.lock();
                batch.push(item);

                let count = self.item_count.fetch_add(1, Ordering::SeqCst);
                should_send_batch = count % self.batch_size == self.batch_size - 1;

                if should_send_batch {
                    // Take the batch but minimize time in the critical section
                    full_batch = Some(std::mem::replace(
                        &mut *batch,
                        Vec::with_capacity(self.batch_size),
                    ));
                }
            }

            if let Some(batch_to_send) = full_batch {
                match self.batch_sender.try_send(batch_to_send.clone()) {
                    Ok(_) => {}
                    Err(channel::TrySendError::Full(_)) => {
                        // If channel is full, we need to restore the batch
                        {
                            let mut batch = self.current_batch.lock();
                            // This assumes the batch is empty, which it should be after the replace above
                            *batch = batch_to_send;
                        }
                        // We didn't actually send a batch, so decrement the count
                        self.item_count.fetch_sub(1, Ordering::SeqCst);
                        return Err(BatchedQueueError::ChannelFull);
                    }
                    Err(channel::TrySendError::Disconnected(_)) => {
                        return Err(BatchedQueueError::Disconnected);
                    }
                }
            }

            Ok(())
        }

        /// Flushes any pending items into a batch, even if the batch is not full.
        ///
        /// This method will block if the channel is bounded and full.
        ///
        /// # Error
        ///
        /// Returns `BatchedQueueError::Disconnected` if the receiving end has been dropped,
        ///
        /// # Examples
        ///
        /// ```
        /// use batched_queue::BatchedQueue;
        ///
        /// let queue = BatchedQueue::<i32>::new(10).expect("Failed to create queue");
        /// let sender = queue.create_sender();
        ///
        /// // Add some items, but not enough to form a complete batch
        /// for i in 0..3 {
        ///     sender.push(i).expect("Failed to push item");
        /// }
        ///
        /// // Flush to ensure items are sent for processing
        /// sender.flush().expect("Failed to flush");
        /// ```
        pub fn flush(&self) -> Result<(), BatchedQueueError> {
            let mut batch = self.current_batch.lock();
            if !batch.is_empty() {
                let partial_batch =
                    std::mem::replace(&mut *batch, Vec::with_capacity(self.batch_size));
                self.batch_sender
                    .send(partial_batch)
                    .map_err(|_| BatchedQueueError::Disconnected)?;
            }
            Ok(())
        }

        /// Attempts to flush any pending items without blocking.
        ///
        /// # Errors
        ///
        /// Returns `BatchedQueueError::Disconnected` if the receiving end has been dropped,
        /// or `BatchedQueueError::ChannelFull` if the channel is full.
        ///
        /// # Examples
        ///
        /// ```
        /// use batched_queue::BatchedQueue;
        ///
        /// // Create a queue with limited capacity
        /// let queue = BatchedQueue::<i32>::new_bounded(5, 1).expect("Failed to create queue");
        /// let sender = queue.create_sender();
        ///
        /// for i in 0..3 {
        ///     sender.push(i).expect("Failed to push item");
        /// }
        ///
        /// // Try to flush without blocking
        /// if !sender.try_flush().is_ok() {
        ///     println!("Channel is full, will try again later");
        /// }
        /// ```
        pub fn try_flush(&self) -> Result<(), BatchedQueueError> {
            let mut batch = self.current_batch.lock();
            if !batch.is_empty() {
                let partial_batch =
                    std::mem::replace(&mut *batch, Vec::with_capacity(self.batch_size));
                match self.batch_sender.try_send(partial_batch) {
                    Ok(_) => Ok(()),
                    Err(channel::TrySendError::Full(_)) => Err(BatchedQueueError::ChannelFull),
                    Err(channel::TrySendError::Disconnected(_)) => {
                        Err(BatchedQueueError::Disconnected)
                    }
                }
            } else {
                Ok(())
            }
        }
    }

    // For testing thread-safe behavior
    #[cfg(test)]
    mod tests {
        use super::*;
        use std::thread;
        use std::time::Duration;

        #[test]
        fn multithreaded() {
            let queue = BatchedQueue::<i32>::new(10).expect("Failed to create queue");
            let sender1 = queue.create_sender();
            let sender2 = queue.create_sender();

            // Thread 1: Push numbers 0-49
            let t1 = thread::spawn(move || {
                for i in 0..50 {
                    sender1.push(i).expect("Failed to push item");
                    thread::sleep(Duration::from_millis(1));
                }
                sender1.flush().expect("Failed to flush");
            });

            // Thread 2: Push numbers 100-149
            let t2 = thread::spawn(move || {
                for i in 100..150 {
                    sender2.push(i).expect("Failed to push item");
                    thread::sleep(Duration::from_millis(1));
                }
                sender2.flush().expect("Failed to flush");
            });

            // Consumer thread: Collect all batches
            let t3 = thread::spawn(move || {
                let mut all_items = Vec::new();

                // Collect for a reasonable amount of time
                for _ in 0..15 {
                    if let Some(batch) = queue.try_next_batch().expect("Failed to get batch") {
                        all_items.extend(batch);
                    }
                    thread::sleep(Duration::from_millis(10));
                }

                // Make sure we got all remaining batches
                while let Some(batch) = queue.try_next_batch().expect("Failed to get batch") {
                    all_items.extend(batch);
                }

                all_items
            });

            // Wait for producer threads to finish
            t1.join().unwrap();
            t2.join().unwrap();

            // Get results from consumer
            let result = t3.join().unwrap();

            // Verify we have all 100 items
            assert_eq!(result.len(), 100);

            // Check that we have all numbers
            let mut result_sorted = result.clone();
            result_sorted.sort();

            // Verify we got all numbers 0-49 and 100-149
            for i in 0..50 {
                assert!(result_sorted.contains(&i));
                assert!(result_sorted.contains(&(i + 100)));
            }
        }

        #[test]
        fn timeout() {
            let queue = BatchedQueue::<i32>::new(5).expect("Failed to create queue");
            let sender = queue.create_sender();

            // Add 3 items (not enough to trigger a batch)
            for i in 1..4 {
                sender.push(i).unwrap();
            }

            // Try to get a batch with a short timeout - should time out
            let result = queue.next_batch_timeout(Duration::from_millis(10));
            assert!(result.is_err());

            // Now flush the incomplete batch
            sender.flush().unwrap();

            // Should get the batch now
            let batch = queue.next_batch_timeout(Duration::from_millis(10)).unwrap();
            assert_eq!(batch, vec![1, 2, 3]);
        }

        #[test]
        fn bounded_channel() {
            // Create a bounded queue with batch size 5 and max 2 batches in the channel
            let queue = BatchedQueue::new_bounded(5, 2).expect("Failed to create queue");
            let sender = queue.create_sender();

            // Producer thread
            let handle = thread::spawn(move || {
                let mut successful_pushes = 0;
                // Try to push 20 items
                for item_idx in 0..20 {
                    // Use push which will block if the channel is full
                    sender.push(item_idx).expect("Failed to push item");
                    successful_pushes += 1;

                    // Add a small delay
                    if item_idx % 5 == 4 {
                        // Every 5th item, wait a bit longer
                        thread::sleep(Duration::from_millis(5));
                    }
                }
                sender.flush().expect("Failed to flush");
                successful_pushes
            });

            // Consumer thread - retrieve batches to prevent deadlock
            let mut received_batches = 0;
            let mut all_items = Vec::new();

            // Receive batches while the producer is running
            while received_batches < 4 {
                // Expect 4 full batches of 5 items each
                if let Some(batch) = queue.try_next_batch().expect("Failed to get batch") {
                    received_batches += 1;
                    all_items.extend(batch);
                }
                thread::sleep(Duration::from_millis(5));
            }

            // Wait for producer to finish
            let successful_pushes = handle.join().unwrap();

            // Receive any remaining batches
            while let Some(batch) = queue.try_next_batch().expect("Failed to get batch") {
                all_items.extend(batch);
            }

            // Should have all 20 items
            assert_eq!(all_items.len(), 20);
            assert_eq!(successful_pushes, 20);

            // Verify we have all numbers 0-19
            let mut sorted_items = all_items.clone();
            sorted_items.sort();
            for i in 0..20 {
                assert!(sorted_items.contains(&i));
            }
        }

        #[test]
        fn backpressure() {
            // Create a bounded queue with backpressure
            let queue = BatchedQueue::new_bounded(5, 1).expect("Failed to create queue"); // Only 1 batch in the channel
            let sender = queue.create_sender();

            // Fill the first batch and send it
            for i in 0..5 {
                sender.push(i).expect("Failed to push item");
            }
            // Now the batch is automatically sent because it's full

            // Create a partial second batch
            for i in 5..8 {
                sender.push(i).expect("Failed to push item");
            }

            // At this point, we have one full batch in the channel and a partial batch in current_batch

            // Get the first batch to make room in the channel
            let batch = queue.next_batch().expect("Failed to get batch");
            assert_eq!(batch, vec![0, 1, 2, 3, 4]);

            // Now flush the partial batch - this should succeed
            assert!(sender.try_flush().is_ok());

            // And we should get the second batch
            let batch = queue
                .next_batch_timeout(Duration::from_millis(50))
                .expect("Failed to get batch");
            assert_eq!(batch, vec![5, 6, 7]);
        }

        #[test]
        fn error_handling() {
            // Test invalid batch size
            let invalid_queue = BatchedQueue::<i32>::new(0);
            assert!(matches!(
                invalid_queue,
                Err(BatchedQueueError::InvalidBatchSize(0))
            ));

            // Create a queue with very limited capacity - only 1 batch in the channel
            let limited_queue = BatchedQueue::new_bounded(5, 1).expect("Failed to create queue");
            let limited_sender = limited_queue.create_sender();

            // Fill the channel with one complete batch
            for i in 0..5 {
                limited_sender
                    .push(i)
                    .expect("Should succeed for first batch");
            }

            // At this point, we have one full batch in the channel
            // Now, add items to start building a second batch
            for i in 5..9 {
                limited_sender
                    .push(i)
                    .expect("Should succeed as we're building a partial batch");
            }

            // Try to complete the second batch, which should fail with ChannelFull
            // because when it completes, it immediately tries to send it
            let result = limited_sender.try_push(9);
            assert!(matches!(result, Err(BatchedQueueError::ChannelFull)));

            // Now let's test the timeout
            // First ensure there's nothing ready in the queue by consuming the batch
            limited_queue
                .next_batch()
                .expect("Should get the first batch");

            // Now we should have nothing in the channel and only a partial batch
            // Try to get a batch with a very short timeout
            let result = limited_queue.next_batch_timeout(Duration::from_millis(1));
            assert!(matches!(result, Err(BatchedQueueError::Timeout(_))));
        }

        #[cfg(test)]
        mod stress_tests {
            use super::*;
            use std::collections::HashSet;
            use std::sync::atomic::{AtomicUsize, Ordering};
            use std::sync::{Arc, Barrier};
            use std::thread;
            use std::time::{Duration, Instant};

            #[test]
            fn batched_queue() {
                // Configuration parameters
                const BATCH_SIZE: usize = 100;
                const CHANNEL_CAPACITY: usize = 10;
                const PRODUCER_COUNT: usize = 64;
                const ITEMS_PER_PRODUCER: usize = 10_000;
                const CONSUMER_COUNT: usize = 4;

                // Wrap the queue in an Arc so it can be safely shared between threads
                let queue = Arc::new(
                    BatchedQueue::new_bounded(BATCH_SIZE, CHANNEL_CAPACITY)
                        .expect("Failed to create queue"),
                );

                // Setup synchronization primitives
                let start_barrier = Arc::new(Barrier::new(PRODUCER_COUNT + CONSUMER_COUNT + 1));
                let total_expected_items = PRODUCER_COUNT * ITEMS_PER_PRODUCER;
                let processed_items = Arc::new(AtomicUsize::new(0));

                // Tracking data structures for verification
                let all_produced_items = Arc::new(parking_lot::Mutex::new(HashSet::new()));
                let all_consumed_items = Arc::new(parking_lot::Mutex::new(HashSet::new()));

                // Track performance metrics
                let producer_times = Arc::new(parking_lot::Mutex::new(Vec::new()));
                let consumer_times = Arc::new(parking_lot::Mutex::new(Vec::new()));

                // Create and launch producer threads
                let producer_handles: Vec<_> = (0..PRODUCER_COUNT)
                    .map(|producer_id| {
                        let queue_sender = queue.create_sender();
                        let start = start_barrier.clone();
                        let produced = all_produced_items.clone();
                        let producer_timing = producer_times.clone();

                        thread::spawn(move || {
                            // Wait for all threads to be ready
                            start.wait();
                            let start_time = Instant::now();

                            // Producer offset ensures each producer generates unique values
                            let offset = producer_id * ITEMS_PER_PRODUCER;

                            // Track items we produced in this thread
                            let mut local_produced = HashSet::new();

                            for i in 0..ITEMS_PER_PRODUCER {
                                let item = offset + i;
                                queue_sender.push(item).expect("Failed to push item");
                                local_produced.insert(item);

                                // Occasionally sleep to create more contention patterns
                                if i % 1000 == 0 {
                                    thread::sleep(Duration::from_micros(10));
                                }
                            }

                            // Ensure final batch is sent
                            queue_sender.flush().expect("Failed to flush");

                            // Record items this producer generated
                            let mut global_produced = produced.lock();
                            for item in local_produced {
                                global_produced.insert(item);
                            }

                            let elapsed = start_time.elapsed();
                            producer_timing.lock().push(elapsed);

                            println!("Producer {}: Finished in {:?}", producer_id, elapsed);
                        })
                    })
                    .collect();

                // Create and launch consumer threads
                let consumer_handles: Vec<_> = (0..CONSUMER_COUNT)
                    .map(|consumer_id| {
                        let queue = queue.clone(); // Clone the Arc, not the queue itself
                        let start = start_barrier.clone();
                        let processed = processed_items.clone();
                        let consumed = all_consumed_items.clone();
                        let consumer_timing = consumer_times.clone();

                        thread::spawn(move || {
                            // Wait for all threads to be ready
                            start.wait();
                            let start_time = Instant::now();

                            // Track items consumed by this thread
                            let mut local_consumed = HashSet::new();
                            let mut batches_processed = 0;

                            loop {
                                // Try to get a batch with timeout
                                if let Ok(batch) =
                                    queue.next_batch_timeout(Duration::from_millis(100))
                                {
                                    batches_processed += 1;
                                    let batch_size = batch.len();

                                    // Process each item in the batch
                                    for item in batch {
                                        local_consumed.insert(item);
                                    }

                                    // Update total processed count
                                    let current = processed.fetch_add(batch_size, Ordering::SeqCst);

                                    // Check if we've processed all expected items
                                    if current + batch_size >= total_expected_items {
                                        break;
                                    }
                                } else if processed.load(Ordering::SeqCst) >= total_expected_items {
                                    // No more batches and we've processed all expected items
                                    break;
                                }

                                // Occasionally check if we're done to avoid waiting for full timeout
                                if processed.load(Ordering::SeqCst) >= total_expected_items {
                                    break;
                                }
                            }

                            // Record the items this consumer processed
                            let mut global_consumed = consumed.lock();
                            for item in local_consumed {
                                global_consumed.insert(item);
                            }

                            let elapsed = start_time.elapsed();
                            consumer_timing.lock().push(elapsed);

                            println!(
                                "Consumer {}: Processed {} batches in {:?}",
                                consumer_id, batches_processed, elapsed
                            );
                        })
                    })
                    .collect();

                // Start the test
                println!(
                    "Starting stress test with {} producers and {} consumers",
                    PRODUCER_COUNT, CONSUMER_COUNT
                );
                println!(
                    "Each producer will generate {} items with batch size {}",
                    ITEMS_PER_PRODUCER, BATCH_SIZE
                );

                let overall_start = Instant::now();
                start_barrier.wait();

                // Wait for all producers to finish
                for handle in producer_handles {
                    handle.join().unwrap();
                }

                println!("All producers finished");

                // Wait for all consumers to finish
                for handle in consumer_handles {
                    handle.join().unwrap();
                }

                let overall_elapsed = overall_start.elapsed();
                println!("All consumers finished");
                println!("Overall test time: {:?}", overall_elapsed);

                // Verify results
                let produced = all_produced_items.lock();
                let consumed = all_consumed_items.lock();

                println!("Items produced: {}", produced.len());
                println!("Items consumed: {}", consumed.len());

                // Verify all produced items were consumed
                assert_eq!(
                    produced.len(),
                    total_expected_items,
                    "Number of produced items doesn't match expected"
                );
                assert_eq!(
                    consumed.len(),
                    total_expected_items,
                    "Number of consumed items doesn't match expected"
                );

                for item in produced.iter() {
                    assert!(
                        consumed.contains(item),
                        "Item {} was produced but not consumed",
                        item
                    );
                }

                // Calculate performance metrics
                let producer_times = producer_times.lock();
                let consumer_times = consumer_times.lock();

                let avg_producer_time = producer_times.iter().map(|d| d.as_millis()).sum::<u128>()
                    / producer_times.len() as u128;

                let avg_consumer_time = consumer_times.iter().map(|d| d.as_millis()).sum::<u128>()
                    / consumer_times.len() as u128;

                let throughput =
                    total_expected_items as f64 / (overall_elapsed.as_millis() as f64 / 1000.0);

                println!("Average producer time: {}ms", avg_producer_time);
                println!("Average consumer time: {}ms", avg_consumer_time);
                println!("Throughput: {:.2} items/second", throughput);
            }
        }
    }
}