aws-smithy-http-client 1.5.0

HTTP client abstractions for generated smithy clients
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! HTTP/1 connection records and exclusive request-sender ownership.
//!
//! Every installed connection remains in its connection-owning [`H1CellState`]
//! until logical close. While the record is live, its exclusive sender exists
//! in exactly one place: inside an `Idle` record or in one external owner while
//! the record is `Selected` or `ReservedForPeer`. Transitions move the sender; they
//! never copy it. The residence records that ownership:
//!
//! ```text
//! completed handshake -- insert_selected ---------------------> Selected
//! test fixture ------- insert_idle ----------------------------> Idle(sender)
//!
//! Idle(sender) -- select_idle --------------------------------> Selected
//! Idle(sender) -- take_idle_candidate ----------------> ReservedForPeer
//! Selected ---- reserve_for_peer ---------------------> ReservedForPeer
//! ReservedForPeer -- commit_return_to_waiter -----------------> Selected
//! Selected ---- return_idle ----------------------------------> Idle(sender)
//! ReservedForPeer -- return_idle -----------------------------> Idle(sender)
//!
//! Idle(sender) ------------ begin_close ----------------------> Closing
//! Selected / ReservedForPeer -- begin_close / close_owned ---> Closing
//! Closing -------- remove_closed -----------------------------> removed
//! ```
//!
//! Outside the lock, ownership moves through values whose drop behavior is
//! specific to the protocol phase:
//!
//! ```text
//! H1Selection
//!   |-- dropped ------------------------------> return to connection-owning cell
//!   |-- retire_connection --------------------------------> logical close
//!   `-- request accepted --> H1Exchange
//!                              |-- incomplete or dropped --> logical close
//!                              `-- offer_for_reuse --------> return to connection-owning cell
//!
//! cross-cell reuse --> ProvisionalH1
//!                        |-- borrow ----------------------> H1Selection
//!                        |-- reclaim ---------------------> logical close
//!                        `-- rejected or dropped ---------> return to connection-owning cell
//! ```
//!
//! [`H1Selection`], [`H1Exchange`], and [`ProvisionalH1`] are the
//! sender-owning values outside the cell lock. Their drop behavior returns or
//! retires the sender so a cancellation cannot leave a record with no terminal
//! owner. They retain the owning cell weakly because a selected sender may
//! temporarily be the ready result in that same cell; cell teardown makes the
//! fallback close the connection directly.

use super::super::admission::{
    AdmissionAction, H1Candidate, H1MatchId, H1SupplyStatus, OriginAdmission, PreparedH1IdleProbe,
    PreparedH1Reservation, SupplyRevision,
};
use super::super::connection::{CloseReason, ConnectionState};
use super::{AcquisitionOutcome, OriginCell};
use crate::sync::{Arc, Weak};
use aws_smithy_runtime_api::client::connection::ConnectionId;
use std::collections::{HashMap, VecDeque};
use std::fmt;
use std::task::{Context, Poll};
use std::time::SystemTime;

use aws_smithy_types::body::SdkBody;

/// Exclusive request handle for one Hyper HTTP/1 client connection.
///
/// Hyper creates one `SendRequest<SdkBody>` handle when the client handshake
/// completes. Pool code calls this handle the HTTP/1 sender. Moving it between
/// residence guards transfers authority to send the next request; it does not
/// move the socket or protocol driver. The test-only variant exercises those
/// ownership transitions without running Hyper.
pub(in crate::client::pool) enum H1Sender {
    /// Hyper's exclusive HTTP/1 request sender.
    Hyper(hyper::client::conn::http1::SendRequest<SdkBody>),
    /// Synthetic sender identity used only by ownership tests.
    #[cfg(test)]
    Test { id: u64, ready: bool },
}

impl H1Sender {
    /// Wraps a sender returned by a successful Hyper HTTP/1 handshake.
    pub(in crate::client::pool) fn from_hyper(
        sender: hyper::client::conn::http1::SendRequest<SdkBody>,
    ) -> Self {
        Self::Hyper(sender)
    }

    /// Returns the Hyper sender for readiness and dispatch.
    ///
    /// # Panics
    ///
    /// Panics when a test-only sender reaches the real dispatch path.
    pub(in crate::client::pool) fn hyper_mut(
        &mut self,
    ) -> &mut hyper::client::conn::http1::SendRequest<SdkBody> {
        match self {
            Self::Hyper(sender) => sender,
            #[cfg(test)]
            Self::Test { .. } => panic!("test HTTP/1 sender reached Hyper dispatch"),
        }
    }

    /// Returns whether Hyper already permits another request.
    fn is_ready(&self) -> bool {
        match self {
            Self::Hyper(sender) => sender.is_ready(),
            #[cfg(test)]
            Self::Test { ready, .. } => *ready,
        }
    }

    /// Polls Hyper for proof that another request may be sent.
    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), hyper::Error>> {
        match self {
            Self::Hyper(sender) => sender.poll_ready(cx),
            #[cfg(test)]
            Self::Test { ready: true, .. } => Poll::Ready(Ok(())),
            #[cfg(test)]
            Self::Test { ready: false, .. } => Poll::Pending,
        }
    }

    /// Creates a synthetic sender for state-machine tests.
    #[cfg(test)]
    pub(in crate::client::pool) fn test(id: u64) -> Self {
        Self::Test { id, ready: true }
    }

    /// Creates a synthetic sender whose readiness remains pending.
    #[cfg(test)]
    pub(in crate::client::pool) fn pending_test(id: u64) -> Self {
        Self::Test { id, ready: false }
    }

    /// Returns the synthetic sender identity.
    #[cfg(test)]
    pub(super) fn test_id(&self) -> u64 {
        match self {
            Self::Test { id, .. } => *id,
            Self::Hyper(_) => panic!("Hyper sender used in a synthetic ownership test"),
        }
    }
}

impl fmt::Debug for H1Sender {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Hyper(_) => f.write_str("H1Sender::Hyper"),
            #[cfg(test)]
            Self::Test { id, ready } => f
                .debug_struct("H1Sender::Test")
                .field("id", id)
                .field("ready", ready)
                .finish(),
        }
    }
}

/// Local reservation for one peer match and its fairness debt.
///
/// Reservation residence and fairness debt are separate because a completed
/// transfer releases the reservation immediately, while the debt remains until
/// later local service or the disappearance of compatible local demand.
#[derive(Debug, Default)]
struct H1Reservation {
    /// State of the current peer reservation.
    state: H1ReservationState,
    /// Whether this cell's next usable HTTP/1 turn must remain local after the
    /// operation that earned it has completed.
    local_turn_owed: bool,
}

/// Authoritative state of one cell-local peer reservation.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
enum H1ReservationState {
    /// No peer match may intercept a sender return.
    #[default]
    Available,
    /// The next reusable sender return is reserved for this match.
    Installed(H1MatchId),
    /// A provisional sender is outside the cell lock for this match.
    Resolving(H1MatchId),
}

impl H1Reservation {
    /// Reserves a future reusable return for one retained match.
    fn reserve_waiting(&mut self, match_id: H1MatchId) -> bool {
        if !matches!(self.state, H1ReservationState::Available) {
            return false;
        }
        self.state = H1ReservationState::Installed(match_id);
        true
    }

    /// Reserves a match that already owns an extracted idle sender.
    fn reserve_resolving(&mut self, match_id: H1MatchId) -> bool {
        if !matches!(self.state, H1ReservationState::Available) {
            return false;
        }
        self.state = H1ReservationState::Resolving(match_id);
        true
    }

