etherbird 0.1.0

Supervised asynchronous resources with automatic recovery
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
use crate::{
    AttributeSetter, CreatedHook, Error, FifoQueue, Lifecycle, OperationQueue, PriorityQueue,
    ResourceHandle, ResourceState, RetryPolicy, Supervisor,
};
use std::{
    any::Any,
    collections::BTreeMap,
    future::Future,
    pin::Pin,
    sync::{
        Arc, Mutex,
        atomic::{AtomicBool, AtomicUsize, Ordering},
    },
    time::Duration,
};
use tokio::{
    sync::{oneshot, watch},
    task::JoinSet,
    time::{Instant, sleep},
};

#[derive(Clone, Debug)]
pub struct PoolConfig {
    pub min_size: usize,
    pub max_size: usize,
    pub idle_timeout: Duration,
    /// Default queue selection. Ignored when an explicit queue factory is supplied.
    pub priority_queue: bool,
}
impl Default for PoolConfig {
    fn default() -> Self {
        Self {
            min_size: 1,
            max_size: 1,
            idle_timeout: Duration::from_secs(60),
            priority_queue: false,
        }
    }
}
struct Entry<L: Lifecycle> {
    supervisor: Supervisor<L>,
    leased: AtomicBool,
    idle_since: Mutex<Instant>,
    relay: Mutex<Option<tokio::task::JoinHandle<()>>>,
}
type Job<L> = Box<dyn FnOnce(ResourceLease<L>) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send>;
type Borrowing<L> =
    Pin<Box<dyn Future<Output = Result<ResourceLease<L>, Error<<L as Lifecycle>::Error>>> + Send>>;
/// Opaque queued work with scheduling metadata. Only the pool can execute it.
pub struct QueuedOperation<L: Lifecycle> {
    priority: i32,
    sequence: u64,
    job: Job<L>,
    cancelled: Arc<AtomicBool>,
}
impl<L: Lifecycle> QueuedOperation<L> {
    pub fn priority(&self) -> i32 {
        self.priority
    }
    pub fn sequence(&self) -> u64 {
        self.sequence
    }
    pub fn is_cancelled(&self) -> bool {
        self.cancelled.load(Ordering::Acquire)
    }
}
type Operation<L> = QueuedOperation<L>;
impl<L: Lifecycle> PartialEq for Operation<L> {
    fn eq(&self, other: &Self) -> bool {
        (self.priority, self.sequence) == (other.priority, other.sequence)
    }
}
impl<L: Lifecycle> Eq for Operation<L> {}
impl<L: Lifecycle> PartialOrd for Operation<L> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl<L: Lifecycle> Ord for Operation<L> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        (other.priority, other.sequence).cmp(&(self.priority, self.sequence))
    }
}
struct Contents<L: Lifecycle> {
    entries: Vec<Arc<Entry<L>>>,
    queue: Box<dyn OperationQueue<QueuedOperation<L>>>,
    sequence: u64,
    attributes: BTreeMap<String, AttributeSetter<L::Resource>>,
    values: BTreeMap<String, Arc<dyn Any + Send + Sync>>,
    retiring: usize,
}
struct Shared<L: Lifecycle> {
    config: PoolConfig,
    factory: Box<dyn Fn() -> Supervisor<L> + Send + Sync>,
    contents: Mutex<Contents<L>>,
    changed: watch::Sender<u64>,
    stopping: watch::Sender<bool>,
    done: watch::Sender<bool>,
    waiters: AtomicUsize,
    last_resource: Mutex<Option<Arc<L::Resource>>>,
    on_created: Mutex<Option<CreatedHook<L::Resource, L::Error>>>,
}
impl<L: Lifecycle> Shared<L> {
    fn apply_attribute(
        &self,
        contents: &Contents<L>,
        name: &str,
        setter: &AttributeSetter<L::Resource>,
    ) {
        let last_resource = self.last_resource.lock().unwrap().clone();
        let mut last_updated = false;
        for entry in &contents.entries {
            let mut attributes = entry.supervisor.attributes.lock().unwrap();
            if let Some(resource) = &attributes.last {
                setter(resource);
                last_updated |= last_resource
                    .as_ref()
                    .is_some_and(|last| Arc::ptr_eq(last, resource));
            }
            attributes.setters.insert(name.to_owned(), setter.clone());
        }
        if !last_updated && let Some(resource) = last_resource {
            setter(&resource);
        }
    }
    fn notify(&self) {
        self.changed
            .send_modify(|revision| *revision = revision.wrapping_add(1));
    }
    fn grow(self: &Arc<Self>, contents: &mut Contents<L>) {
        let supervisor = (self.factory)();
        for (name, setter) in &contents.attributes {
            supervisor.set_attribute(name.clone(), setter.clone());
        }
        let weak = Arc::downgrade(self);
        // Track creation before connect; callers can read attributes during connection attempts.
        supervisor.attributes.lock().unwrap().after_created = Some(Arc::new(move |resource| {
            let shared = weak.upgrade();
            Box::pin(async move {
                if let Some(shared) = shared {
                    *shared.last_resource.lock().unwrap() = Some(resource.clone());
                    let hook = shared.on_created.lock().unwrap().clone();
                    if let Some(hook) = hook {
                        hook(resource).await?;
                    }
                }
                Ok(())
            })
        }));
        supervisor.begin();
        let entry = Arc::new(Entry {
            supervisor,
            leased: AtomicBool::new(false),
            idle_since: Mutex::new(Instant::now()),
            relay: Mutex::new(None),
        });
        let mut state = entry.supervisor.subscribe();
        let changed = self.changed.clone();
        let relay = tokio::spawn(async move {
            loop {
                if state.borrow_and_update().state == ResourceState::Stopped {
                    break;
                }
                if state.changed().await.is_err() {
                    break;
                }
                changed.send_modify(|revision| *revision = revision.wrapping_add(1));
            }
        });
        *entry.relay.lock().unwrap() = Some(relay);
        contents.entries.push(entry);
        self.notify();
    }
}
struct PoolOwner {
    stopping: watch::Sender<bool>,
    started: AtomicBool,
}
impl Drop for PoolOwner {
    fn drop(&mut self) {
        self.stopping.send_replace(true);
    }
}

