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
//! The pool handle, the acquire path, and the per-shard reaper.
use std::{
any::Any,
cell::RefCell,
collections::HashMap,
rc::{Rc, Weak},
sync::{
Arc,
atomic::{AtomicBool, AtomicU64, Ordering::Relaxed},
},
time::Instant,
};
use crate::{
config::Config,
error::Error,
exchange::{Exchange, NoExchange, Parked, Unparked},
guard::Pooled,
manage::Manage,
metrics::{Counters, Metrics},
shard::Shard,
slot::Slot,
};
static NEXT_POOL_ID: AtomicU64 = AtomicU64::new(0);
/// Holds a shard's capacity claim across an `await`, giving it back if the
/// `acquire` future is cancelled.
///
/// `acquire` claims budget with `Shard::try_reserve` *before* awaiting
/// `Manage::connect`, `Exchange::unpark` or `Manage::recycle`, and `acquire`
/// is itself cancellable - by the acquire timeout, or by the caller's own
/// `select!`. Without this guard, a cancellation between the claim and the end
/// of the await would leak that capacity permanently, and a shard would
/// eventually sit at `max_size` holding no connections at all.
struct Reserved<'a, M: Manage> {
shard: &'a Rc<Shard<M>>,
counters: &'a Counters,
/// True when a real connection is riding along, counted in `live`.
holds_conn: bool,
armed: bool,
}
impl<'a, M: Manage> Reserved<'a, M> {
/// Budget claimed, no connection behind it yet.
fn empty(shard: &'a Rc<Shard<M>>, counters: &'a Counters) -> Self {
Self {
shard,
counters,
holds_conn: false,
armed: true,
}
}
/// Budget claimed and a live connection is out of the free list.
fn holding(shard: &'a Rc<Shard<M>>, counters: &'a Counters) -> Self {
Self {
shard,
counters,
holds_conn: true,
armed: true,
}
}
/// The await completed; ownership has passed on.
fn disarm(mut self) {
self.armed = false;
}
}
impl<M: Manage> Drop for Reserved<'_, M> {
fn drop(&mut self) {
if !self.armed {
return;
}
if self.holds_conn {
// The connection is dropped by the cancelled future without a
// graceful `Manage::disconnect`; there is no way to await one here.
Counters::dec(&self.counters.live);
Counters::inc(&self.counters.closed);
}
self.shard.release();
}
}
thread_local! {
/// Every pool's shard for *this* thread, keyed by pool id.
///
/// This is what makes `Pool` `Send + Sync` while `M::Connection` is not:
/// the `Arc<Inner>` holds no connections at all. They live here, and a
/// value in a thread-local is by construction only reachable from its own
/// thread. At thread exit the map drops, closing that thread's connections
/// on the thread whose compio driver owns them.
static SHARDS: RefCell<HashMap<u64, Box<dyn Any>>> = RefCell::new(HashMap::new());
}
struct Inner<M: Manage, X: Exchange<M>> {
id: u64,
manager: Arc<M>,
config: Config,
exchange: X,
counters: Arc<Counters>,
generation: AtomicU64,
closed: AtomicBool,
}
/// A sharded connection pool for `compio`.
///
/// Cheap to clone and `Send + Sync`: clone it onto every compio thread. Each
/// thread transparently gets its own shard, so the acquire fast path touches no
/// atomics and no locks.
///
/// The `X` parameter selects the cross-thread strategy. The default,
/// [`NoExchange`], keeps threads fully independent. Swap in a
/// [`Reservoir`](crate::Reservoir) — which requires [`Detach`](crate::Detach) —
/// to let idle connections migrate to whichever thread needs them.
pub struct Pool<M: Manage, X: Exchange<M> = NoExchange> {
inner: Arc<Inner<M, X>>,
}
impl<M: Manage, X: Exchange<M>> Clone for Pool<M, X> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<M: Manage> Pool<M> {
/// Builds a pool with no cross-thread sharing.
pub fn new(manager: M, config: Config) -> Self {
Self::with_exchange(manager, config, NoExchange)
}
/// Starts a [`Builder`].
pub fn builder(manager: M) -> Builder<M, NoExchange> {
Builder {
manager,
config: Config::default(),
exchange: NoExchange,
}
}
}
impl<M: Manage, X: Exchange<M>> Pool<M, X> {
/// Builds a pool with an explicit cross-thread [`Exchange`].
pub fn with_exchange(manager: M, config: Config, exchange: X) -> Self {
Self {
inner: Arc::new(Inner {
id: NEXT_POOL_ID.fetch_add(1, Relaxed),
manager: Arc::new(manager),
config,
exchange,
counters: Arc::new(Counters::default()),
generation: AtomicU64::new(0),
closed: AtomicBool::new(false),
}),
}
}
/// The manager, for callers that need to reach configuration through it.
pub fn manager(&self) -> &M {
self.inner.manager.as_ref()
}
/// The configuration this pool was built with.
pub fn config(&self) -> &Config {
&self.inner.config
}
/// Connections owned by *this thread's* shard, idle plus checked out.
pub fn local_size(&self) -> usize {
self.shard().size()
}
/// Idle connections sitting in *this thread's* shard.
pub fn local_idle(&self) -> usize {
self.shard().idle_len()
}
/// A snapshot of pool activity.
pub fn metrics(&self) -> Metrics {
self.inner.counters.snapshot(self.inner.exchange.parked())
}
/// Retires every connection created before this call.
///
/// Live checkouts keep working and are closed when returned. Use after a
/// credential rotation or a failover, where existing sockets point at the
/// wrong place but the pool itself is still wanted.
pub fn invalidate(&self) {
self.inner.generation.fetch_add(1, Relaxed);
self.inner.exchange.clear();
}
/// Closes the pool.
///
/// Subsequent [`acquire`](Pool::acquire) calls fail with
/// [`Error::Closed`]. This thread's idle connections are closed
/// immediately; other threads' are closed when they next touch the pool or
/// when their thread exits.
pub fn close(&self) {
self.inner.closed.store(true, Relaxed);
self.inner.exchange.clear();
let shard = self.shard();
for slot in shard.drain_all() {
self.destroy(&shard, slot);
}
}
/// Whether [`close`](Pool::close) has been called.
pub fn is_closed(&self) -> bool {
self.inner.closed.load(Relaxed)
}
/// Opens connections on the current thread up to `min_idle`.
///
/// Call this once per compio thread at startup so the first real request
/// does not pay for a handshake.
pub async fn warm(&self) -> Result<(), Error<M::Error>> {
let shard = self.shard();
let target = self.inner.config.effective_min_idle();
while shard.idle_len() < target {
if !shard.try_reserve(self.inner.config.max_size) {
break;
}
match self.inner.manager.connect().await {
Ok(conn) => {
Counters::inc(&self.inner.counters.created);
Counters::inc(&self.inner.counters.live);
let generation = self.inner.generation.load(Relaxed);
shard.push_idle(Slot::new(conn, generation));
}
Err(e) => {
shard.release();
return Err(Error::Backend(e));
}
}
}
Ok(())
}
/// Checks out a connection, waiting if the shard is at `max_size`.
///
/// Must be called on a compio runtime thread. Honours
/// [`Config::acquire_timeout`].
pub async fn acquire(&self) -> Result<Pooled<M, X>, Error<M::Error>> {
match self.inner.config.acquire_timeout {
Some(limit) => match compio::time::timeout(limit, self.acquire_inner()).await {
Ok(result) => result,
Err(_elapsed) => {
Counters::inc(&self.inner.counters.timeouts);
Err(Error::Timeout)
}
},
None => self.acquire_inner().await,
}
}
async fn acquire_inner(&self) -> Result<Pooled<M, X>, Error<M::Error>> {
let shard = self.shard();
loop {
if self.inner.closed.load(Relaxed) {
return Err(Error::Closed);
}
let generation = self.inner.generation.load(Relaxed);
// 1. Warmest idle connection on this thread. No atomics, no lock.
while let Some(slot) = shard.pop_idle() {
if slot.is_expired(&self.inner.config, generation) {
self.destroy(&shard, slot);
continue;
}
let Slot { mut conn, meta } = slot;
// The connection is now out of the free list but not yet in a
// guard: if this await is cancelled, `reserved` accounts for it.
let reserved = Reserved::holding(&shard, &self.inner.counters);
let recycled = self.inner.manager.recycle(&mut conn, &meta).await;
reserved.disarm();
match recycled {
Ok(()) => {
Counters::inc(&self.inner.counters.acquires);
return Ok(Pooled::new(Slot { conn, meta }, shard, self.clone()));
}
Err(_) => {
Counters::inc(&self.inner.counters.recycle_failures);
self.destroy_conn(&shard, conn);
}
}
}
// 2. Room for one more on this thread? Claim the budget first, so
// that unpark and connect are both covered by one reservation.
if shard.try_reserve(self.inner.config.max_size) {
// Both awaits below are cancellable; `reserved` hands the
// budget back if either is dropped part-way through.
let reserved = Reserved::empty(&shard, &self.inner.counters);
let unparked = self.inner.exchange.unpark().await;
reserved.disarm();
match unparked {
// Someone else's idle socket, re-wrapped in this thread's
// runtime. Cheaper than a handshake, so it is tried first.
Unparked::Claimed(conn, meta) => {
Counters::inc(&self.inner.counters.unparked);
Counters::inc(&self.inner.counters.live);
Counters::inc(&self.inner.counters.acquires);
return Ok(Pooled::new(Slot { conn, meta }, shard, self.clone()));
}
// It was popped but could not be reattached, so it is gone.
// Parking already decremented `live`; balance `created`
// here and fall through to dialling a replacement.
Unparked::Lost => Counters::inc(&self.inner.counters.closed),
Unparked::Empty => {}
}
let reserved = Reserved::empty(&shard, &self.inner.counters);
let connected = self.inner.manager.connect().await;
reserved.disarm();
return match connected {
Ok(conn) => {
Counters::inc(&self.inner.counters.created);
Counters::inc(&self.inner.counters.live);
Counters::inc(&self.inner.counters.acquires);
Ok(Pooled::new(
Slot::new(conn, generation),
shard,
self.clone(),
))
}
Err(e) => {
shard.release();
Err(Error::Backend(e))
}
};
}
// 3. At capacity. Wait for one of *this thread's* checkouts to come
// back; they always will, so this cannot deadlock.
Counters::inc(&self.inner.counters.waits);
shard.wait().await;
}
}
/// Return path for [`Pooled::drop`].
pub(crate) fn release(
&self,
shard: &Rc<Shard<M>>,
mut slot: Slot<M::Connection>,
poisoned: bool,
) {
slot.meta.uses += 1;
slot.meta.last_used = Instant::now();
if poisoned {
Counters::inc(&self.inner.counters.poisoned);
return self.destroy(shard, slot);
}
if self.inner.closed.load(Relaxed) {
return self.destroy(shard, slot);
}
let generation = self.inner.generation.load(Relaxed);
if slot.is_expired(&self.inner.config, generation) {
return self.destroy(shard, slot);
}
// Surplus idle connections are offered to other threads rather than
// held here until they age out. Only surplus: keep `min_idle` local,
// and never give one away while someone on this thread is waiting.
if !shard.has_waiters() && shard.idle_len() >= self.inner.config.effective_min_idle() {
let Slot { conn, meta } = slot;
match self.inner.exchange.park(conn, meta) {
Parked::Accepted => {
// The reservoir took it; it is no longer this shard's.
Counters::dec(&self.inner.counters.live);
shard.release();
return;
}
// `detach` found an operation still in flight and dropped it
// rather than hand a busy handle to another driver.
Parked::Destroyed => {
Counters::dec(&self.inner.counters.live);
Counters::inc(&self.inner.counters.closed);
shard.release();
return;
}
Parked::Refused(conn, meta) => slot = Slot { conn, meta },
}
}
shard.push_idle(slot);
}
/// Drops a connection out of the pool without closing it, for
/// [`Pooled::take`].
pub(crate) fn forget(&self, shard: &Rc<Shard<M>>) {
Counters::dec(&self.inner.counters.live);
shard.release();
}
fn destroy(&self, shard: &Rc<Shard<M>>, slot: Slot<M::Connection>) {
self.destroy_conn(shard, slot.conn);
}
fn destroy_conn(&self, shard: &Rc<Shard<M>>, conn: M::Connection) {
self.inner.manager.disconnect(conn);
Counters::dec(&self.inner.counters.live);
Counters::inc(&self.inner.counters.closed);
shard.release();
}
/// This thread's shard, creating it (and its reaper) on first touch.
fn shard(&self) -> Rc<Shard<M>> {
let id = self.inner.id;
let (shard, created) = SHARDS.with(|shards| {
let mut shards = shards.borrow_mut();
match shards.get(&id) {
Some(any) => (
any.downcast_ref::<Rc<Shard<M>>>()
.expect("pool id uniquely determines shard type")
.clone(),
false,
),
None => {
// Cloned here, not above: these are `Arc`s shared by every
// thread, and bumping their refcounts on the hit path would
// put two atomic RMWs on a contended cache line into every
// single checkout.
let shard = Rc::new(Shard::<M>::new(
self.inner.counters.clone(),
self.inner.manager.clone(),
));
shards.insert(id, Box::new(shard.clone()));
(shard, true)
}
}
});
// Spawn outside the `with` closure: the thread-local is no longer
// borrowed, so the reaper is free to reach for its own shard.
if created && self.inner.config.needs_reaper() {
self.spawn_reaper(&shard);
}
shard
}
fn spawn_reaper(&self, shard: &Rc<Shard<M>>) {
// Outside a compio runtime there is nothing to spawn onto. The pool
// still works; expired connections are then caught on acquire instead.
if compio::runtime::Runtime::try_with_current(|_| ()).is_err() {
return;
}
let pool = self.clone();
let shard = Rc::downgrade(shard);
compio::runtime::spawn(async move { pool.reap_loop(shard).await }).detach();
}
/// Enforces idle/lifetime limits and refills `min_idle` on one thread.
///
/// Holds only a `Weak` to the shard, so it stops on its own once the
/// thread's shard is gone.
async fn reap_loop(self, shard: Weak<Shard<M>>) {
let interval = self.inner.config.reap_interval;
loop {
compio::time::sleep(interval).await;
let Some(shard) = shard.upgrade() else { return };
if self.inner.closed.load(Relaxed) {
for slot in shard.drain_all() {
self.destroy(&shard, slot);
}
return;
}
let generation = self.inner.generation.load(Relaxed);
for slot in shard.drain_expired(&self.inner.config, generation) {
self.destroy(&shard, slot);
}
let target = self.inner.config.effective_min_idle();
while shard.idle_len() < target && !shard.has_waiters() {
if !shard.try_reserve(self.inner.config.max_size) {
break;
}
match self.inner.manager.connect().await {
Ok(conn) => {
Counters::inc(&self.inner.counters.created);
Counters::inc(&self.inner.counters.live);
shard.push_idle(Slot::new(conn, generation));
}
Err(_) => {
shard.release();
break;
}
}
}
}
}
}
impl<M: Manage, X: Exchange<M>> std::fmt::Debug for Pool<M, X> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Pool")
.field("id", &self.inner.id)
.field("closed", &self.inner.closed.load(Relaxed))
.field("metrics", &self.metrics())
.finish_non_exhaustive()
}
}
/// Fluent constructor for [`Pool`].
pub struct Builder<M: Manage, X: Exchange<M>> {
manager: M,
config: Config,
exchange: X,
}
impl<M: Manage, X: Exchange<M>> Builder<M, X> {
/// Replaces the whole configuration.
pub fn config(mut self, config: Config) -> Self {
self.config = config;
self
}
/// Maximum live connections per runtime thread.
pub fn max_size(mut self, n: usize) -> Self {
self.config = self.config.clone().max_size(n);
self
}
/// Idle connections each shard keeps warm.
pub fn min_idle(mut self, n: usize) -> Self {
self.config = self.config.clone().min_idle(n);
self
}
/// How long `acquire` waits before giving up.
pub fn acquire_timeout(mut self, t: impl Into<Option<std::time::Duration>>) -> Self {
self.config = self.config.clone().acquire_timeout(t);
self
}
/// Hard age cap on a connection.
pub fn max_lifetime(mut self, t: impl Into<Option<std::time::Duration>>) -> Self {
self.config = self.config.clone().max_lifetime(t);
self
}
/// How long a connection may sit idle before being reaped.
pub fn idle_timeout(mut self, t: impl Into<Option<std::time::Duration>>) -> Self {
self.config = self.config.clone().idle_timeout(t);
self
}
/// Retire a connection after this many checkouts.
pub fn max_uses(mut self, n: impl Into<Option<u64>>) -> Self {
self.config = self.config.clone().max_uses(n);
self
}
/// Installs a cross-thread [`Exchange`], changing the pool's type.
pub fn exchange<Y: Exchange<M>>(self, exchange: Y) -> Builder<M, Y> {
Builder {
manager: self.manager,
config: self.config,
exchange,
}
}
/// Builds the pool.
pub fn build(self) -> Pool<M, X> {
Pool::with_exchange(self.manager, self.config, self.exchange)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_support::TestManager;
fn untimed() -> Config {
Config::new().max_lifetime(None).idle_timeout(None)
}
fn pool() -> Pool<TestManager> {
Pool::new(TestManager::new(), untimed())
}
// ---- Reserved: the cancellation-safety guard ----------------------------
//
// `acquire` claims shard budget before awaiting, and `acquire` is itself
// cancellable. These four cases are the whole contract.
/// A guard for a claim that has already been converted into a real checkout
/// must not touch anything when it goes out of scope.
#[test]
fn a_disarmed_empty_reservation_returns_nothing() {
let p = pool();
let shard = p.shard();
assert!(shard.try_reserve(4));
Reserved::empty(&shard, &p.inner.counters).disarm();
assert_eq!(shard.size(), 1, "the budget stayed claimed");
assert_eq!(p.metrics().closed, 0);
}
/// The point of the guard: a cancelled dial hands its budget back, or the
/// shard silently loses capacity until it sits at `max_size` holding nothing.
#[test]
fn a_dropped_empty_reservation_returns_the_budget() {
let p = pool();
let shard = p.shard();
assert!(shard.try_reserve(4));
drop(Reserved::empty(&shard, &p.inner.counters));
assert_eq!(shard.size(), 0);
let m = p.metrics();
assert_eq!(m.closed, 0, "there was no connection to close");
assert_eq!(m.live, 0);
}
#[test]
fn a_disarmed_holding_reservation_leaves_the_connection_counted() {
let p = pool();
let shard = p.shard();
assert!(shard.try_reserve(4));
Counters::inc(&p.inner.counters.live);
Reserved::holding(&shard, &p.inner.counters).disarm();
let m = p.metrics();
assert_eq!(m.live, 1, "the connection is still ours");
assert_eq!(m.closed, 0);
assert_eq!(shard.size(), 1);
}
/// A connection out of the free list but not yet in a guard is dropped by
/// the cancelled future, with no chance to await `disconnect`. It still has
/// to be accounted for.
#[test]
fn a_dropped_holding_reservation_counts_the_connection_closed() {
let p = pool();
let shard = p.shard();
assert!(shard.try_reserve(4));
Counters::inc(&p.inner.counters.live);
drop(Reserved::holding(&shard, &p.inner.counters));
let m = p.metrics();
assert_eq!(m.live, 0);
assert_eq!(m.closed, 1);
assert_eq!(shard.size(), 0, "and the budget comes back too");
}
// ---- The thread-local shard map -----------------------------------------
#[test]
fn a_shard_is_created_once_per_thread_and_then_reused() {
let p = pool();
let first = p.shard();
let second = p.shard();
assert!(
Rc::ptr_eq(&first, &second),
"the hit path must not rebuild the shard"
);
// A clone of the handle is the same pool, so the same shard.
assert!(Rc::ptr_eq(&first, &p.clone().shard()));
}
#[test]
fn two_pools_of_the_same_type_do_not_share_a_shard() {
let a = pool();
let b = pool();
assert!(
!Rc::ptr_eq(&a.shard(), &b.shard()),
"shards are keyed by pool id, not by connection type"
);
}
/// Outside a compio runtime there is nothing to spawn the reaper onto. The
/// pool must still be usable; expiry is then enforced on acquire instead.
#[test]
fn a_pool_with_a_reaper_config_works_without_a_runtime() {
let p = Pool::new(TestManager::new(), Config::new().min_idle(2));
assert!(p.config().needs_reaper());
assert_eq!(p.local_size(), 0);
assert_eq!(p.local_idle(), 0);
}
#[test]
fn a_pool_without_a_reaper_config_skips_the_spawn_entirely() {
let p = pool();
assert!(!p.config().needs_reaper());
assert_eq!(p.local_size(), 0);
}
// ---- Handle surface ------------------------------------------------------
#[test]
fn debug_reports_identity_state_and_metrics() {
let p = pool();
let s = format!("{p:?}");
assert!(s.contains("closed: false"), "got {s}");
assert!(s.contains("metrics"), "got {s}");
p.close();
assert!(format!("{p:?}").contains("closed: true"));
}
#[test]
fn pool_ids_are_unique() {
let a = pool();
let b = pool();
assert_ne!(a.inner.id, b.inner.id);
}
#[test]
fn destroy_closes_through_the_manager_and_frees_budget() {
let manager = TestManager::new();
let counts = manager.counts();
let p = Pool::new(manager, untimed());
let shard = p.shard();
assert!(shard.try_reserve(4));
Counters::inc(&p.inner.counters.live);
p.destroy(&shard, Slot::new(crate::test_support::TestConn::new(0), 0));
assert_eq!(counts.disconnected(), 1);
assert_eq!(shard.size(), 0);
let m = p.metrics();
assert_eq!((m.live, m.closed), (0, 1));
}
#[test]
fn forget_frees_budget_without_closing_anything() {
let manager = TestManager::new();
let counts = manager.counts();
let p = Pool::new(manager, untimed());
let shard = p.shard();
assert!(shard.try_reserve(4));
Counters::inc(&p.inner.counters.live);
p.forget(&shard);
assert_eq!(counts.disconnected(), 0, "the caller owns it now");
assert_eq!(shard.size(), 0, "but the shard may open a replacement");
let m = p.metrics();
assert_eq!((m.live, m.closed), (0, 0));
}
}