    /// Transfers a waiting match into resolution on sender return.
    fn take_return_match(&mut self) -> Option<H1MatchId> {
        let H1ReservationState::Installed(match_id) = self.state else {
            return None;
        };
        self.state = H1ReservationState::Resolving(match_id);
        Some(match_id)
    }

    /// Releases a matching reservation without earning a local fairness turn.
    fn release(&mut self, match_id: H1MatchId) -> bool {
        if !self.names(match_id) {
            return false;
        }
        self.state = H1ReservationState::Available;
        true
    }

    /// Completes an irreversible transfer and records any usable local turn.
    fn complete_transfer(&mut self, match_id: H1MatchId, local_h1_demand: bool) -> bool {
        if !matches!(self.state, H1ReservationState::Resolving(current) if current == match_id) {
            return false;
        }
        self.state = H1ReservationState::Available;
        self.local_turn_owed |= local_h1_demand;
        true
    }

    /// Returns whether a usable local turn currently excludes a peer H1 match.
    fn blocks_peer_selection(&self, local_h1_demand: bool) -> bool {
        self.local_turn_owed && local_h1_demand
    }

    /// Consumes an owed turn when local HTTP/1 service wins.
    fn consume_local_turn(&mut self) -> bool {
        if !self.local_turn_owed {
            return false;
        }
        self.local_turn_owed = false;
        true
    }

    /// Clears debt that can no longer be consumed by local demand.
    fn clear_unused_turn(&mut self, local_h1_demand: bool) -> bool {
        if !self.local_turn_owed || local_h1_demand {
            return false;
        }
        self.local_turn_owed = false;
        true
    }

    /// Returns whether another peer reservation may be installed.
    fn is_available(&self) -> bool {
        matches!(self.state, H1ReservationState::Available)
    }

    /// Returns whether this reservation still names the given operation.
    fn names(&self, match_id: H1MatchId) -> bool {
        matches!(
            self.state,
            H1ReservationState::Installed(current) | H1ReservationState::Resolving(current)
                if current == match_id
        )
    }

    /// Checks relationships that are not already encoded by the state enum.
    #[cfg(any(debug_assertions, test))]
    fn assert_consistent(&self, supports_peer_reservation: bool) {
        if std::thread::panicking() {
            return;
        }
        if matches!(self.state, H1ReservationState::Installed(_)) {
            assert!(
                supports_peer_reservation,
                "installed HTTP/1 peer reservation had no externally owned sender record to settle it"
            );
        }
    }

    #[cfg(test)]
    fn local_turn_owed(&self) -> bool {
        self.local_turn_owed
    }
}

/// Complete HTTP/1 sender and peer-reservation state under one cell lock.
#[derive(Debug, Default)]
pub(super) struct H1CellState {
    /// Every installed record that has not completed logical close.
    records: HashMap<ConnectionId, H1Record>,
    /// Reusable records in return order; selection takes the newest sender.
    idle_order: VecDeque<ConnectionId>,
    /// Peer match that may intercept the next reusable sender return.
    peer_reservation: H1Reservation,
}

/// One cell-owned HTTP/1 connection record.
#[derive(Debug)]
struct H1Record {
    /// Shared logical and physical connection lifetime.
    connection: Arc<ConnectionState>,
    /// Location of the record's exclusive sender.
    sender_state: H1SenderResidence,
}

/// Authoritative location of one exclusive HTTP/1 sender.
#[derive(Debug)]
enum H1SenderResidence {
    /// The sender is stored in this record and available for local selection.
    Idle {
        /// Exclusive sender available for the next local request.
        sender: H1Sender,
        /// Maintenance deadline, absent when idle expiry is disabled.
        deadline: Option<SystemTime>,
    },
    /// A request-side [`H1Selection`] or [`H1Exchange`] owns the sender.
    Selected,
    /// A [`ProvisionalH1`] owns the sender for cross-cell reuse.
    ReservedForPeer,
    /// Logical close has started and no new selection or return may commit.
    Closing,
}

impl H1CellState {
    /// Returns whether admission may install another peer reservation.
    pub(super) fn peer_reservation_available(&self) -> bool {
        self.peer_reservation.is_available()
    }

    /// Returns whether fairness currently excludes peer selection.
    pub(super) fn blocks_peer_selection(&self, local_h1_demand: bool) -> bool {
        self.peer_reservation.blocks_peer_selection(local_h1_demand)
    }

    /// Reserves a future sender return for one retained peer match.
    pub(super) fn reserve_waiting(&mut self, match_id: H1MatchId) -> bool {
        self.peer_reservation.reserve_waiting(match_id)
    }

    /// Reserves a peer match that already owns an extracted sender.
    pub(super) fn reserve_resolving(&mut self, match_id: H1MatchId) -> bool {
        self.peer_reservation.reserve_resolving(match_id)
    }

    /// Takes a waiting peer match when a reusable sender returns.
    pub(super) fn take_return_match(&mut self) -> Option<H1MatchId> {
        self.peer_reservation.take_return_match()
    }

    /// Releases a peer reservation without recording a local turn.
    pub(super) fn release_peer_reservation(&mut self, match_id: H1MatchId) -> bool {
        self.peer_reservation.release(match_id)
    }

    /// Returns whether the peer reservation still names `match_id`.
    pub(super) fn names_peer_reservation(&self, match_id: H1MatchId) -> bool {
        self.peer_reservation.names(match_id)
    }

    /// Completes a transferred peer sender and records any local fairness turn.
    pub(super) fn complete_peer_transfer(
        &mut self,
        match_id: H1MatchId,
        local_h1_demand: bool,
    ) -> bool {
        self.peer_reservation
            .complete_transfer(match_id, local_h1_demand)
    }

    /// Consumes an owed local turn after local HTTP/1 service wins.
    pub(super) fn consume_local_turn(&mut self) -> bool {
        self.peer_reservation.consume_local_turn()
    }

    /// Clears fairness debt that no live local demand can consume.
    pub(super) fn clear_unused_turn(&mut self, local_h1_demand: bool) -> bool {
        self.peer_reservation.clear_unused_turn(local_h1_demand)
    }

    /// Returns whether this cell owes its next usable sender to local demand.
    #[cfg(test)]
    pub(super) fn local_turn_owed(&self) -> bool {
        self.peer_reservation.local_turn_owed()
    }

    /// Installs a fresh connection as selected by its launching acquisition.
    pub(super) fn insert_selected(
        &mut self,
        connection: Arc<ConnectionState>,
        sender: H1Sender,
    ) -> Result<OwnedH1Sender, OwnedH1Sender> {
        let owner = OwnedH1Sender::new(connection, sender, false);
        if self.records.contains_key(&owner.id()) {
            return Err(owner);
        }
        self.records.insert(
            owner.id(),
            H1Record {
                connection: owner.connection.clone(),
                sender_state: H1SenderResidence::Selected,
            },
        );
        self.assert_consistent();
        Ok(owner)
    }

    /// Installs a connection that completed without a live launching waiter.
    #[cfg(test)]
    pub(super) fn insert_idle(
        &mut self,
        connection: Arc<ConnectionState>,
        sender: H1Sender,
        deadline: Option<SystemTime>,
    ) -> Result<(), OwnedH1Sender> {
        let owner = OwnedH1Sender::new(connection, sender, true);
        if self.records.contains_key(&owner.id()) {
            return Err(owner);
        }
        let id = owner.id();
        let OwnedH1Sender {
            connection, sender, ..
        } = owner;
        self.records.insert(
            id,
            H1Record {
                connection,
                sender_state: H1SenderResidence::Idle { sender, deadline },
            },
        );
        self.idle_order.push_back(id);
        self.assert_consistent();
        Ok(())
    }