/// Demand-grown pool of independently supervised resources. Minimum counts all entries.
/// Start inside a Tokio runtime. Lower priority numbers run first when priority_queue is enabled.
pub struct Pool<L: Lifecycle> {
    shared: Arc<Shared<L>>,
    owner: Arc<PoolOwner>,
}
impl<L: Lifecycle> Clone for Pool<L> {
    fn clone(&self) -> Self {
        Self {
            shared: self.shared.clone(),
            owner: self.owner.clone(),
        }
    }
}
impl<L: Lifecycle> Pool<L> {
    pub fn start(
        factory: impl Fn() -> Supervisor<L> + Send + Sync + 'static,
        config: PoolConfig,
    ) -> Self {
        let pool = Self::new(factory, config);
        pool.begin();
        pool
    }
    /// Construct a lazy pool. Borrowing or executing work starts it automatically.
    pub fn new(
        factory: impl Fn() -> Supervisor<L> + Send + Sync + 'static,
        config: PoolConfig,
    ) -> Self {
        if config.priority_queue {
            Self::new_with_queue_factory(factory, config, PriorityQueue::default)
        } else {
            Self::new_with_queue_factory(factory, config, FifoQueue::default)
        }
    }
    /// Create one custom operation queue for this pool.
    /// The returned queue must be empty. Queue methods run under the pool lock.
    /// The explicit queue controls ordering regardless of `config.priority_queue`.
    pub fn start_with_queue_factory<Q>(
        factory: impl Fn() -> Supervisor<L> + Send + Sync + 'static,
        config: PoolConfig,
        queue_factory: impl FnOnce() -> Q,
    ) -> Self
    where
        Q: OperationQueue<QueuedOperation<L>> + 'static,
    {
        let pool = Self::new_with_queue_factory(factory, config, queue_factory);
        pool.begin();
        pool
    }
    pub fn new_with_queue_factory<Q>(
        factory: impl Fn() -> Supervisor<L> + Send + Sync + 'static,
        config: PoolConfig,
        queue_factory: impl FnOnce() -> Q,
    ) -> Self
    where
        Q: OperationQueue<QueuedOperation<L>> + 'static,
    {
        assert!(
            config.max_size > 0 && config.min_size <= config.max_size,
            "invalid pool capacity"
        );
        assert!(
            !config.idle_timeout.is_zero(),
            "idle timeout must be positive"
        );
        let queue = queue_factory();
        assert!(queue.is_empty(), "queue factory must return an empty queue");
        let (changed, _) = watch::channel(0);
        let (stopping, _) = watch::channel(false);
        let (done, _) = watch::channel(false);
        let shared = Arc::new(Shared {
            config,
            factory: Box::new(factory),
            contents: Mutex::new(Contents {
                entries: Vec::new(),
                queue: Box::new(queue),
                sequence: 0,
                attributes: BTreeMap::new(),
                values: BTreeMap::new(),
                retiring: 0,
            }),
            changed,
            stopping,
            done,
            waiters: AtomicUsize::new(0),
            last_resource: Mutex::new(None),
            on_created: Mutex::new(None),
        });
        Self {
            owner: Arc::new(PoolOwner {
                stopping: shared.stopping.clone(),
                started: AtomicBool::new(false),
            }),
            shared,
        }
    }
    /// Start once and eagerly establish the configured minimum. Stopped pools stay stopped.
    pub fn begin(&self) {
        if *self.shared.stopping.borrow() || self.owner.started.swap(true, Ordering::AcqRel) {
            return;
        }
        {
            let mut contents = self.shared.contents.lock().unwrap();
            while contents.entries.len() < self.shared.config.min_size {
                self.shared.grow(&mut contents);
            }
        }
        tokio::spawn(run(self.shared.clone()));
    }
    pub fn size(&self) -> usize {
        self.shared.contents.lock().unwrap().entries.len()
    }
    pub fn supervisors(&self) -> Vec<Supervisor<L>> {
        self.shared
            .contents
            .lock()
            .unwrap()
            .entries
            .iter()
            .map(|entry| entry.supervisor.clone())
            .collect()
    }
    pub fn resources(&self) -> Vec<ResourceHandle<L::Resource>> {
        self.supervisors()
            .iter()
            .filter_map(Supervisor::current)
            .collect()
    }
    pub fn with_latest<T>(&self, read: impl FnOnce(&L::Resource) -> T) -> Option<T> {
        let resource = self
            .resources()
            .first()
            .map(|handle| handle.resource().clone())
            .or_else(|| self.shared.last_resource.lock().unwrap().clone());
        resource.as_deref().map(read)
    }
    pub fn subscribe(&self) -> watch::Receiver<u64> {
        self.shared.changed.subscribe()
    }
    pub fn state(&self) -> ResourceState {
        if *self.shared.stopping.borrow() {
            return if *self.shared.done.borrow() {
                ResourceState::Stopped
            } else {
                ResourceState::Stopping
            };
        }
        let states: Vec<_> = self.supervisors().iter().map(Supervisor::state).collect();
        for candidate in [
            ResourceState::Connected,
            ResourceState::Connecting,
            ResourceState::Recovering,
        ] {
            if states.contains(&candidate) {
                return candidate;
            }
        }
        ResourceState::Disconnected
    }
    pub async fn connected(&self) -> Result<(), Error<L::Error>> {
        let mut changed = self.subscribe();
        let mut stopping = self.shared.stopping.subscribe();
        loop {
            if *stopping.borrow() {
                return Err(Error::Stopped);
            }
            if self.is_connected() {
                return Ok(());
            }
            let mut connections = JoinSet::new();
            for supervisor in self.supervisors() {
                if let Some(resource) = supervisor.current() {
                    let lifecycle = supervisor.lifecycle.clone();
                    connections.spawn(async move {
                        lifecycle.wait_connected(&resource.resource).await;
                    });
                }
            }
            if self.is_connected() {
                connections.abort_all();
                while connections.join_next().await.is_some() {}
                return Ok(());
            }
            tokio::select! { _ = crate::shutdown(&mut stopping) => {}, _ = changed.changed() => {}, _ = connections.join_next(), if !connections.is_empty() => {} }
            connections.abort_all();
            while connections.join_next().await.is_some() {}
        }
    }
    pub fn is_connected(&self) -> bool {
        self.supervisors().iter().any(Supervisor::is_connected)
    }
    pub fn stopped(&self) -> watch::Receiver<bool> {
        self.shared.stopping.subscribe()
    }
    pub async fn borrow(&self) -> Result<ResourceLease<L>, Error<L::Error>> {
        self.begin();
        let mut lease = borrow(self.shared.clone(), false).await?;
        lease.owner = Some(self.owner.clone());
        Ok(lease)
    }
    pub async fn recover(
        &self,
        expected: Option<&ResourceHandle<L::Resource>>,
        terminal: bool,
    ) -> Result<(), Error<L::Error>> {
        for supervisor in self.supervisors() {
            if let Some(handle) = expected {
                supervisor.recover(handle, terminal).await?;
            } else if let Some(handle) = supervisor.current() {
                supervisor.recover(&handle, terminal).await?;
            }
        }
        Ok(())
    }
    /// Setters apply immediately to existing resources and before setup on future resources.
    pub fn set_attribute(&self, name: impl Into<String>, setter: AttributeSetter<L::Resource>) {
        let name = name.into();
        let mut contents = self.shared.contents.lock().unwrap();
        self.shared.apply_attribute(&contents, &name, &setter);
        contents.attributes.insert(name, setter);
    }
    pub fn set_value<T: Clone + Send + Sync + 'static>(
        &self,
        name: impl Into<String>,
        value: T,
        apply: impl Fn(&L::Resource, &T) + Send + Sync + 'static,
    ) {
        let name = name.into();
        let value = Arc::new(value);
        let stored = value.clone();
        let setter: AttributeSetter<L::Resource> =
            Arc::new(move |resource| apply(resource, &value));
        let mut contents = self.shared.contents.lock().unwrap();
        self.shared.apply_attribute(&contents, &name, &setter);
        contents.values.insert(name.clone(), stored);
        contents.attributes.insert(name, setter);
    }
    pub fn value<T: Clone + Send + Sync + 'static>(&self, name: &str) -> Option<T> {
        self.shared
            .contents
            .lock()
            .unwrap()
            .values
            .get(name)?
            .downcast_ref::<T>()
            .cloned()
    }
    pub fn set_on_resource_created(&self, hook: CreatedHook<L::Resource, L::Error>) {
        *self.shared.on_created.lock().unwrap() = Some(hook);
    }
    pub async fn execute<F, Fut, T>(&self, operation: F) -> Result<T, Error<L::Error>>
    where
        F: FnOnce(Arc<L::Resource>) -> Fut + Send + 'static,
        Fut: Future<Output = Result<T, L::Error>> + Send + 'static,
        T: Send + 'static,
    {
        self.execute_with_priority(0, operation).await
    }
    pub async fn execute_with_priority<F, Fut, T>(
        &self,
        priority: i32,
        operation: F,
    ) -> Result<T, Error<L::Error>>
    where
        F: FnOnce(Arc<L::Resource>) -> Fut + Send + 'static,
        Fut: Future<Output = Result<T, L::Error>> + Send + 'static,
        T: Send + 'static,
    {
        self.enqueue(
            priority,
            |lease| async move { lease.execute(operation).await },
        )
        .await
    }
    /// Explicitly retry a replay-safe operation while retaining its pool lease.
    /// The deadline includes queueing. Each attempt waits for the leased supervisor
    /// to become ready; queue errors, shutdown, and deadlines are never retried.
    /// See [`Supervisor::execute_with_retry`] for replay and cancellation semantics.
    pub async fn execute_with_retry<F, Fut, T, P>(
        &self,
        policy: RetryPolicy,
        operation: F,
        retry_if: P,
    ) -> Result<T, Error<L::Error>>
    where
        F: FnMut(Arc<L::Resource>) -> Fut + Send + 'static,
        Fut: Future<Output = Result<T, L::Error>> + Send + 'static,
        T: Send + 'static,
        P: FnMut(&L::Error) -> bool + Send + 'static,
    {
        self.execute_with_priority_and_retry(0, policy, operation, retry_if)
            .await
    }
    /// Queue a retryable operation once at `priority`, retaining capacity throughout
    /// recovery. The overall deadline includes its initial wait in the queue.
    pub async fn execute_with_priority_and_retry<F, Fut, T, P>(
        &self,
        priority: i32,
        policy: RetryPolicy,
        operation: F,
        retry_if: P,
    ) -> Result<T, Error<L::Error>>
    where
        F: FnMut(Arc<L::Resource>) -> Fut + Send + 'static,
        Fut: Future<Output = Result<T, L::Error>> + Send + 'static,
        T: Send + 'static,
        P: FnMut(&L::Error) -> bool + Send + 'static,
    {
        if policy.timeout.is_zero() {
            return Err(Error::Timeout);
        }
        let queued = self.enqueue(priority, move |lease| async move {
            lease.execute_with_retry(policy, operation, retry_if).await
        });
        tokio::time::timeout(policy.timeout, queued)
            .await
            .unwrap_or(Err(Error::Timeout))
    }
    async fn enqueue<F, Fut, T>(&self, priority: i32, operation: F) -> Result<T, Error<L::Error>>
    where
        F: FnOnce(ResourceLease<L>) -> Fut + Send + 'static,
        Fut: Future<Output = Result<T, Error<L::Error>>> + Send + 'static,
        T: Send + 'static,
    {
        self.begin();
        let (mut sender, receiver) = oneshot::channel();
        let cancelled = Arc::new(AtomicBool::new(false));
        let cancellation = CancelOnDrop {
            cancelled: cancelled.clone(),
            changed: self.shared.changed.clone(),
        };
        {
            let mut contents = self.shared.contents.lock().unwrap();
            if *self.shared.stopping.borrow() {
                return Err(Error::Stopped);
            }
            let sequence = contents.sequence;
            contents.sequence += 1;
            contents
                .queue
                .push(Operation {
                    priority,
                    sequence,
                    cancelled,
                    job: Box::new(move |lease| {
                        Box::pin(async move {
                            if sender.is_closed() {
                                return;
                            }
                            tokio::select! {
                                biased;
                                _ = sender.closed() => {},
                                result = operation(lease) => { let _ = sender.send(result); }
                            }
                        })
                    }),
                })
                .map_err(Error::Queue)?;
            self.shared.notify();
        }
        let result = receiver.await.unwrap_or(Err(Error::Stopped));
        drop(cancellation);
        result
    }
    /// Cancellation-safe shutdown closes leased resources and cancels queued/running work.
    pub async fn stop(&self) {
        self.shared.stopping.send_replace(true);
        self.shared.notify();
        if !self.owner.started.load(Ordering::Acquire) {
            self.shared.done.send_replace(true);
        }
        let mut done = self.shared.done.subscribe();
        while !*done.borrow_and_update() {
            if done.changed().await.is_err() {
                return;
            }
        }
    }
}
struct CancelOnDrop {
    cancelled: Arc<AtomicBool>,
    changed: watch::Sender<u64>,
}
impl Drop for CancelOnDrop {
    fn drop(&mut self) {
        self.cancelled.store(true, Ordering::Release);
        self.changed.send_modify(|v| *v = v.wrapping_add(1));
    }
}

