etherbird 0.4.2

Stable async clients with connection recovery and session restoration
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
mod dispatch;
mod job;
use crate::{
    AttributeSetter, ConfigError, CreatedHook, Error, FifoQueue, Lifecycle, OperationQueue,
    PriorityQueue, ResourceHandle, ResourceState, RetryPolicy, Supervisor,
};
use dispatch::{all_failed, borrow, reserve_ready, run};
use job::{CancelOnDrop, run_job};
use std::{
    any::Any,
    collections::BTreeMap,
    future::Future,
    pin::Pin,
    sync::{
        Arc, Mutex,
        atomic::{AtomicBool, AtomicUsize, Ordering},
    },
    time::Duration,
};
use tokio::{
    sync::{Notify, 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,
        }
    }
}
impl PoolConfig {
    pub fn validate(&self) -> Result<(), ConfigError> {
        if self.max_size == 0 {
            return Err(ConfigError {
                field: "max_size",
                reason: "must be positive",
            });
        }
        if self.min_size > self.max_size {
            return Err(ConfigError {
                field: "min_size",
                reason: "must not exceed max_size",
            });
        }
        if self.idle_timeout.is_zero() {
            return Err(ConfigError {
                field: "idle_timeout",
                reason: "must be positive",
            });
        }
        Ok(())
    }
}
struct Entry<L: Lifecycle> {
    supervisor: Supervisor<L>,
    leased: AtomicBool,
    idle_since: Mutex<Instant>,
    relay: Mutex<Option<tokio::task::JoinHandle<()>>>,
}
type Admission<L> = Result<ResourceLease<L>, Error<<L as Lifecycle>::Error>>;
type Job<L> = Box<dyn FnOnce(Admission<L>) -> Pin<Box<dyn Future<Output = ()> + Send>> + 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>>,
    operations: Mutex<JoinSet<()>>,
    fast_admission: bool,
    changed: watch::Sender<u64>,
    dispatcher: Arc<Notify>,
    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.dispatcher.notify_one();
        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 {
                    {
                        let mut last = shared.last_resource.lock().unwrap();
                        // Timed-out created callbacks may finish after pool stop.
                        if *shared.stopping.borrow() {
                            return Ok(());
                        }
                        *last = 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 state = entry.supervisor.subscribe();
        let changed = self.changed.clone();
        let dispatcher = self.dispatcher.clone();
        let relay = tokio::spawn(relay_state(state, changed, dispatcher));
        *entry.relay.lock().unwrap() = Some(relay);
        contents.entries.push(entry);
        self.notify();
    }
}
struct PoolOwner {
    stopping: watch::Sender<bool>,
    started: AtomicBool,
}
async fn relay_state<R: Send + Sync + 'static>(
    mut state: watch::Receiver<crate::ResourceStatus<R>>,
    changed: watch::Sender<u64>,
    dispatcher: Arc<Notify>,
) {
    loop {
        // Observe before notifying. Ready may have arrived before our first
        // poll; forwarding only subsequent changes would strand pool waiters.
        let stopped = state.borrow_and_update().state == ResourceState::Stopped;
        changed.send_modify(|revision| *revision = revision.wrapping_add(1));
        dispatcher.notify_one();
        if stopped || state.changed().await.is_err() {
            break;
        }
    }
}
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> {
    /// Validate before constructing channels, queues or invoking the factory.
    /// Each supervisor factory remains responsible for its lifecycle Config.
    pub fn try_new(
        factory: impl Fn() -> Supervisor<L> + Send + Sync + 'static,
        config: PoolConfig,
    ) -> Result<Self, ConfigError> {
        config.validate()?;
        Ok(Self::new(factory, config))
    }
    pub fn try_start(
        factory: impl Fn() -> Supervisor<L> + Send + Sync + 'static,
        config: PoolConfig,
    ) -> Result<Self, ConfigError> {
        let pool = Self::try_new(factory, config)?;
        pool.begin();
        Ok(pool)
    }
    pub fn try_new_with_queue_factory<Q>(
        factory: impl Fn() -> Supervisor<L> + Send + Sync + 'static,
        config: PoolConfig,
        queue_factory: impl FnOnce() -> Q,
    ) -> Result<Self, ConfigError>
    where
        Q: OperationQueue<QueuedOperation<L>> + 'static,
    {
        config.validate()?;
        let queue = queue_factory();
        if !queue.is_empty() {
            return Err(ConfigError {
                field: "queue_factory",
                reason: "must return an empty queue",
            });
        }
        Ok(Self::with_queue(factory, config, move || queue, false))
    }
    pub fn try_start_with_queue_factory<Q>(
        factory: impl Fn() -> Supervisor<L> + Send + Sync + 'static,
        config: PoolConfig,
        queue_factory: impl FnOnce() -> Q,
    ) -> Result<Self, ConfigError>
    where
        Q: OperationQueue<QueuedOperation<L>> + 'static,
    {
        let pool = Self::try_new_with_queue_factory(factory, config, queue_factory)?;
        pool.begin();
        Ok(pool)
    }
    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::with_queue(factory, config, PriorityQueue::default, true)
        } else {
            Self::with_queue(factory, config, FifoQueue::default, true)
        }
    }
    /// 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,
    {
        Self::with_queue(factory, config, queue_factory, false)
    }
    fn with_queue<Q>(
        factory: impl Fn() -> Supervisor<L> + Send + Sync + 'static,
        config: PoolConfig,
        queue_factory: impl FnOnce() -> Q,
        fast_admission: bool,
    ) -> 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,
            }),
            operations: Mutex::new(JoinSet::new()),
            fast_admission,
            changed,
            dispatcher: Arc::new(Notify::new()),
            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.owner.started.load(Ordering::Acquire) || *self.shared.stopping.borrow() {
            return;
        }
        {
            let mut contents = self.shared.contents.lock().unwrap();
            // Serialize initial startup with the lazy-stop completion check.
            // Stop must not finish before a racing begin registers its worker.
            if *self.shared.stopping.borrow() || self.owner.started.swap(true, Ordering::AcqRel) {
                return;
            }
            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()
    }
    /// Read the latest resource during outages or idle retirement. Returns None
    /// after awaited stop; this read neither reserves capacity nor implies readiness.
    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)
    }
    /// Observe resource state and capacity changes. Queue insertion/cancellation
    /// wakes the dispatcher directly rather than broadcasting to observers.
    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,
            ResourceState::Failed,
        ] {
            if states.contains(&candidate) {
                return candidate;
            }
        }
        ResourceState::Disconnected
    }
    /// Wait for readiness within an overall timeout, including recovery.
    /// Zero never starts supervision; shutdown takes precedence over expiry.
    pub async fn connected_with_timeout(&self, duration: Duration) -> Result<(), Error<L::Error>> {
        crate::with_call_timeout(duration, self.stopped(), self.connected()).await
    }
    pub async fn connected(&self) -> Result<(), Error<L::Error>> {
        self.begin();
        let mut changed = self.subscribe();
        let mut stopping = self.shared.stopping.subscribe();
        loop {
            if *stopping.borrow() {
                return Err(Error::Stopped);
            }
            if let Some(cause) = self.failure() {
                return Err(Error::Lifecycle(cause));
            }
            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()
    }
    /// Return the first slot's cause only when every slot is failed. Healthy,
    /// leased or recovering slots remain eligible and prevent aggregate failure.
    pub fn failure(&self) -> Option<Arc<L::Error>> {
        let contents = self.shared.contents.lock().unwrap();
        all_failed(&contents)
    }
    /// Reset suspended slots explicitly. Failed slots remain counted toward
    /// capacity and are never idle-retired or automatically replaced.
    pub fn reset_failed(&self) -> usize {
        if *self.shared.stopping.borrow() {
            return 0;
        }
        let reset = self
            .supervisors()
            .iter()
            .filter(|s| s.reset_failure())
            .count();
        if reset > 0 {
            self.shared.notify();
        }
        reset
    }
    pub async fn borrow(&self) -> Result<ResourceLease<L>, Error<L::Error>> {
        self.begin();
        let mut lease = borrow(self.shared.clone()).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
    }
    /// Run once within an overall timeout including queueing, readiness, the
    /// operation and any failure teardown. No replay. Expiry cancels queued work
    /// or signals cancellation of the tracked active job; its drop is asynchronous.
    /// Adapters must still poison interrupted exchanges. Zero never admits work,
    /// and shutdown takes precedence over expiry.
    pub async fn execute_with_timeout<F, Fut, T>(
        &self,
        duration: Duration,
        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_and_timeout(0, duration, operation)
            .await
    }
    /// Single-attempt timeout dispatch with explicit queue priority.
    /// See `execute_with_timeout` for deadline and cancellation guarantees.
    pub async fn execute_with_priority_and_timeout<F, Fut, T>(
        &self,
        priority: i32,
        duration: Duration,
        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,
    {
        crate::with_call_timeout(
            duration,
            self.stopped(),
            self.execute_with_priority(priority, operation),
        )
        .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 (sender, receiver) = oneshot::channel();
        let mut cancellation = {
            let mut contents = self.shared.contents.lock().unwrap();
            if *self.shared.stopping.borrow() {
                return Err(Error::Stopped);
            }
            if let Some(cause) = all_failed(&contents) {
                return Err(Error::Lifecycle(cause));
            }
            let sequence = contents.sequence;
            contents.sequence += 1;
            // Only built-in unbounded queues can skip admission. Custom queues
            // must always get their push/rejection/ordering opportunity.
            let lease = if self.shared.fast_admission
                && contents.queue.is_empty()
                && self.shared.waiters.load(Ordering::Acquire) == 0
            {
                reserve_ready(&self.shared, &contents)
            } else {
                None
            };
            if let Some(lease) = lease {
                // Register under the contents lock so shutdown cannot pass its
                // admission barrier before this task has been tracked.
                self.shared
                    .operations
                    .lock()
                    .unwrap()
                    .spawn(run_job(sender, Ok(lease), operation));
                self.shared.dispatcher.notify_one();
                None
            } else {
                let cancelled = Arc::new(AtomicBool::new(false));
                let cancellation = CancelOnDrop {
                    cancelled: cancelled.clone(),
                    dispatcher: self.shared.dispatcher.clone(),
                    armed: true,
                };
                contents
                    .queue
                    .push(Operation {
                        priority,
                        sequence,
                        cancelled,
                        job: Box::new(move |lease| Box::pin(run_job(sender, lease, operation))),
                    })
                    .map_err(Error::Queue)?;
                // Queue changes concern the dispatcher, not readiness/capacity waiters.
                self.shared.dispatcher.notify_one();
                Some(cancellation)
            }
        };
        let result = receiver.await.unwrap_or(Err(Error::Stopped));
        // The dispatcher has already removed completed work from the queue.
        // Lease release and task completion provide the necessary wakeups;
        // only an abandoned call needs a cancellation notification.
        if let Some(cancellation) = &mut cancellation {
            cancellation.armed = false;
        }
        drop(cancellation);
        if matches!(result, Err(Error::Lifecycle(_))) && *self.shared.stopping.borrow() {
            return Err(Error::Stopped);
        }
        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();
        {
            let _contents = self.shared.contents.lock().unwrap();
            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;
            }
        }
    }
}
/// 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 }
    }
    /// Run once with a timeout starting on this already-held lease. Includes
    /// replacement readiness; expiry drops work without replay and does not
    /// release the caller's lease. Shutdown takes precedence.
    pub async fn execute_with_timeout<F, Fut, T>(
        &self,
        duration: Duration,
        operation: F,
    ) -> Result<T, Error<L::Error>>
    where
        F: FnOnce(Arc<L::Resource>) -> Fut,
        Fut: Future<Output = Result<T, L::Error>>,
    {
        crate::with_call_timeout(
            duration,
            self.shared.stopping.subscribe(),
            self.execute(operation),
        )
        .await
    }
    /// 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();
    }
}
/// 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 failure(&self) -> Option<Arc<L::Error>> {
        self.pool.failure()
    }
    pub fn reset_failed(&self) -> usize {
        self.pool.reset_failed()
    }
    pub fn new(pool: Pool<L>) -> Self {
        Self { pool }
    }
    /// Run once on an exclusive pooled resource.
    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.pool.execute(operation).await
    }
    /// Run once with an overall timeout including queueing and readiness.
    /// See `Pool::execute_with_timeout` for cancellation and shutdown semantics.
    pub async fn execute_with_timeout<F, Fut, T>(
        &self,
        duration: Duration,
        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.pool.execute_with_timeout(duration, operation).await
    }
    /// Explicitly retry a replay-safe operation while retaining an exclusive lease.
    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.pool
            .execute_with_retry(policy, operation, retry_if)
            .await
    }
    /// Wait for readiness within an overall timeout, including recovery.
    /// Zero never starts supervision; shutdown takes precedence over expiry.
    pub async fn connected_with_timeout(&self, duration: Duration) -> Result<(), Error<L::Error>> {
        crate::with_call_timeout(duration, self.pool.stopped(), self.connected()).await
    }
    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;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[tokio::test]
    async fn relay_forwards_readiness_published_before_its_first_poll() {
        let (state, receiver) = watch::channel(crate::ResourceStatus::<()> {
            state: ResourceState::Connecting,
            handle: None,
        });
        let (changed, mut observer) = watch::channel(0_u64);
        let dispatcher = Arc::new(Notify::new());
        state.send_replace(crate::ResourceStatus {
            state: ResourceState::Connected,
            handle: None,
        });
        let relay = tokio::spawn(relay_state(receiver, changed, dispatcher.clone()));
        tokio::time::timeout(Duration::from_secs(1), observer.changed())
            .await
            .unwrap()
            .unwrap();
        tokio::time::timeout(Duration::from_secs(1), dispatcher.notified())
            .await
            .unwrap();
        state.send_replace(crate::ResourceStatus {
            state: ResourceState::Stopped,
            handle: None,
        });
        tokio::time::timeout(Duration::from_secs(1), relay)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(*observer.borrow(), 2);
    }
    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());
    }
}