    /// Takes the most recently returned idle sender for one request.
    pub(super) fn select_idle(&mut self) -> Option<OwnedH1Sender> {
        let id = self.idle_order.pop_back()?;
        let record = self
            .records
            .get_mut(&id)
            .expect("idle HTTP/1 record disappeared");
        if !matches!(record.sender_state, H1SenderResidence::Idle { .. }) {
            panic!("idle HTTP/1 order named a non-idle record");
        }
        let H1SenderResidence::Idle { sender, .. } =
            std::mem::replace(&mut record.sender_state, H1SenderResidence::Selected)
        else {
            unreachable!("HTTP/1 residence changed under the cell lock");
        };
        let owner = OwnedH1Sender::new(record.connection.clone(), sender, true);
        self.assert_consistent();
        Some(owner)
    }

    /// Extracts the newest idle sender into provisional return residence.
    ///
    /// A returning sender uses this path so rejection can follow the same ordinary
    /// return fallback as a sender intercepted after an active exchange.
    pub(super) fn take_idle_candidate(&mut self) -> Option<OwnedH1Sender> {
        let id = self.idle_order.pop_back()?;
        let record = self
            .records
            .get_mut(&id)
            .expect("idle HTTP/1 record disappeared");
        let H1SenderResidence::Idle { sender, .. } =
            std::mem::replace(&mut record.sender_state, H1SenderResidence::ReservedForPeer)
        else {
            panic!("idle HTTP/1 order named a non-idle record");
        };
        let owner = OwnedH1Sender::new(record.connection.clone(), sender, true);
        self.assert_consistent();
        Some(owner)
    }

    /// Returns whether a sender could satisfy peer selection now or on return.
    pub(super) fn has_returnable(&self) -> bool {
        self.records.values().any(|record| {
            matches!(
                record.sender_state,
                H1SenderResidence::Idle { .. }
                    | H1SenderResidence::Selected
                    | H1SenderResidence::ReservedForPeer
            )
        })
    }

    /// Returns whether `owner` may still re-enter reusable return policy.
    pub(super) fn accepts_return(&self, owner: &OwnedH1Sender) -> bool {
        self.records.get(&owner.id()).is_some_and(|record| {
            matches!(
                record.sender_state,
                H1SenderResidence::Selected | H1SenderResidence::ReservedForPeer
            )
        })
    }

    /// Returns whether an installed peer reservation still has sender state.
    ///
    /// Logical close may move an externally owned sender to `Closing` before
    /// its return resolves the installed match, so reservation consistency is
    /// broader than current peer selectability.
    #[cfg(any(debug_assertions, test))]
    fn supports_peer_reservation(&self) -> bool {
        self.records.values().any(|record| {
            matches!(
                record.sender_state,
                H1SenderResidence::Selected
                    | H1SenderResidence::ReservedForPeer
                    | H1SenderResidence::Closing
            )
        })
    }

    /// Reserves an external sender for cross-cell reuse.
    pub(super) fn reserve_for_peer(&mut self, owner: &OwnedH1Sender) -> bool {
        let Some(record) = self.records.get_mut(&owner.id()) else {
            return false;
        };
        match record.sender_state {
            H1SenderResidence::Selected => {
                record.sender_state = H1SenderResidence::ReservedForPeer;
                self.assert_consistent();
                true
            }
            H1SenderResidence::ReservedForPeer => true,
            H1SenderResidence::Idle { .. } | H1SenderResidence::Closing => false,
        }
    }

    /// Restores a returned sender to idle storage.
    pub(super) fn return_idle(
        &mut self,
        owner: OwnedH1Sender,
        deadline: Option<SystemTime>,
    ) -> Result<(), OwnedH1Sender> {
        let Some(record) = self.records.get_mut(&owner.id()) else {
            return Err(owner);
        };
        if !matches!(
            record.sender_state,
            H1SenderResidence::Selected | H1SenderResidence::ReservedForPeer
        ) {
            return Err(owner);
        }
        let id = owner.id();
        let OwnedH1Sender { sender, .. } = owner;
        record.sender_state = H1SenderResidence::Idle { sender, deadline };
        self.idle_order.push_back(id);
        self.assert_consistent();
        Ok(())
    }

    /// Commits a selected or returning sender to a waiting request.
    pub(super) fn commit_return_to_waiter(&mut self, owner: &OwnedH1Sender) -> bool {
        let Some(record) = self.records.get_mut(&owner.id()) else {
            return false;
        };
        match record.sender_state {
            H1SenderResidence::Selected => true,
            H1SenderResidence::ReservedForPeer => {
                record.sender_state = H1SenderResidence::Selected;
                self.assert_consistent();
                true
            }
            H1SenderResidence::Idle { .. } | H1SenderResidence::Closing => false,
        }
    }

    /// Marks a selected or returning sender as closing.
    ///
    /// The caller still owns the sender and must complete logical close after
    /// releasing the cell lock.
    pub(super) fn close_owned(&mut self, owner: &OwnedH1Sender) -> bool {
        let should_close = match self.records.get_mut(&owner.id()) {
            Some(record) => match record.sender_state {
                H1SenderResidence::Selected | H1SenderResidence::ReservedForPeer => {
                    record.sender_state = H1SenderResidence::Closing;
                    true
                }
                H1SenderResidence::Closing => false,
                H1SenderResidence::Idle { .. } => {
                    panic!("externally owned HTTP/1 sender was recorded as idle")
                }
            },
            None => true,
        };
        self.assert_consistent();
        should_close
    }

    /// Starts close for a record named without its external sender.
    ///
    /// Idle close extracts the sender. Selected and returning records stay
    /// represented as `Closing` until their external owner comes back.
    pub(super) fn begin_close(&mut self, id: ConnectionId) -> Option<H1CloseTransition> {
        let record = self.records.get_mut(&id)?;
        let connection = record.connection.clone();
        let sender = match &record.sender_state {
            H1SenderResidence::Idle { .. } => {
                let position = self
                    .idle_order
                    .iter()
                    .position(|candidate| *candidate == id)
                    .expect("idle HTTP/1 record was absent from idle order");
                self.idle_order.remove(position);
                let H1SenderResidence::Idle { sender, .. } =
                    std::mem::replace(&mut record.sender_state, H1SenderResidence::Closing)
                else {
                    unreachable!("HTTP/1 residence changed under the cell lock");
                };
                Some(sender)
            }
            H1SenderResidence::Selected | H1SenderResidence::ReservedForPeer => {
                record.sender_state = H1SenderResidence::Closing;
                None
            }
            H1SenderResidence::Closing => return None,
        };
        self.assert_consistent();
        Some(H1CloseTransition { connection, sender })
    }

    /// Removes a record after logical close and sender destruction complete.
    pub(super) fn remove_closed(&mut self, id: ConnectionId) {
        let Some(record) = self.records.get(&id) else {
            return;
        };
        if !matches!(record.sender_state, H1SenderResidence::Closing) {
            return;
        }
        self.records.remove(&id);
        self.assert_consistent();
    }