/// Exclusive reservation of a supervisor; reconnection does not end the lease.
pub struct ResourceLease<L: Lifecycle> {
    entry: Arc<Entry<L>>,
    shared: Arc<Shared<L>>,
    // Public leases keep the pool alive. Dispatcher leases must not own the pool,
    // otherwise dropping all public handles could never cancel an active job.
    owner: Option<Arc<PoolOwner>>,
}
impl<L: Lifecycle> ResourceLease<L> {
    /// Return capacity immediately. Ownership prevents use after release.
    pub fn release(self) {}
    pub fn supervisor(&self) -> &Supervisor<L> {
        &self.entry.supervisor
    }
    pub async fn acquire(&self) -> Result<ResourceHandle<L::Resource>, Error<L::Error>> {
        if *self.shared.stopping.borrow() {
            return Err(Error::Stopped);
        }
        self.entry.supervisor.acquire().await
    }
    pub async fn execute<F, Fut, T>(&self, operation: F) -> Result<T, Error<L::Error>>
    where
        F: FnOnce(Arc<L::Resource>) -> Fut,
        Fut: Future<Output = Result<T, L::Error>>,
    {
        let mut stopping = self.shared.stopping.subscribe();
        tokio::select! { biased; _ = crate::shutdown(&mut stopping) => Err(Error::Stopped), result = self.entry.supervisor.execute(operation) => result }
    }
    /// Retry a replay-safe operation on this reservation, following replacement
    /// generations. The deadline starts here because the lease is already held.
    pub async fn execute_with_retry<F, Fut, T, P>(
        &self,
        policy: RetryPolicy,
        operation: F,
        retry_if: P,
    ) -> Result<T, Error<L::Error>>
    where
        F: FnMut(Arc<L::Resource>) -> Fut,
        Fut: Future<Output = Result<T, L::Error>>,
        P: FnMut(&L::Error) -> bool,
    {
        let mut stopping = self.shared.stopping.subscribe();
        tokio::select! {
            biased;
            _ = crate::shutdown(&mut stopping) => Err(Error::Stopped),
            result = self.entry.supervisor.execute_with_retry(policy, operation, retry_if) => result,
        }
    }
}
impl<L: Lifecycle> Drop for ResourceLease<L> {
    fn drop(&mut self) {
        *self.entry.idle_since.lock().unwrap() = Instant::now();
        self.entry.leased.store(false, Ordering::Release);
        self.shared.notify();
    }
}
async fn borrow<L: Lifecycle>(
    shared: Arc<Shared<L>>,
    queued: bool,
) -> Result<ResourceLease<L>, Error<L::Error>> {
    let _waiter = if queued {
        None
    } else {
        shared.waiters.fetch_add(1, Ordering::AcqRel);
        Some(Waiter(shared.clone()))
    };
    let mut changed = shared.changed.subscribe();
    let mut stopping = shared.stopping.subscribe();
    loop {
        {
            let mut contents = shared.contents.lock().unwrap();
            if *stopping.borrow() {
                return Err(Error::Stopped);
            }
            for entry in &contents.entries {
                if entry.supervisor.state() == ResourceState::Connected
                    && entry
                        .leased
                        .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
                        .is_ok()
                {
                    return Ok(ResourceLease {
                        entry: entry.clone(),
                        shared: shared.clone(),
                        owner: None,
                    });
                }
            }
            let demand = shared.waiters.load(Ordering::Acquire)
                + contents.queue.len()
                + contents
                    .entries
                    .iter()
                    .filter(|entry| entry.leased.load(Ordering::Acquire))
                    .count();
            if contents.entries.len() + contents.retiring < shared.config.max_size.min(demand) {
                shared.grow(&mut contents);
            }
        }
        tokio::select! { biased; _ = crate::shutdown(&mut stopping) => return Err(Error::Stopped), _ = changed.changed() => {} }
    }
}
struct Waiter<L: Lifecycle>(Arc<Shared<L>>);
impl<L: Lifecycle> Drop for Waiter<L> {
    fn drop(&mut self) {
        self.0.waiters.fetch_sub(1, Ordering::AcqRel);
        self.0.notify();
    }
}
async fn run<L: Lifecycle>(shared: Arc<Shared<L>>) {
    let mut stopping = shared.stopping.subscribe();
    let mut changed = shared.changed.subscribe();
    let mut operations = JoinSet::new();
    let mut retiring = JoinSet::new();
    let mut borrowing: Option<Borrowing<L>> = None;
    let mut reaper = Box::pin(sleep(
        shared.config.idle_timeout.min(Duration::from_secs(1)),
    ));
    loop {
        {
            let mut contents = shared.contents.lock().unwrap();
            contents.queue.retain(&mut |item| !item.is_cancelled());
            if contents.queue.is_empty() {
                borrowing = None;
            } else if borrowing.is_none() {
                borrowing = Some(Box::pin(borrow(shared.clone(), true)));
            }
        }
        tokio::select! {
            biased;
            _ = crate::shutdown(&mut stopping) => break,
            result = async { match borrowing.as_mut() { Some(future) => future.await, None => std::future::pending().await } } => {
                borrowing = None;
                if let Ok(lease) = result {
                    // Reconsider priority after capacity becomes available.
                    let item = shared.contents.lock().unwrap().queue.pop();
                    if let Some(item) = item && !item.cancelled.load(Ordering::Acquire) { operations.spawn((item.job)(lease)); }
                }
            }
            _ = changed.changed() => {},
            _ = operations.join_next(), if !operations.is_empty() => {},
            _ = retiring.join_next(), if !retiring.is_empty() => {
                shared.contents.lock().unwrap().retiring -= 1; shared.notify();
            }
            _ = &mut reaper => {
                let mut contents = shared.contents.lock().unwrap();
                if contents.queue.is_empty() && borrowing.is_none() && shared.waiters.load(Ordering::Acquire) == 0 {
                    let mut index = 0;
                    while index < contents.entries.len() && contents.entries.len() > shared.config.min_size {
                        let entry = &contents.entries[index];
                        if !entry.leased.load(Ordering::Acquire) && entry.idle_since.lock().unwrap().elapsed() >= shared.config.idle_timeout {
                            let entry = contents.entries.remove(index); contents.retiring += 1;
                            retiring.spawn(stop_entry(entry)); shared.notify();
                        } else { index += 1; }
                    }
                }
                reaper.as_mut().reset(Instant::now() + shared.config.idle_timeout.min(Duration::from_secs(1)));
            }
        }
    }
    drop(borrowing);
    operations.abort_all();
    while operations.join_next().await.is_some() {}
    let entries = {
        let mut contents = shared.contents.lock().unwrap();
        contents.queue.clear();
        contents.entries.clone()
    };
    for entry in entries {
        retiring.spawn(stop_entry(entry));
    }
    while retiring.join_next().await.is_some() {}
    shared.done.send_replace(true);
    shared.notify();
}
async fn stop_entry<L: Lifecycle>(entry: Arc<Entry<L>>) {
    entry.supervisor.stop().await;
    let relay = entry.relay.lock().unwrap().take();
    if let Some(relay) = relay {
        let _ = relay.await;
    }
}