    /// Returns idle and externally owned sender counts.
    pub(super) fn connection_counts(&self) -> (usize, usize) {
        self.records
            .values()
            .fold((0, 0), |(idle, active), record| match record.sender_state {
                H1SenderResidence::Idle { .. } => (idle + 1, active),
                H1SenderResidence::Selected | H1SenderResidence::ReservedForPeer => {
                    (idle, active + 1)
                }
                H1SenderResidence::Closing => (idle, active),
            })
    }

    /// Returns the installed record and idle counts.
    #[cfg(test)]
    pub(super) fn counts(&self) -> (usize, usize) {
        (self.records.len(), self.idle_order.len())
    }

    /// Returns the synthetic identities of every idle sender.
    #[cfg(all(test, smithy_http_client_loom))]
    pub(super) fn idle_sender_ids(&self) -> Vec<u64> {
        self.records
            .values()
            .filter_map(|record| match &record.sender_state {
                H1SenderResidence::Idle { sender, .. } => Some(sender.test_id()),
                H1SenderResidence::Selected
                | H1SenderResidence::ReservedForPeer
                | H1SenderResidence::Closing => None,
            })
            .collect()
    }

    /// Returns the sole installed connection for focused dispatch tests.
    #[cfg(all(test, feature = "rt-tokio"))]
    pub(super) fn only_connection_for_test(&self) -> Arc<ConnectionState> {
        assert_eq!(
            1,
            self.records.len(),
            "expected exactly one installed HTTP/1 record"
        );
        self.records
            .values()
            .next()
            .expect("HTTP/1 record count changed under the cell lock")
            .connection
            .clone()
    }

    /// Returns idle records whose configured deadline has elapsed.
    pub(super) fn expired_idle(&self, now: SystemTime) -> Vec<ConnectionId> {
        self.idle_order
            .iter()
            .copied()
            .filter(|id| {
                matches!(
                    self.records.get(id).map(|record| &record.sender_state),
                    Some(H1SenderResidence::Idle {
                        deadline: Some(deadline),
                        ..
                    }) if *deadline <= now
                )
            })
            .collect()
    }

    /// Returns the nearest configured deadline among reusable senders.
    pub(super) fn nearest_idle_deadline(&self) -> Option<SystemTime> {
        self.idle_order
            .iter()
            .filter_map(|id| {
                match &self
                    .records
                    .get(id)
                    .expect("idle HTTP/1 record disappeared")
                    .sender_state
                {
                    H1SenderResidence::Idle { deadline, .. } => *deadline,
                    _ => unreachable!("idle HTTP/1 order named a non-idle record"),
                }
            })
            .min()
    }

    /// Returns every installed record identity for pool-wide shutdown.
    pub(super) fn connection_ids(&self) -> Vec<ConnectionId> {
        self.records.keys().copied().collect()
    }

    /// Checks that idle records and the idle order describe the same set.
    pub(super) fn assert_consistent(&self) {
        #[cfg(any(debug_assertions, test))]
        {
            if std::thread::panicking() {
                return;
            }
            self.peer_reservation
                .assert_consistent(self.supports_peer_reservation());
            let idle_records = self
                .records
                .values()
                .filter(|record| matches!(record.sender_state, H1SenderResidence::Idle { .. }))
                .count();
            assert_eq!(
                idle_records,
                self.idle_order.len(),
                "HTTP/1 idle count did not match idle order"
            );
            let mut seen = HashMap::new();
            for id in &self.idle_order {
                assert!(
                    matches!(
                        self.records.get(id).map(|record| &record.sender_state),
                        Some(H1SenderResidence::Idle { .. })
                    ),
                    "HTTP/1 idle order named a missing or non-idle record"
                );
                assert!(
                    seen.insert(*id, ()).is_none(),
                    "HTTP/1 idle order contained a duplicate record"
                );
            }
        }
    }
}

/// Exclusive sender and connection state detached from a connection-owning cell record.
#[derive(Debug)]
pub(super) struct OwnedH1Sender {
    /// Shared lifetime state for the physical connection.
    connection: Arc<ConnectionState>,
    /// The one Hyper HTTP/1 sender for the connection.
    sender: H1Sender,
    /// Whether this sender came from an already installed reusable record.
    reused: bool,
}

impl OwnedH1Sender {
    /// Creates detached sender ownership.
    fn new(connection: Arc<ConnectionState>, sender: H1Sender, reused: bool) -> Self {
        Self {
            connection,
            sender,
            reused,
        }
    }

    /// Returns the installed connection identity.
    pub(super) fn id(&self) -> ConnectionId {
        self.connection.id()
    }

    /// Returns whether this selection came from an existing reusable record.
    pub(super) fn is_reused(&self) -> bool {
        self.reused
    }

    /// Marks the next request as reusing an already established connection.
    pub(super) fn mark_reused(&mut self) {
        self.reused = true;
    }

    /// Returns the shared connection state.
    pub(super) fn connection(&self) -> &Arc<ConnectionState> {
        &self.connection
    }

    /// Returns the exclusive sender.
    pub(super) fn sender_mut(&mut self) -> &mut H1Sender {
        &mut self.sender
    }

    /// Returns the synthetic sender identity.
    #[cfg(test)]
    pub(super) fn test_sender_id(&self) -> u64 {
        self.sender.test_id()
    }
}

/// Returns an external sender to its connection-owning cell.
///
/// A sender whose cell was torn down closes its connection directly.
fn return_to_connection_cell(connection_cell: &Weak<OriginCell>, owner: OwnedH1Sender) {
    if let Some(connection_cell) = connection_cell.upgrade() {
        OriginCell::return_h1_owner(&connection_cell, owner);
    } else {
        owner.connection().logical_close(CloseReason::PoolDropped);
        drop(owner);
    }
}

/// Retires an external sender through its connection-owning cell.
///
/// A sender whose cell was torn down closes its connection directly.
fn retire_at_connection_cell(
    connection_cell: &Weak<OriginCell>,
    owner: OwnedH1Sender,
    reason: CloseReason,
) {
    if let Some(connection_cell) = connection_cell.upgrade() {
        OriginCell::retire_h1_owner(&connection_cell, owner, reason);
    } else {
        owner.connection().logical_close(reason);
        drop(owner);
    }
}

/// State detached when close begins for a record named by identity.
pub(super) struct H1CloseTransition {
    /// Connection whose logical lifetime close must end.
    pub(super) connection: Arc<ConnectionState>,
    /// Idle sender extracted for destruction, if close found one in the cell.
    pub(super) sender: Option<H1Sender>,
}

/// Exclusive sender checked out for readiness and one request dispatch.
///
/// Local selection, successful establishment, or peer reuse creates this value
/// while the installed record is `Selected`. The value alone may use the
/// sender. Hyper accepting the request transfers ownership to [`H1Exchange`];
/// explicit retirement closes the installed record. Dropping an undispatched
/// selection returns the sender through ordinary connection-owning-cell policy.
pub(in crate::client::pool) struct H1Selection {
    /// Non-retaining reference to the cell that owns the installed record.
    connection_cell: Weak<OriginCell>,
    /// Sender ownership until return, retirement, or response transfer.
    owner: Option<OwnedH1Sender>,
}

impl H1Selection {
    /// Creates a selected sender owned outside the connection-owning cell lock.
    pub(super) fn new(connection_cell: &Arc<OriginCell>, owner: OwnedH1Sender) -> Self {
        Self {
            connection_cell: Weak::from_arc(connection_cell),
            owner: Some(owner),
        }
    }

    /// Returns this selection's physical connection identity.
    pub(in crate::client::pool) fn connection_id(&self) -> ConnectionId {
        self.owner
            .as_ref()
            .expect("HTTP/1 selection consumed more than once")
            .id()
    }

    /// Returns whether this sender came from a reusable installed record.
    pub(in crate::client::pool) fn is_reused(&self) -> bool {
        self.owner
            .as_ref()
            .expect("HTTP/1 selection consumed more than once")
            .is_reused()
    }

    /// Returns the selected connection state.
    pub(in crate::client::pool) fn connection(&self) -> &Arc<ConnectionState> {
        self.owner
            .as_ref()
            .expect("HTTP/1 selection consumed more than once")
            .connection()
    }

    /// Returns the exclusive sender for readiness and dispatch.
    pub(in crate::client::pool) fn sender_mut(&mut self) -> &mut H1Sender {
        self.owner
            .as_mut()
            .expect("HTTP/1 selection consumed more than once")
            .sender_mut()
    }

    /// Transfers the sender after Hyper accepts the request.
    ///
    /// The installed record remains `Selected`; [`H1Exchange`] now owns the
    /// sender until the response proves reuse or requires retirement.
    pub(in crate::client::pool) fn into_exchange(mut self) -> H1Exchange {
        H1Exchange {
            connection_cell: self.connection_cell.clone(),
            owner: self.owner.take(),
        }
    }

    /// Returns non-retaining close authority for this selected connection.
    pub(in crate::client::pool) fn close_handle(&self) -> H1CloseHandle {
        let owner = self
            .owner
            .as_ref()
            .expect("HTTP/1 selection consumed more than once");
        H1CloseHandle {
            connection_cell: self.connection_cell.clone(),
            connection: Weak::from_arc(owner.connection()),
            connection_id: owner.id(),
        }
    }

    /// Retires this selection instead of returning it for reuse.
    pub(in crate::client::pool) fn retire_connection(mut self, reason: CloseReason) {
        if let Some(owner) = self.owner.take() {
            retire_at_connection_cell(&self.connection_cell, owner, reason);
        }
    }

    /// Returns the synthetic sender identity.
    #[cfg(test)]
    pub(super) fn test_sender_id(&self) -> u64 {
        self.owner
            .as_ref()
            .expect("HTTP/1 selection consumed more than once")
            .test_sender_id()
    }
}

impl fmt::Debug for H1Selection {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("H1Selection")
            .field(
                "connection-owning cell",
                &self
                    .connection_cell
                    .upgrade()
                    .map(|connection_cell| connection_cell.id().clone()),
            )
            .field("connection_id", &self.owner.as_ref().map(OwnedH1Sender::id))
            .finish()
    }
}

impl Drop for H1Selection {
    fn drop(&mut self) {
        if let Some(owner) = self.owner.take() {
            return_to_connection_cell(&self.connection_cell, owner);
        }
    }
}

/// Exclusive sender for a request Hyper accepted on this connection.
///
/// The installed record remains `Selected` while response-body processing owns
/// this value. After a complete response and successful Hyper readiness,
/// [`H1Exchange::offer_for_reuse`] runs owning-cell return arbitration.
/// Explicit retirement handles protocol failure or upgrade. Dropping the
/// exchange means a reusable message boundary was not proven and closes the
/// connection as an incomplete HTTP/1 exchange.
pub(in crate::client::pool) struct H1Exchange {
    /// Non-retaining reference to the cell that owns the selected record.
    connection_cell: Weak<OriginCell>,
    /// Sender held until Hyper proves it may return.
    owner: Option<OwnedH1Sender>,
}

impl H1Exchange {
    /// Returns the installed connection that owns this exchange.
    pub(in crate::client::pool) fn connection(&self) -> &Arc<ConnectionState> {
        self.owner
            .as_ref()
            .expect("HTTP/1 exchange consumed more than once")
            .connection()
    }

    /// Returns whether Hyper already permits another request.
    pub(in crate::client::pool) fn is_ready(&self) -> bool {
        self.owner
            .as_ref()
            .expect("HTTP/1 exchange consumed more than once")
            .sender
            .is_ready()
    }

    /// Polls Hyper for proof that the sender can accept another request.
    pub(in crate::client::pool) fn poll_ready(
        &mut self,
        cx: &mut Context<'_>,
    ) -> Poll<Result<(), hyper::Error>> {
        self.owner
            .as_mut()
            .expect("HTTP/1 exchange consumed more than once")
            .sender_mut()
            .poll_ready(cx)
    }

    /// Offers a proven-ready sender to owning-cell reuse policy.
    pub(in crate::client::pool) fn offer_for_reuse(mut self) {
        let mut owner = self
            .owner
            .take()
            .expect("HTTP/1 exchange consumed more than once");
        let connection = owner.connection().clone();
        owner.mark_reused();
        return_to_connection_cell(&self.connection_cell, owner);
        connection.complete_h1_exchange(CloseReason::ProtocolClosed);
    }

    /// Retires the sender instead of returning it to the connection-owning cell.
    pub(in crate::client::pool) fn retire_connection(mut self, reason: CloseReason) {
        if let Some(owner) = self.owner.take() {
            let connection = owner.connection().clone();
            retire_at_connection_cell(&self.connection_cell, owner, reason);
            connection.complete_h1_exchange(reason);
        }
    }
}

impl fmt::Debug for H1Exchange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("H1Exchange")
            .field(
                "connection-owning cell",
                &self
                    .connection_cell
                    .upgrade()
                    .map(|connection_cell| connection_cell.id().clone()),
            )
            .field("connection_id", &self.owner.as_ref().map(OwnedH1Sender::id))
            .finish()
    }
}

impl Drop for H1Exchange {
    fn drop(&mut self) {
        if let Some(owner) = self.owner.take() {
            let connection = owner.connection().clone();
            retire_at_connection_cell(
                &self.connection_cell,
                owner,
                CloseReason::IncompleteH1Exchange,
            );
            connection.complete_h1_exchange(CloseReason::IncompleteH1Exchange);
        }
    }
}

/// Sender temporarily detached from a `ReservedForPeer` record.
///
/// Admission matching creates this value before crossing from the
/// connection-owning cell to a requesting cell. A successful borrow converts
/// it into [`H1Selection`]; reclaim closes the connection and releases its
/// capacity. Rejection, cancellation, or `Drop` returns the sender through
/// ordinary connection-owning-cell policy.
pub(in crate::client::pool) struct ProvisionalH1 {
    /// Non-retaining owning cell whose record remains `ReservedForPeer`.
    connection_cell: Weak<OriginCell>,
    /// Sender reserved by the provisional action.
    owner: Option<OwnedH1Sender>,
}

impl ProvisionalH1 {
    /// Creates a provisional owner for a sender extracted by admission matching.
    pub(super) fn new(connection_cell: &Arc<OriginCell>, owner: OwnedH1Sender) -> Self {
        Self {
            connection_cell: Weak::from_arc(connection_cell),
            owner: Some(owner),
        }
    }

    /// Returns the selected connection identity without consuming the sender.
    pub(in crate::client::pool) fn connection_id(&self) -> ConnectionId {
        self.owner
            .as_ref()
            .expect("provisional HTTP/1 sender consumed more than once")
            .id()
    }

    /// Transfers the cell reference and sender into the next match transition.
    pub(super) fn into_parts(mut self) -> (Weak<OriginCell>, OwnedH1Sender) {
        let owner = self
            .owner
            .take()
            .expect("provisional HTTP/1 sender consumed more than once");
        (self.connection_cell.clone(), owner)
    }