/// Stable proxy backing typed client wrappers. Attributes are replayed through named setters.
pub struct ManagedResourceProxy<L: Lifecycle> {
    pub pool: Pool<L>,
}
impl<L: Lifecycle> Clone for ManagedResourceProxy<L> {
    fn clone(&self) -> Self {
        Self {
            pool: self.pool.clone(),
        }
    }
}
impl<L: Lifecycle> ManagedResourceProxy<L> {
    pub fn new(pool: Pool<L>) -> Self {
        Self { pool }
    }
    pub async fn connected(&self) -> Result<(), Error<L::Error>> {
        self.pool.connected().await
    }
    pub fn is_connected(&self) -> bool {
        self.pool.is_connected()
    }
    pub fn set_attribute(&self, name: impl Into<String>, setter: AttributeSetter<L::Resource>) {
        self.pool.set_attribute(name, setter);
    }
    pub fn attribute<T>(&self, read: impl FnOnce(&L::Resource) -> T) -> Option<T> {
        self.pool.with_latest(read)
    }
    pub fn set_value<T: Clone + Send + Sync + 'static>(
        &self,
        name: impl Into<String>,
        value: T,
        apply: impl Fn(&L::Resource, &T) + Send + Sync + 'static,
    ) {
        self.pool.set_value(name, value, apply);
    }
    pub fn value<T: Clone + Send + Sync + 'static>(&self, name: &str) -> Option<T> {
        self.pool.value(name)
    }
    pub async fn stop(&self) {
        self.pool.stop().await;
    }
}