    /// Restores provisional ownership when a match transition cannot commit.
    pub(super) fn from_parts(connection_cell: Weak<OriginCell>, owner: OwnedH1Sender) -> Self {
        Self {
            connection_cell,
            owner: Some(owner),
        }
    }
}

impl fmt::Debug for ProvisionalH1 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ProvisionalH1")
            .field(
                "connection-owning cell",
                &self
                    .connection_cell
                    .upgrade()
                    .map(|connection_cell| connection_cell.id().clone()),
            )
            .field("connection_id", &self.owner.as_ref().map(OwnedH1Sender::id))
            .finish()
    }
}

impl Drop for ProvisionalH1 {
    fn drop(&mut self) {
        if let Some(owner) = self.owner.take() {
            return_to_connection_cell(&self.connection_cell, owner);
        }
    }
}

/// Non-retaining authority to retire one installed H1 record.
#[derive(Clone, Debug)]
pub(in crate::client::pool) struct H1CloseHandle {
    /// Connection-owning cell used to remove dispatch eligibility before
    /// logical close.
    connection_cell: Weak<OriginCell>,
    /// Core fallback when the connection-owning cell no longer exists.
    connection: Weak<ConnectionState>,
    /// Generation identity rejected by stale close actions.
    connection_id: ConnectionId,
}

impl H1CloseHandle {
    /// Creates a close handle for a newly installed H1 record.
    pub(in crate::client::pool) fn new(
        connection_cell: &Arc<OriginCell>,
        connection: &Arc<ConnectionState>,
    ) -> Self {
        Self {
            connection_cell: Weak::from_arc(connection_cell),
            connection: Weak::from_arc(connection),
            connection_id: connection.id(),
        }
    }

    /// Begins logical close and returns whether this signal won.
    pub(in crate::client::pool) fn close(&self, reason: CloseReason) -> bool {
        if let Some(connection_cell) = self.connection_cell.upgrade() {
            return OriginCell::close_h1(&connection_cell, self.connection_id, reason);
        }
        self.connection
            .upgrade()
            .is_some_and(|connection| connection.logical_close(reason))
    }
}

/// Driver-owned fallback that closes its H1 record on termination.
#[derive(Debug)]
pub(in crate::client::pool) struct H1DriverGuard {
    /// Non-retaining connection close authority.
    close: H1CloseHandle,
    /// Whether drop still represents owner-runtime shutdown.
    active: bool,
}

impl H1DriverGuard {
    /// Arms driver-lifecycle cleanup for an installed H1 record.
    pub(in crate::client::pool) fn new(close: H1CloseHandle) -> Self {
        Self {
            close,
            active: true,
        }
    }

    /// Records ordinary protocol-driver completion.
    pub(in crate::client::pool) fn protocol_closed(mut self) {
        self.active = false;
        self.close.close(CloseReason::ProtocolClosed);
    }
}

impl Drop for H1DriverGuard {
    fn drop(&mut self) {
        if self.active {
            self.close.close(CloseReason::OwnerRuntimeShutdown);
        }
    }
}

impl OriginCell {
    /// Installs a fresh H1 record selected by its launching request.
    ///
    /// # Panics
    ///
    /// Panics if this cell already contains the connection identity.
    pub(in crate::client::pool) fn insert_selected_h1(
        cell: &Arc<Self>,
        connection: Arc<ConnectionState>,
        sender: H1Sender,
    ) -> H1Selection {
        let (installed, supply_update) = {
            let mut state = cell.state.lock();
            let installed = state.h1.insert_selected(connection, sender);
            state.assert_consistent();
            (installed, state.take_h1_supply_update())
        };
        cell.submit_h1_supply_update(supply_update);
        match installed {
            Ok(owner) => H1Selection::new(cell, owner),
            Err(owner) => {
                owner
                    .connection()
                    .logical_close(CloseReason::ProtocolClosed);
                drop(owner);
                panic!("duplicate HTTP/1 connection identity installed in one cell");
            }
        }
    }

    /// Installs an H1 record whose launching acquisition already completed.
    ///
    /// # Panics
    ///
    /// Panics if this cell already contains the connection identity.
    #[cfg(test)]
    pub(in crate::client::pool) fn insert_idle_h1(
        cell: &Arc<Self>,
        connection: Arc<ConnectionState>,
        sender: H1Sender,
    ) {
        let deadline = cell.idle_deadline();
        let (installed, supply_update) = {
            let mut state = cell.state.lock();
            let installed = state.h1.insert_idle(connection, sender, deadline);
            state.assert_consistent();
            (installed, state.take_h1_supply_update())
        };
        cell.submit_h1_supply_update(supply_update);
        if let Err(owner) = installed {
            owner
                .connection()
                .logical_close(CloseReason::ProtocolClosed);
            drop(owner);
            panic!("duplicate HTTP/1 connection identity installed in one cell");
        }
        cell.notify_maintenance(deadline);
    }

    /// Selects the newest reusable H1 sender without origin-wide coordination.
    pub(in crate::client::pool) fn select_h1(cell: &Arc<Self>) -> Option<H1Selection> {
        let (owner, supply_update) = {
            let mut state = cell.state.lock();
            let owner = state.h1.select_idle();
            if owner.is_some() {
                state.h1.consume_local_turn();
            }
            state.assert_consistent();
            let supply_update = cell
                .admission
                .as_ref()
                .and_then(|_| state.take_h1_supply_update());
            (owner, supply_update)
        };
        cell.submit_h1_supply_update(supply_update);
        let owner = owner?;
        Some(H1Selection::new(cell, owner))
    }

    /// Commits one admission-selected supplier reservation at this cell.
    pub(in crate::client::pool) fn commit_h1_reservation(
        cell: &Arc<Self>,
        admission: Arc<OriginAdmission>,
        prepared: PreparedH1Reservation,
    ) -> Option<AdmissionAction> {
        let decision = {
            let mut state = cell.state.lock();
            state.commit_h1_reservation(prepared.match_id)
        };

        let decision = decision.map_candidate(|owner| {
            let provisional = ProvisionalH1::new(cell, owner);
            H1Candidate::new(
                admission.clone(),
                prepared.match_id,
                cell.id.partition(),
                provisional,
            )
        });
        OriginAdmission::settle_h1_reservation(
            &admission,
            prepared.match_id,
            cell.id.partition(),
            decision,
        )
    }

    /// Attempts to extract an idle sender without intercepting a future return.
    pub(in crate::client::pool) fn try_take_idle_h1(
        cell: &Arc<Self>,
        admission: Arc<OriginAdmission>,
        prepared: PreparedH1IdleProbe,
    ) -> Option<AdmissionAction> {
        let decision = {
            let mut state = cell.state.lock();
            state.try_take_idle_h1(prepared.match_id)
        };
        let decision = decision.map_candidate(|owner| {
            let provisional = ProvisionalH1::new(cell, owner);
            H1Candidate::new(
                admission.clone(),
                prepared.match_id,
                cell.id.partition(),
                provisional,
            )
        });
        OriginAdmission::settle_h1_idle_probe(
            &admission,
            prepared.match_id,
            cell.id.partition(),
            decision,
        )
    }

    /// Returns the current admission-facing HTTP/1 supply revision.
    pub(in crate::client::pool) fn current_h1_supply_revision(
        &self,
    ) -> SupplyRevision<H1SupplyStatus> {
        self.state.lock().current_h1_supply_revision()
    }

    /// Clears an installed or resolving reservation after request cancellation.
    pub(in crate::client::pool) fn cancel_h1_reservation(
        &self,
        match_id: H1MatchId,
    ) -> SupplyRevision<H1SupplyStatus> {
        self.state.lock().cancel_h1_reservation(match_id)
    }

    /// Returns a rejected provisional sender through ordinary connection-owning cell handling.
    pub(in crate::client::pool) fn reject_h1_match(
        cell: &Arc<Self>,
        match_id: H1MatchId,
        provisional: ProvisionalH1,
    ) -> SupplyRevision<H1SupplyStatus> {
        {
            let mut state = cell.state.lock();
            state.reject_h1_match(match_id);
        }
        drop(provisional);
        let mut state = cell.state.lock();
        state.assert_consistent();
        state.current_h1_supply_revision()
    }

    /// Revalidates one match and commits its provisional sender for dispatch.
    pub(in crate::client::pool) fn commit_h1_match(
        cell: &Arc<Self>,
        match_id: H1MatchId,
        provisional: ProvisionalH1,
    ) -> Result<H1Selection, ProvisionalH1> {
        let (connection_cell, owner) = provisional.into_parts();
        let committed = cell.state.lock().commit_h1_match(match_id, &owner);
        if committed {
            Ok(H1Selection::new(cell, owner))
        } else {
            Err(ProvisionalH1::from_parts(connection_cell, owner))
        }
    }

    /// Closes a selected sender and records fairness only if close wins.
    pub(in crate::client::pool) fn reclaim_h1_candidate(
        cell: &Arc<Self>,
        match_id: H1MatchId,
        provisional: ProvisionalH1,
    ) -> Result<(SupplyRevision<H1SupplyStatus>, bool), ProvisionalH1> {
        let (connection_cell, owner) = provisional.into_parts();
        if !cell.state.lock().h1.names_peer_reservation(match_id) {
            return Err(ProvisionalH1::from_parts(connection_cell, owner));
        }

        let close_won = Self::retire_h1_owner(cell, owner, CloseReason::Reclaimed);
        let supply_update = {
            let mut state = cell.state.lock();
            let revision = state.complete_h1_match(match_id, close_won);
            state.assert_consistent();
            revision
        };
        Ok((supply_update, close_won))
    }

    /// Completes local match state after a requesting cell accepts the sender.
    pub(in crate::client::pool) fn complete_h1_match(
        &self,
        match_id: H1MatchId,
        transferred: bool,
    ) -> SupplyRevision<H1SupplyStatus> {
        self.state.lock().complete_h1_match(match_id, transferred)
    }

    /// Submits this cell's changed HTTP/1 supply when the origin is bounded.
    pub(super) fn submit_h1_supply_update(&self, revision: Option<SupplyRevision<H1SupplyStatus>>) {
        if let (Some(admission), Some(revision)) = (&self.admission, revision) {
            OriginAdmission::apply_h1_supply_revision(
                admission,
                self.id.partition(),
                self.eligibility_group.clone(),
                revision,
            );
        }
    }

    /// Returns a reusable sender to the oldest compatible waiter or idle set.
    ///
    /// Demand publication, task wakeup, and any rejected-result fallback all
    /// run after the cell lock is released. `owner` remains outside the locked
    /// scope so sender or connection drop cannot run while the cell guard is
    /// live.
    fn return_h1_owner(cell: &Arc<Self>, owner: OwnedH1Sender) {
        let connection_id = owner.id();
        let mut owner = Some(owner);
        let mut installation = None;
        let mut intercepted = None;
        let mut rejected_match = None;
        let idle_deadline = cell.idle_deadline();
        let should_retire = {
            let mut state = cell.state.lock();
            let returnable = state
                .h1
                .accepts_return(owner.as_ref().expect("HTTP/1 owner disappeared"));
            if !returnable {
                if let Some(match_id) = state.h1.take_return_match() {
                    let revision = state.complete_h1_match(match_id, false);
                    rejected_match = Some((match_id, revision));
                } else {
                    state.assert_consistent();
                }
                true
            } else if let Some(match_id) = state.h1.take_return_match() {
                if state
                    .h1
                    .reserve_for_peer(owner.as_ref().expect("HTTP/1 owner disappeared"))
                {
                    state.assert_consistent();
                    intercepted = Some((
                        match_id,
                        ProvisionalH1::new(cell, owner.take().expect("HTTP/1 owner disappeared")),
                    ));
                    false
                } else {
                    let revision = state.complete_h1_match(match_id, false);
                    rejected_match = Some((match_id, revision));
                    true
                }
            } else if state.acquisitions.has_h1_compatible_waiter()
                && state
                    .h1
                    .commit_return_to_waiter(owner.as_ref().expect("HTTP/1 owner disappeared"))
            {
                state.h1.consume_local_turn();
                state.assert_consistent();
                let mut returned = owner.take().expect("HTTP/1 owner disappeared");
                returned.mark_reused();
                let (waiter, mut install) = state.acquisitions.offer_returned_h1(
                    || AcquisitionOutcome::H1(H1Selection::new(cell, returned)),
                    &cell.eligibility_group,
                );
                state.h2.cancel_pending_waiter(
                    waiter.expect("compatible HTTP/1 waiter disappeared during offer"),
                );
                install.demand_updates =
                    state.publishable_demand_updates(std::mem::take(&mut install.demand_updates));
                installation = Some(install);
                false
            } else {
                let returned = state.h1.return_idle(
                    owner.take().expect("HTTP/1 owner disappeared"),
                    idle_deadline,
                );
                match returned {
                    Ok(()) => {
                        state.assert_consistent();
                        false
                    }
                    Err(returned) => {
                        owner = Some(returned);
                        true
                    }
                }
            }
        };

        if should_retire {
            if tracing::level_enabled!(tracing::Level::TRACE) {
                trace_h1_return(cell, connection_id, H1ReturnTrace::Rejected);
            }
            if let Some((match_id, revision)) = rejected_match {
                let admission = cell
                    .admission
                    .as_ref()
                    .expect("an H1 match requires bounded admission");
                OriginAdmission::reject_returned_h1_match(
                    admission,
                    match_id,
                    cell.id.partition(),
                    revision,
                );
            }
            Self::retire_h1_owner(
                cell,
                owner.take().expect("retired HTTP/1 owner disappeared"),
                CloseReason::ProtocolClosed,
            );
            return;
        }

        if let Some((match_id, provisional)) = intercepted {
            if tracing::level_enabled!(tracing::Level::TRACE) {
                trace_h1_return(
                    cell,
                    connection_id,
                    H1ReturnTrace::PeerReservationIntercepted,
                );
            }
            let admission = cell
                .admission
                .as_ref()
                .expect("an H1 match requires bounded admission");
            let candidate = H1Candidate::new(
                admission.clone(),
                match_id,
                cell.id.partition(),
                provisional,
            );
            let action = OriginAdmission::resolve_h1_match(admission, match_id, candidate);
            OriginAdmission::run_action_chain(action);
            return;
        }

        let Some(installation) = installation else {
            if tracing::level_enabled!(tracing::Level::TRACE) {
                trace_h1_return(cell, connection_id, H1ReturnTrace::Idle);
            }
            cell.notify_maintenance(idle_deadline);
            if cell.admission.is_some() {
                let revision = cell.state.lock().take_h1_supply_update();
                cell.submit_h1_supply_update(revision);
            }
            return;
        };
        if tracing::level_enabled!(tracing::Level::TRACE) {
            trace_h1_return(cell, connection_id, H1ReturnTrace::LocalDemand);
        }
        if let Some(admission) = &cell.admission {
            for snapshot in installation.demand_updates.into_iter().flatten() {
                OriginAdmission::submit_demand_snapshot(admission, cell.id.partition(), snapshot);
            }
        }
        drop(installation.returned_step);
        if let Some(waker) = installation.waker {
            waker.wake();
        }
        if cell.admission.is_some() {
            let revision = cell.state.lock().take_h1_supply_update();
            cell.submit_h1_supply_update(revision);
        }
    }