/// Generate a typed proxy with ordinary async client methods returning owned results.
/// Methods must return the lifecycle error type. Borrowed/streaming results need a manual wrapper.
#[macro_export]
macro_rules! managed_client {
    ($visibility:vis struct $name:ident for $lifecycle:ty { $($methods:tt)* }) => {
        #[derive(Clone)]
        $visibility struct $name { pub managed: $crate::ManagedResourceProxy<$lifecycle> }
        impl $name {
            pub fn new(pool: $crate::Pool<$lifecycle>) -> Self { Self { managed: $crate::ManagedResourceProxy::new(pool) } }
            $crate::managed_client_methods! { $lifecycle; $($methods)* }
        }
    };
}
#[doc(hidden)]
#[macro_export]
macro_rules! managed_client_methods {
    ($lifecycle:ty;) => {};
    ($lifecycle:ty; async fn $method:ident($($argument:ident: $ty:ty),* $(,)?) -> $output:ty; $($rest:tt)*) => {
        pub async fn $method(&self, $($argument: $ty),*) -> Result<$output, $crate::Error<<$lifecycle as $crate::Lifecycle>::Error>> {
            self.managed.pool.execute(move |resource| async move { resource.$method($($argument),*).await }).await
        }
        $crate::managed_client_methods! { $lifecycle; $($rest)* }
    };
    ($lifecycle:ty; fn $method:ident($($argument:ident: $ty:ty),* $(,)?) -> $output:ty; $($rest:tt)*) => {
        pub async fn $method(&self, $($argument: $ty),*) -> Result<$output, $crate::Error<<$lifecycle as $crate::Lifecycle>::Error>> {
            self.managed.pool.execute(move |resource| async move { resource.$method($($argument),*) }).await
        }
        $crate::managed_client_methods! { $lifecycle; $($rest)* }
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    struct Hooks;
    #[crate::async_trait]
    impl Lifecycle for Hooks {
        type Resource = ();
        type Error = std::convert::Infallible;
        async fn create(&self) -> Result<(), Self::Error> {
            Ok(())
        }
        async fn connect(&self, _: &()) -> Result<(), Self::Error> {
            Ok(())
        }
        async fn disconnect(&self, _: &()) -> Result<(), Self::Error> {
            Ok(())
        }
    }
    #[tokio::test]
    async fn shutdown_joins_notification_relays_even_when_supervisors_are_retained() {
        let pool = Pool::start(
            || Supervisor::new(Hooks, crate::Config::default()),
            PoolConfig::default(),
        );
        pool.connected().await.unwrap();
        let retained = pool.supervisors();
        pool.stop().await;
        assert!(
            pool.shared
                .contents
                .lock()
                .unwrap()
                .entries
                .iter()
                .all(|entry| entry.relay.lock().unwrap().is_none())
        );
        assert!(
            retained
                .iter()
                .all(|supervisor| supervisor.state() == ResourceState::Stopped)
        );
    }
    #[tokio::test]
    async fn notification_relays_are_joined_during_idle_retirement() {
        let pool = Pool::start(
            || Supervisor::new(Hooks, crate::Config::default()),
            PoolConfig {
                min_size: 0,
                idle_timeout: Duration::from_millis(5),
                ..PoolConfig::default()
            },
        );
        let lease = pool.borrow().await.unwrap();
        let entry = lease.entry.clone();
        drop(lease);
        tokio::time::timeout(Duration::from_secs(1), async {
            loop {
                if entry.relay.lock().unwrap().is_none() {
                    break;
                }
                sleep(Duration::from_millis(1)).await;
            }
        })
        .await
        .unwrap();
        pool.stop().await;
    }

    struct PendingHooks;
    #[crate::async_trait]
    impl Lifecycle for PendingHooks {
        type Resource = ();
        type Error = std::convert::Infallible;
        async fn create(&self) -> Result<(), Self::Error> {
            Ok(())
        }
        async fn connect(&self, _: &()) -> Result<(), Self::Error> {
            std::future::pending().await
        }
        async fn disconnect(&self, _: &()) -> Result<(), Self::Error> {
            Ok(())
        }
        fn watch_disconnect(&self, _: Arc<()>) -> Option<crate::LifecycleFuture<Self::Error>> {
            Some(Box::pin(std::future::pending()))
        }
    }
    #[tokio::test]
    async fn shutdown_joins_relays_while_connecting_with_watchdog_configured() {
        let pool = Pool::start(
            || Supervisor::new(PendingHooks, crate::Config::default()),
            PoolConfig::default(),
        );
        tokio::time::timeout(Duration::from_secs(1), async {
            while pool.state() != ResourceState::Connecting {
                tokio::task::yield_now().await;
            }
        })
        .await
        .unwrap();
        let entries = pool.shared.contents.lock().unwrap().entries.clone();
        assert_eq!(entries.len(), 1);
        assert!(entries[0].relay.lock().unwrap().is_some());
        tokio::time::timeout(Duration::from_secs(1), pool.stop())
            .await
            .unwrap();
        assert!(entries[0].relay.lock().unwrap().is_none());
        assert_eq!(entries[0].supervisor.state(), ResourceState::Stopped);
        assert!(*pool.shared.done.borrow());
    }
}