    /// Retires an externally owned H1 sender and removes its connection-owning cell record.
    ///
    /// Returns whether this path won the connection's logical-close race.
    fn retire_h1_owner(cell: &Arc<Self>, owner: OwnedH1Sender, reason: CloseReason) -> bool {
        let should_close = {
            let mut state = cell.state.lock();
            let should_close = state.h1.close_owned(&owner);
            state.assert_consistent();
            should_close
        };

        let id = owner.id();
        let won = should_close && owner.connection().logical_close(reason);
        drop(owner);

        let mut state = cell.state.lock();
        state.h1.remove_closed(id);
        state.assert_consistent();
        let revision = state.take_h1_supply_update();
        drop(state);
        cell.submit_h1_supply_update(revision);
        won
    }

    /// Begins close for an installed H1 record named without its sender.
    ///
    /// Returns whether this signal won the connection's logical-close race.
    pub(super) fn close_h1(cell: &Arc<Self>, id: ConnectionId, reason: CloseReason) -> bool {
        let Some((close, supply_update)) = ({
            let mut state = cell.state.lock();
            let close = state.h1.begin_close(id);
            state.assert_consistent();
            close.map(|close| (close, state.take_h1_supply_update()))
        }) else {
            return false;
        };

        cell.submit_h1_supply_update(supply_update);
        let remove_record = close.sender.is_some();
        let won = close.connection.logical_close(reason);
        drop(close.sender);
        if remove_record {
            let mut state = cell.state.lock();
            state.h1.remove_closed(id);
            state.assert_consistent();
        }
        won
    }

    /// Returns H1 record counts for focused ownership tests.
    #[cfg(test)]
    pub(super) fn h1_counts(&self) -> (usize, usize) {
        self.state.lock().h1.counts()
    }

    /// Returns the synthetic identities of every idle HTTP/1 sender.
    #[cfg(all(test, smithy_http_client_loom))]
    pub(super) fn h1_idle_sender_ids(&self) -> Vec<u64> {
        self.state.lock().h1.idle_sender_ids()
    }

    /// Returns the sole installed HTTP/1 connection for focused dispatch tests.
    #[cfg(all(test, feature = "rt-tokio"))]
    pub(in crate::client::pool) fn only_h1_connection_for_test(&self) -> Arc<ConnectionState> {
        self.state.lock().h1.only_connection_for_test()
    }
}

/// Terminal outcome for one reusable HTTP/1 sender return.
enum H1ReturnTrace {
    /// The owning cell no longer accepts the sender.
    Rejected,
    /// Origin admission reserved the sender for peer demand.
    PeerReservationIntercepted,
    /// No compatible demand exists, so the sender became idle.
    Idle,
    /// A waiter in the owning cell accepted the sender.
    LocalDemand,
}

/// Emits a committed HTTP/1 return outcome outside the cell lock.
// Keep field formatting out of a return transition that can synchronously
// enter admission and sender fallbacks.
#[inline(never)]
fn trace_h1_return(cell: &OriginCell, connection_id: ConnectionId, outcome: H1ReturnTrace) {
    match outcome {
        H1ReturnTrace::Rejected => tracing::trace!(
            connection_id = %connection_id,
            connection_partition = ?cell.id.partition(),
            origin_scheme = %cell.id.origin().scheme(),
            origin_host = cell.id.origin().host(),
            origin_port = ?cell.id.origin().port(),
            "HTTP/1 return was rejected by its connection-owning cell"
        ),
        H1ReturnTrace::PeerReservationIntercepted => tracing::trace!(
            connection_id = %connection_id,
            connection_partition = ?cell.id.partition(),
            origin_scheme = %cell.id.origin().scheme(),
            origin_host = cell.id.origin().host(),
            origin_port = ?cell.id.origin().port(),
            "HTTP/1 return was intercepted by a peer reservation"
        ),
        H1ReturnTrace::Idle => tracing::trace!(
            connection_id = %connection_id,
            connection_partition = ?cell.id.partition(),
            origin_scheme = %cell.id.origin().scheme(),
            origin_host = cell.id.origin().host(),
            origin_port = ?cell.id.origin().port(),
            "HTTP/1 connection returned to idle storage"
        ),
        H1ReturnTrace::LocalDemand => tracing::trace!(
            connection_id = %connection_id,
            request_partition = ?cell.id.partition(),
            connection_partition = ?cell.id.partition(),
            origin_scheme = %cell.id.origin().scheme(),
            origin_host = cell.id.origin().host(),
            origin_port = ?cell.id.origin().port(),
            "HTTP/1 return satisfied local demand"
        ),
    }
}

#[cfg(all(test, not(smithy_http_client_loom)))]
mod tests {
    use super::*;
    use crate::client::pool::connection::ConnectionInfo;
    use crate::client::pool::PartitionId;

    #[test]
    fn closing_a_selected_record_rejects_its_later_return() {
        let info = ConnectionInfo::for_test(ConnectionId::new(1), PartitionId::from_index(1));
        let (connection, _physical) = ConnectionState::unbounded(info);
        let mut records = H1CellState::default();
        let owner = records
            .insert_selected(connection, H1Sender::test(11))
            .expect("fresh HTTP/1 record was rejected");

        assert!(records.accepts_return(&owner));
        assert!(records.begin_close(owner.id()).is_some());
        assert!(!records.accepts_return(&owner));

        drop(owner);
        records.remove_closed(ConnectionId::new(1));
    }
    #[test]
    fn peer_reservation_distinguishes_waiting_resolution_and_completion() {
        let match_id = H1MatchId::for_test(1);
        let mut slot = H1Reservation::default();

        assert!(slot.reserve_waiting(match_id));
        assert!(!slot.reserve_waiting(H1MatchId::for_test(2)));
        assert_eq!(Some(match_id), slot.take_return_match());
        assert!(slot.complete_transfer(match_id, true));
        assert!(slot.local_turn_owed());
        assert!(slot.is_available());
    }

    #[test]
    fn rejection_does_not_manufacture_a_fairness_turn() {
        let match_id = H1MatchId::for_test(1);
        let mut slot = H1Reservation::default();

        assert!(slot.reserve_resolving(match_id));
        assert!(slot.release(match_id));
        assert!(!slot.local_turn_owed());
    }

    #[test]
    fn owed_turn_blocks_only_while_local_h1_demand_can_use_it() {
        let match_id = H1MatchId::for_test(1);
        let mut slot = H1Reservation::default();
        assert!(slot.reserve_resolving(match_id));
        assert!(slot.complete_transfer(match_id, true));

        assert!(slot.blocks_peer_selection(true));
        assert!(!slot.blocks_peer_selection(false));
        assert!(slot.clear_unused_turn(false));
        assert!(!slot.blocks_peer_selection(true));
    }
}