net-io 0.6.0

Network IO substrate for Rust: TCP, UDP and local-socket listeners, connection lifecycle, limits, graceful drain, PROXY protocol, and a TLS seam. Protocol-free and dependency-free.
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
//! Errors, close reasons, and the small enums they carry.

use std::fmt;
use std::io;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::time::Duration;

use crate::addr::AddrError;
use crate::driver::DriverKind;
use crate::handoff::HandoffError;
use crate::inherit::InheritError;
use crate::proxy::ProxyError;
use crate::tls::TlsError;

/// The one error type of the crate.
///
/// Every variant's `Display` says what happened and what to do about it.
///
/// # Examples
///
/// ```no_run
/// use net_io::{ErrorClass, NetError};
///
/// match net_io::connect("127.0.0.1:9") {
///     Ok(_) => {}
///     Err(NetError::ConnectionRefused { addr }) => eprintln!("nothing listens on {addr}"),
///     Err(e) if e.class() == ErrorClass::Timeout => eprintln!("slow network: {e}"),
///     Err(e) => return Err(e),
/// }
/// # Ok::<(), NetError>(())
/// ```
#[non_exhaustive]
#[derive(Debug)]
pub enum NetError {
    /// Binding failed for a reason without its own variant; `source` has the OS error.
    Bind {
        /// The address that could not be bound.
        addr: SocketAddr,
        /// The OS error.
        source: io::Error,
    },
    /// Another socket holds the address.
    AddrInUse {
        /// The address in use.
        addr: SocketAddr,
    },
    /// The address is not on this host, or IPv6 is disabled.
    AddrNotAvailable {
        /// The address that is not available.
        addr: SocketAddr,
    },
    /// The OS refused the operation for lack of permission.
    PermissionDenied {
        /// What was refused.
        what: &'static str,
    },
    /// Name resolution returned no address.
    NoAddresses,
    /// No answer from the peer within the connect deadline.
    ConnectTimeout {
        /// The address that did not answer.
        addr: SocketAddr,
        /// How long the attempt waited.
        after: Duration,
    },
    /// Nothing listens at the address.
    ConnectionRefused {
        /// The address that refused.
        addr: SocketAddr,
    },
    /// No route to the host.
    HostUnreachable {
        /// The unreachable address.
        addr: SocketAddr,
    },
    /// No route to the network.
    NetworkUnreachable {
        /// The unreachable address.
        addr: SocketAddr,
    },
    /// One of net-io's deadlines passed.
    TimedOut {
        /// Which deadline.
        kind: TimeoutKind,
    },
    /// The peer reset the connection.
    ConnectionReset,
    /// A write after the peer closed the connection.
    BrokenPipe,
    /// The connection was already closed by this side or the peer.
    Closed,
    /// A kill handle closed the connection.
    Killed {
        /// The caller's code.
        reason: KillReason,
    },
    /// The gate denied a write; the connection was reset.
    EgressDenied {
        /// The gate's code.
        code: u16,
    },
    /// The connection was refused.
    Refused {
        /// Why.
        cause: RefusalCause,
    },
    /// A configured bound was hit.
    Limit {
        /// Which bound.
        which: LimitKind,
    },
    /// The minimum transfer rate was not met.
    SlowPeer {
        /// In which direction.
        direction: Direction,
    },
    /// A builder or option value is out of range or inconsistent.
    InvalidConfig {
        /// The setter or field that holds the value.
        field: &'static str,
        /// The rule it broke.
        reason: ConfigReason,
    },
    /// The platform lacks the feature.
    Unsupported {
        /// The feature.
        feature: &'static str,
        /// The platform.
        platform: &'static str,
    },
    /// A Unix socket path exceeds the platform limit.
    PathTooLong {
        /// The limit in bytes, including the terminator.
        max: usize,
    },
    /// A live listener owns the socket path, or the path is not a socket.
    StaleSocketInUse {
        /// The path.
        path: PathBuf,
    },
    /// Another process created the named pipe first.
    PipeNameInUse {
        /// The pipe name.
        name: String,
    },
    /// No local endpoint with that name.
    NotFound,
    /// A file shrank while it was being sent.
    FileTruncated {
        /// Bytes sent before the file ended.
        sent: u64,
    },
    /// An address, CIDR block, or trusted-proxy list did not parse.
    Address(AddrError),
    /// A PROXY protocol header was invalid.
    Proxy(ProxyError),
    /// The TLS engine failed.
    Tls(TlsError),
    /// Inherited descriptors failed verification.
    Inherit(InheritError),
    /// A listener hand-off failed; nothing was adopted.
    Handoff(HandoffError),
    /// The selected driver could not start.
    DriverSetup {
        /// The driver that failed.
        driver: DriverKind,
        /// The OS error.
        source: io::Error,
    },
    /// The operation needs the door open.
    DoorClosed,
    /// The server is draining or stopped.
    ShuttingDown,
    /// A server was used in a process created by `fork` after it was built.
    ForkedChild,
    /// Any other OS failure.
    Os {
        /// The operation that failed.
        op: &'static str,
        /// The OS error.
        source: io::Error,
    },
}

/// The broad kind of a [`NetError`], for policy decisions.
///
/// # Examples
///
/// ```
/// use net_io::{ErrorClass, NetError};
/// assert_eq!(NetError::ConnectionReset.class(), ErrorClass::Peer);
/// ```
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ErrorClass {
    /// Fix the configuration.
    Config,
    /// Descriptors, memory, or buffers ran out; retry later.
    Resource,
    /// The other side misbehaved or left.
    Peer,
    /// A deadline passed.
    Timeout,
    /// The gate, a limit, or a refusal stopped it.
    Policy,
    /// The platform lacks the feature.
    Platform,
    /// A contract was broken by the caller.
    Bug,
}

impl NetError {
    /// The broad kind of this error.
    ///
    /// ```
    /// use net_io::{ErrorClass, NetError, TimeoutKind};
    /// assert_eq!(NetError::TimedOut { kind: TimeoutKind::Read }.class(), ErrorClass::Timeout);
    /// ```
    #[must_use]
    pub fn class(&self) -> ErrorClass {
        match self {
            NetError::InvalidConfig { .. }
            | NetError::Address(_)
            | NetError::AddrInUse { .. }
            | NetError::AddrNotAvailable { .. }
            | NetError::PermissionDenied { .. }
            | NetError::NoAddresses
            | NetError::PathTooLong { .. }
            | NetError::StaleSocketInUse { .. }
            | NetError::PipeNameInUse { .. }
            | NetError::NotFound
            | NetError::Bind { .. } => ErrorClass::Config,
            NetError::Limit { which } => match which {
                LimitKind::BufferBudget | LimitKind::Descriptors | LimitKind::CompletionQueue => {
                    ErrorClass::Resource
                }
                _ => ErrorClass::Policy,
            },
            NetError::ConnectTimeout { .. } | NetError::TimedOut { .. } => ErrorClass::Timeout,
            NetError::ConnectionRefused { .. }
            | NetError::HostUnreachable { .. }
            | NetError::NetworkUnreachable { .. }
            | NetError::ConnectionReset
            | NetError::BrokenPipe
            | NetError::Closed
            | NetError::FileTruncated { .. }
            | NetError::Proxy(_)
            | NetError::Tls(_)
            | NetError::SlowPeer { .. } => ErrorClass::Peer,
            NetError::Killed { .. }
            | NetError::EgressDenied { .. }
            | NetError::Refused { .. }
            | NetError::DoorClosed
            | NetError::ShuttingDown => ErrorClass::Policy,
            NetError::Unsupported { .. } | NetError::DriverSetup { .. } => ErrorClass::Platform,
            NetError::ForkedChild => ErrorClass::Bug,
            NetError::Inherit(_) | NetError::Handoff(_) => ErrorClass::Config,
            NetError::Os { source, .. } => class_of_io(source),
        }
    }

    /// Whether retrying the same operation later can succeed.
    ///
    /// ```
    /// use net_io::NetError;
    /// assert!(!NetError::ConnectionReset.is_retryable());
    /// ```
    #[must_use]
    pub fn is_retryable(&self) -> bool {
        matches!(
            self,
            NetError::ConnectTimeout { .. }
                | NetError::TimedOut {
                    kind: TimeoutKind::Connect
                }
        ) || self.class() == ErrorClass::Resource
    }

    /// The raw OS error number, when the error came from the OS.
    ///
    /// ```
    /// use net_io::NetError;
    /// assert_eq!(NetError::Closed.os_error_code(), None);
    /// ```
    #[must_use]
    pub fn os_error_code(&self) -> Option<i32> {
        match self {
            NetError::Bind { source, .. }
            | NetError::DriverSetup { source, .. }
            | NetError::Os { source, .. } => source.raw_os_error(),
            _ => None,
        }
    }

    /// Builds the error for a failed OS call, mapping the common kinds to their own variants.
    pub(crate) fn from_os(op: &'static str, source: io::Error) -> NetError {
        match source.kind() {
            io::ErrorKind::ConnectionReset | io::ErrorKind::ConnectionAborted => {
                NetError::ConnectionReset
            }
            io::ErrorKind::BrokenPipe => NetError::BrokenPipe,
            io::ErrorKind::NotConnected => NetError::Closed,
            _ => NetError::Os { op, source },
        }
    }
}

fn class_of_io(e: &io::Error) -> ErrorClass {
    match e.kind() {
        io::ErrorKind::TimedOut | io::ErrorKind::WouldBlock => ErrorClass::Timeout,
        io::ErrorKind::OutOfMemory => ErrorClass::Resource,
        io::ErrorKind::PermissionDenied | io::ErrorKind::InvalidInput => ErrorClass::Config,
        io::ErrorKind::Unsupported => ErrorClass::Platform,
        _ => ErrorClass::Peer,
    }
}

impl fmt::Display for NetError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            NetError::Bind { addr, source } => write!(f, "could not bind {addr}: {source}"),
            NetError::AddrInUse { addr } => write!(
                f,
                "another socket is listening on {addr}; stop it or choose another port (on restart, use listener hand-off)"
            ),
            NetError::AddrNotAvailable { addr } => write!(
                f,
                "{addr} is not an address of this host (or IPv6 is disabled); bind an address this host owns"
            ),
            NetError::PermissionDenied { what } => write!(
                f,
                "permission denied: {what}; ports below 1024 need privileges: bind before dropping them, or use a high port behind a reverse proxy"
            ),
            NetError::NoAddresses => {
                f.write_str("name resolution returned no address; check the host name")
            }
            NetError::ConnectTimeout { addr, after } => write!(
                f,
                "no answer from {addr} within {after:?}; check reachability or raise the connect timeout"
            ),
            NetError::ConnectionRefused { addr } => {
                write!(f, "{addr} refused the connection; nothing listens there")
            }
            NetError::HostUnreachable { addr } => write!(f, "no route to host {addr}"),
            NetError::NetworkUnreachable { addr } => write!(f, "no route to the network of {addr}"),
            NetError::TimedOut { kind } => write!(
                f,
                "the {} deadline passed; the peer is too slow, or the limit is too low for this workload",
                kind.as_str()
            ),
            NetError::ConnectionReset => f.write_str("the peer reset the connection"),
            NetError::BrokenPipe => f.write_str("write after the peer closed the connection"),
            NetError::Closed => f.write_str("the connection is already closed"),
            NetError::Killed { reason } => {
                write!(f, "the connection was killed (code {})", reason.0)
            }
            NetError::EgressDenied { code } => {
                write!(
                    f,
                    "the gate denied a write (code {code}); the connection was reset"
                )
            }
            NetError::Refused { cause } => {
                write!(f, "the connection was refused: {}", cause.as_str())
            }
            NetError::Limit { which } => {
                write!(
                    f,
                    "the {} limit was reached; raise it or shed load",
                    which.as_str()
                )
            }
            NetError::SlowPeer { direction } => write!(
                f,
                "the peer did not reach the minimum transfer rate ({})",
                direction.as_str()
            ),
            NetError::InvalidConfig { field, reason } => {
                write!(f, "invalid configuration for `{field}`: {reason}")
            }
            NetError::Unsupported { feature, platform } => {
                write!(f, "{feature} is not supported on {platform}")
            }
            NetError::PathTooLong { max } => write!(
                f,
                "the socket path is longer than the platform allows ({max} bytes including the terminator); use a shorter path"
            ),
            NetError::StaleSocketInUse { path } => write!(
                f,
                "{} is in use by a live listener or is not a socket; stop that listener or choose another path",
                path.display()
            ),
            NetError::PipeNameInUse { name } => write!(
                f,
                "another process created the pipe {name} first (possible squatting); choose another name or find that process"
            ),
            NetError::NotFound => {
                f.write_str("no local endpoint with that name; start the server first")
            }
            NetError::FileTruncated { sent } => write!(
                f,
                "the file ended after {sent} bytes while it was being sent; the connection was reset"
            ),
            NetError::Address(e) => write!(f, "{e}"),
            NetError::Proxy(e) => write!(f, "PROXY protocol: {e}"),
            NetError::Tls(e) => write!(f, "TLS: {e}"),
            NetError::Inherit(e) => write!(f, "inherited listeners: {e}"),
            NetError::Handoff(e) => write!(f, "listener hand-off: {e}"),
            NetError::DriverSetup { driver, source } => write!(
                f,
                "the {driver:?} driver could not start: {source}; use DriverKind::Auto or the blocking driver"
            ),
            NetError::DoorClosed => f.write_str("the door is closed; call open_door first"),
            NetError::ShuttingDown => f.write_str("the server is draining or stopped"),
            NetError::ForkedChild => f.write_str(
                "a server was used in a child process after fork; create servers after forking",
            ),
            NetError::Os { op, source } => write!(f, "{op} failed: {source}"),
        }
    }
}

impl std::error::Error for NetError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            NetError::Bind { source, .. }
            | NetError::DriverSetup { source, .. }
            | NetError::Os { source, .. } => Some(source),
            NetError::Address(e) => Some(e),
            NetError::Proxy(e) => Some(e),
            NetError::Tls(e) => Some(e),
            NetError::Inherit(e) => Some(e),
            NetError::Handoff(e) => Some(e),
            _ => None,
        }
    }
}

impl From<io::Error> for NetError {
    fn from(e: io::Error) -> NetError {
        match e.kind() {
            io::ErrorKind::TimedOut | io::ErrorKind::WouldBlock => NetError::TimedOut {
                kind: TimeoutKind::Read,
            },
            _ => NetError::from_os("io", e),
        }
    }
}

impl From<AddrError> for NetError {
    fn from(e: AddrError) -> NetError {
        NetError::Address(e)
    }
}

impl From<NetError> for io::Error {
    fn from(e: NetError) -> io::Error {
        let kind = match &e {
            NetError::TimedOut { .. }
            | NetError::ConnectTimeout { .. }
            | NetError::SlowPeer { .. } => io::ErrorKind::TimedOut,
            NetError::ConnectionReset | NetError::Killed { .. } | NetError::EgressDenied { .. } => {
                io::ErrorKind::ConnectionReset
            }
            NetError::BrokenPipe => io::ErrorKind::BrokenPipe,
            NetError::Closed => io::ErrorKind::NotConnected,
            NetError::ConnectionRefused { .. } => io::ErrorKind::ConnectionRefused,
            NetError::AddrInUse { .. } => io::ErrorKind::AddrInUse,
            NetError::AddrNotAvailable { .. } => io::ErrorKind::AddrNotAvailable,
            NetError::PermissionDenied { .. } => io::ErrorKind::PermissionDenied,
            NetError::NotFound => io::ErrorKind::NotFound,
            NetError::Unsupported { .. } => io::ErrorKind::Unsupported,
            NetError::InvalidConfig { .. } | NetError::Address(_) => io::ErrorKind::InvalidInput,
            _ => io::ErrorKind::Other,
        };
        io::Error::new(kind, e)
    }
}

/// Why a connection closed. Every connection ends with exactly one.
///
/// # Examples
///
/// ```
/// use net_io::{CloseReason, Direction};
/// let reason = CloseReason::SlowPeer { direction: Direction::In };
/// assert_eq!(reason.as_str(), "slow_peer_in");
/// assert!(reason.is_abortive());
/// assert!(!CloseReason::PeerClosed.is_abortive());
/// ```
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CloseReason {
    /// The peer closed its side and the session closed.
    PeerClosed,
    /// The peer sent RST.
    PeerReset,
    /// No exchange started within the idle timeout.
    Idle,
    /// The PROXY header and TLS handshake did not finish within the handshake timeout.
    HandshakeTimeout,
    /// No progress within the read timeout.
    ReadTimeout,
    /// No progress within the write timeout.
    WriteTimeout,
    /// The session's deadline for the current exchange passed.
    ExchangeDeadline,
    /// A parked connection was not woken in time.
    ParkDeadline,
    /// The minimum transfer rate was not met.
    SlowPeer {
        /// In which direction.
        direction: Direction,
    },
    /// Refused at accept or by the session factory.
    Refused(RefusalCause),
    /// A PROXY-required listener got a connection from an untrusted peer.
    ProxyUntrusted,
    /// A trusted peer sent no PROXY header.
    ProxyMissing,
    /// The PROXY header was malformed or too large.
    ProxyInvalid,
    /// The TLS engine failed.
    TlsFailed,
    /// The TLS engine's hello hook refused the ClientHello.
    HelloRefused,
    /// A kill handle closed the connection.
    Killed(KillReason),
    /// The gate denied a write.
    EgressDenied(u16),
    /// The session finished after drain started.
    Drained,
    /// The session had not finished when the drain deadline passed.
    DrainDeadline,
    /// The control line shed this lane.
    Shed,
    /// The control line said `Stopping`, or the server shut down.
    Shutdown,
    /// The session returned `Step::Close`.
    SessionClosed,
    /// The session returned `Step::Abort`, or a handler returned an error.
    SessionError,
    /// The session panicked.
    SessionPanicked,
    /// A per-connection bound was hit.
    Limit(LimitKind),
    /// The OS reported an error not covered above.
    Io(OsErrorClass),
}

impl CloseReason {
    /// One value per counter slot, inner values at their first or zero value, in
    /// `index` order (for iterating counters).
    pub(crate) const BY_INDEX: [CloseReason; CloseReason::COUNT] = [
        CloseReason::PeerClosed,
        CloseReason::PeerReset,
        CloseReason::Idle,
        CloseReason::HandshakeTimeout,
        CloseReason::ReadTimeout,
        CloseReason::WriteTimeout,
        CloseReason::ExchangeDeadline,
        CloseReason::ParkDeadline,
        CloseReason::SlowPeer {
            direction: Direction::In,
        },
        CloseReason::Refused(RefusalCause::GlobalCap),
        CloseReason::ProxyUntrusted,
        CloseReason::ProxyMissing,
        CloseReason::ProxyInvalid,
        CloseReason::TlsFailed,
        CloseReason::HelloRefused,
        CloseReason::Killed(KillReason(0)),
        CloseReason::EgressDenied(0),
        CloseReason::Drained,
        CloseReason::DrainDeadline,
        CloseReason::Shed,
        CloseReason::Shutdown,
        CloseReason::SessionClosed,
        CloseReason::SessionError,
        CloseReason::SessionPanicked,
        CloseReason::Limit(LimitKind::Connections),
        CloseReason::Io(OsErrorClass::Other(0)),
    ];

    /// A stable snake_case name for logs.
    ///
    /// ```
    /// assert_eq!(net_io::CloseReason::Idle.as_str(), "idle");
    /// ```
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            CloseReason::PeerClosed => "peer_closed",
            CloseReason::PeerReset => "peer_reset",
            CloseReason::Idle => "idle",
            CloseReason::HandshakeTimeout => "handshake_timeout",
            CloseReason::ReadTimeout => "read_timeout",
            CloseReason::WriteTimeout => "write_timeout",
            CloseReason::ExchangeDeadline => "exchange_deadline",
            CloseReason::ParkDeadline => "park_deadline",
            CloseReason::SlowPeer {
                direction: Direction::In,
            } => "slow_peer_in",
            CloseReason::SlowPeer {
                direction: Direction::Out,
            } => "slow_peer_out",
            CloseReason::Refused(_) => "refused",
            CloseReason::ProxyUntrusted => "proxy_untrusted",
            CloseReason::ProxyMissing => "proxy_missing",
            CloseReason::ProxyInvalid => "proxy_invalid",
            CloseReason::TlsFailed => "tls_failed",
            CloseReason::HelloRefused => "hello_refused",
            CloseReason::Killed(_) => "killed",
            CloseReason::EgressDenied(_) => "egress_denied",
            CloseReason::Drained => "drained",
            CloseReason::DrainDeadline => "drain_deadline",
            CloseReason::Shed => "shed",
            CloseReason::Shutdown => "shutdown",
            CloseReason::SessionClosed => "session_closed",
            CloseReason::SessionError => "session_error",
            CloseReason::SessionPanicked => "session_panicked",
            CloseReason::Limit(_) => "limit",
            CloseReason::Io(_) => "io",
        }
    }

    /// Whether the connection is closed with RST (`true`) or gracefully with FIN (`false`).
    ///
    /// ```
    /// assert!(net_io::CloseReason::ReadTimeout.is_abortive());
    /// ```
    #[must_use]
    pub fn is_abortive(&self) -> bool {
        !matches!(
            self,
            CloseReason::PeerClosed
                | CloseReason::Idle
                | CloseReason::Drained
                | CloseReason::SessionClosed
        )
    }

    /// An index per variant for per-reason counters (inner values ignored).
    pub(crate) fn index(&self) -> usize {
        match self {
            CloseReason::PeerClosed => 0,
            CloseReason::PeerReset => 1,
            CloseReason::Idle => 2,
            CloseReason::HandshakeTimeout => 3,
            CloseReason::ReadTimeout => 4,
            CloseReason::WriteTimeout => 5,
            CloseReason::ExchangeDeadline => 6,
            CloseReason::ParkDeadline => 7,
            CloseReason::SlowPeer { .. } => 8,
            CloseReason::Refused(_) => 9,
            CloseReason::ProxyUntrusted => 10,
            CloseReason::ProxyMissing => 11,
            CloseReason::ProxyInvalid => 12,
            CloseReason::TlsFailed => 13,
            CloseReason::HelloRefused => 14,
            CloseReason::Killed(_) => 15,
            CloseReason::EgressDenied(_) => 16,
            CloseReason::Drained => 17,
            CloseReason::DrainDeadline => 18,
            CloseReason::Shed => 19,
            CloseReason::Shutdown => 20,
            CloseReason::SessionClosed => 21,
            CloseReason::SessionError => 22,
            CloseReason::SessionPanicked => 23,
            CloseReason::Limit(_) => 24,
            CloseReason::Io(_) => 25,
        }
    }

    /// The number of distinct indexes [`CloseReason::index`] returns.
    pub(crate) const COUNT: usize = 26;

    /// The close reason for a timeout of this kind.
    pub(crate) fn for_timeout(kind: TimeoutKind) -> CloseReason {
        match kind {
            TimeoutKind::Handshake => CloseReason::HandshakeTimeout,
            TimeoutKind::Idle => CloseReason::Idle,
            TimeoutKind::Read | TimeoutKind::Connect => CloseReason::ReadTimeout,
            TimeoutKind::Write => CloseReason::WriteTimeout,
            TimeoutKind::Exchange => CloseReason::ExchangeDeadline,
            TimeoutKind::Park => CloseReason::ParkDeadline,
            TimeoutKind::Drain => CloseReason::DrainDeadline,
        }
    }
}

impl fmt::Display for CloseReason {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Why a connection was refused.
///
/// ```
/// assert_eq!(net_io::RefusalCause::SourceCap.as_str(), "source_cap");
/// ```
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RefusalCause {
    /// The global connection cap was reached.
    GlobalCap,
    /// The listener's connection cap was reached.
    ListenerCap,
    /// The per-source concurrency cap was reached.
    SourceCap,
    /// The lane's reservation was used up.
    LaneReserve,
    /// The gate refused, with its code.
    Gate(u16),
    /// The session factory refused.
    Session,
    /// The blocking driver had no free worker.
    Busy,
    /// The door was closed.
    DoorClosed,
    /// The control line shed this lane.
    Shed,
    /// The control line is in lockdown and the listener does not allow it.
    Lockdown,
    /// A local socket peer's user id is not the one the listener requires
    /// (`LocalOptions::require_uid`).
    Credentials,
}

impl RefusalCause {
    /// One value per counter slot in `index` order (`Gate` carries code 0).
    pub(crate) const BY_INDEX: [RefusalCause; RefusalCause::COUNT] = [
        RefusalCause::GlobalCap,
        RefusalCause::ListenerCap,
        RefusalCause::SourceCap,
        RefusalCause::LaneReserve,
        RefusalCause::Gate(0),
        RefusalCause::Session,
        RefusalCause::Busy,
        RefusalCause::DoorClosed,
        RefusalCause::Shed,
        RefusalCause::Lockdown,
        RefusalCause::Credentials,
    ];

    /// A stable snake_case name for logs.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            RefusalCause::GlobalCap => "global_cap",
            RefusalCause::ListenerCap => "listener_cap",
            RefusalCause::SourceCap => "source_cap",
            RefusalCause::LaneReserve => "lane_reserve",
            RefusalCause::Gate(_) => "gate",
            RefusalCause::Session => "session",
            RefusalCause::Busy => "busy",
            RefusalCause::DoorClosed => "door_closed",
            RefusalCause::Shed => "shed",
            RefusalCause::Lockdown => "lockdown",
            RefusalCause::Credentials => "credentials",
        }
    }

    pub(crate) fn index(&self) -> usize {
        match self {
            RefusalCause::GlobalCap => 0,
            RefusalCause::ListenerCap => 1,
            RefusalCause::SourceCap => 2,
            RefusalCause::LaneReserve => 3,
            RefusalCause::Gate(_) => 4,
            RefusalCause::Session => 5,
            RefusalCause::Busy => 6,
            RefusalCause::DoorClosed => 7,
            RefusalCause::Shed => 8,
            RefusalCause::Lockdown => 9,
            RefusalCause::Credentials => 10,
        }
    }

    pub(crate) const COUNT: usize = 11;
}

/// Which configured bound was hit.
///
/// ```
/// assert_eq!(net_io::LimitKind::OutputBudget.as_str(), "output_budget");
/// ```
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum LimitKind {
    /// The global connection cap.
    Connections,
    /// A listener's connection cap.
    PerListener,
    /// The per-source concurrency cap.
    PerSource,
    /// The total buffer budget.
    BufferBudget,
    /// A connection's output budget.
    OutputBudget,
    /// A worker's completion queue.
    CompletionQueue,
    /// The PROXY header size.
    ProxyHeader,
    /// The accept batch.
    AcceptBatch,
    /// A connection's read buffer (full and not consumed).
    ReadBuffer,
    /// The UDP amplification guard.
    Amplification,
    /// A UDP source budget.
    SourceBudget,
    /// The process's descriptor limit.
    Descriptors,
}

impl LimitKind {
    /// A stable snake_case name for logs.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            LimitKind::Connections => "connections",
            LimitKind::PerListener => "per_listener",
            LimitKind::PerSource => "per_source",
            LimitKind::BufferBudget => "buffer_budget",
            LimitKind::OutputBudget => "output_budget",
            LimitKind::CompletionQueue => "completion_queue",
            LimitKind::ProxyHeader => "proxy_header",
            LimitKind::AcceptBatch => "accept_batch",
            LimitKind::ReadBuffer => "read_buffer",
            LimitKind::Amplification => "amplification",
            LimitKind::SourceBudget => "source_budget",
            LimitKind::Descriptors => "descriptors",
        }
    }
}

/// Which deadline passed.
///
/// ```
/// assert_eq!(net_io::TimeoutKind::Idle.as_str(), "idle");
/// ```
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TimeoutKind {
    /// The handshake (PROXY and TLS).
    Handshake,
    /// Waiting for the next exchange.
    Idle,
    /// Waiting for bytes during an exchange.
    Read,
    /// Waiting for the peer to take bytes.
    Write,
    /// The session's total deadline for the exchange.
    Exchange,
    /// A parked connection.
    Park,
    /// An outbound connect.
    Connect,
    /// A drain.
    Drain,
}

impl TimeoutKind {
    /// A stable snake_case name for logs.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            TimeoutKind::Handshake => "handshake",
            TimeoutKind::Idle => "idle",
            TimeoutKind::Read => "read",
            TimeoutKind::Write => "write",
            TimeoutKind::Exchange => "exchange",
            TimeoutKind::Park => "park",
            TimeoutKind::Connect => "connect",
            TimeoutKind::Drain => "drain",
        }
    }
}

/// A caller-defined code for a kill.
///
/// ```
/// let reason = net_io::KillReason(7);
/// assert_eq!(reason.0, 7);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct KillReason(pub u16);

/// A direction of transfer.
///
/// ```
/// assert_eq!(net_io::Direction::In.as_str(), "in");
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Direction {
    /// From the peer to us.
    In,
    /// From us to the peer.
    Out,
}

impl Direction {
    /// `"in"` or `"out"`.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Direction::In => "in",
            Direction::Out => "out",
        }
    }
}

/// The class of an OS error, without its message text.
///
/// ```
/// use net_io::OsErrorClass;
/// let class = OsErrorClass::from_io(&std::io::Error::from(std::io::ErrorKind::ConnectionReset));
/// assert_eq!(class, OsErrorClass::Reset);
/// ```
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum OsErrorClass {
    /// The peer reset the connection.
    Reset,
    /// The connection was aborted locally.
    Aborted,
    /// The OS timed the connection out (for example failed keepalive probes).
    TimedOut,
    /// The connection was refused.
    Refused,
    /// No route.
    Unreachable,
    /// A file ended while it was being sent.
    FileTruncated,
    /// Any other OS error, by number.
    Other(i32),
}

impl OsErrorClass {
    /// Classifies an `io::Error`.
    #[must_use]
    pub fn from_io(e: &io::Error) -> OsErrorClass {
        match e.kind() {
            io::ErrorKind::ConnectionReset | io::ErrorKind::BrokenPipe => OsErrorClass::Reset,
            io::ErrorKind::ConnectionAborted => OsErrorClass::Aborted,
            io::ErrorKind::TimedOut => OsErrorClass::TimedOut,
            io::ErrorKind::ConnectionRefused => OsErrorClass::Refused,
            io::ErrorKind::HostUnreachable | io::ErrorKind::NetworkUnreachable => {
                OsErrorClass::Unreachable
            }
            _ => OsErrorClass::Other(e.raw_os_error().unwrap_or(0)),
        }
    }
}

/// The rule a configuration value broke.
///
/// ```
/// use net_io::ConfigReason;
/// assert_eq!(ConfigReason::Zero.to_string(), "zero is not allowed here");
/// ```
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ConfigReason {
    /// Zero where zero is meaningless.
    Zero,
    /// Above the maximum.
    TooLarge {
        /// The maximum.
        max: u64,
    },
    /// Below the minimum.
    TooSmall {
        /// The minimum.
        min: u64,
    },
    /// A per-listener or per-source cap above the global cap.
    AboveGlobal,
    /// A deadline shorter than one timer tick.
    BelowTick,
    /// The minimum-rate window is shorter than its check interval.
    WindowBelowCheck,
    /// The buffer budget is below workers times the largest class times two.
    BudgetBelowWorkers {
        /// The smallest valid budget in bytes.
        need: u64,
    },
    /// The caps exceed the process's descriptor limit.
    DescriptorLimit {
        /// The descriptor limit.
        have: u64,
        /// Descriptors the configuration needs.
        need: u64,
    },
    /// A required part is not set.
    Missing,
    /// An adopted socket is not a listening socket.
    NotListening,
    /// Values that contradict each other.
    Inconsistent,
    /// A byte or character the value cannot contain (a NUL in a socket path).
    Malformed,
}

impl fmt::Display for ConfigReason {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ConfigReason::Zero => f.write_str("zero is not allowed here"),
            ConfigReason::TooLarge { max } => write!(f, "above the maximum of {max}"),
            ConfigReason::TooSmall { min } => write!(f, "below the minimum of {min}"),
            ConfigReason::AboveGlobal => f.write_str("above the global connection cap"),
            ConfigReason::BelowTick => f.write_str("shorter than one timer tick"),
            ConfigReason::WindowBelowCheck => {
                f.write_str("the minimum-rate window is shorter than its check interval")
            }
            ConfigReason::BudgetBelowWorkers { need } => write!(
                f,
                "the buffer budget must be at least {need} bytes (workers x largest class x 2)"
            ),
            ConfigReason::DescriptorLimit { have, need } => write!(
                f,
                "needs {need} descriptors but the process limit is {have}; raise the limit or lower the caps"
            ),
            ConfigReason::Missing => f.write_str("a required part is not set"),
            ConfigReason::NotListening => {
                f.write_str("the socket is not a listening stream socket")
            }
            ConfigReason::Inconsistent => f.write_str("values contradict each other"),
            ConfigReason::Malformed => f.write_str("contains a byte or character it cannot"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_by_index_tables_match_index() {
        for (i, r) in CloseReason::BY_INDEX.iter().enumerate() {
            assert_eq!(r.index(), i, "{r:?}");
        }
        for (i, c) in RefusalCause::BY_INDEX.iter().enumerate() {
            assert_eq!(c.index(), i, "{c:?}");
        }
    }

    #[test]
    fn test_close_reason_index_covers_every_variant_once() {
        let all = [
            CloseReason::PeerClosed,
            CloseReason::PeerReset,
            CloseReason::Idle,
            CloseReason::HandshakeTimeout,
            CloseReason::ReadTimeout,
            CloseReason::WriteTimeout,
            CloseReason::ExchangeDeadline,
            CloseReason::ParkDeadline,
            CloseReason::SlowPeer {
                direction: Direction::Out,
            },
            CloseReason::Refused(RefusalCause::Busy),
            CloseReason::ProxyUntrusted,
            CloseReason::ProxyMissing,
            CloseReason::ProxyInvalid,
            CloseReason::TlsFailed,
            CloseReason::HelloRefused,
            CloseReason::Killed(KillReason(1)),
            CloseReason::EgressDenied(1),
            CloseReason::Drained,
            CloseReason::DrainDeadline,
            CloseReason::Shed,
            CloseReason::Shutdown,
            CloseReason::SessionClosed,
            CloseReason::SessionError,
            CloseReason::SessionPanicked,
            CloseReason::Limit(LimitKind::ReadBuffer),
            CloseReason::Io(OsErrorClass::Reset),
        ];
        let mut seen = [false; CloseReason::COUNT];
        for r in all {
            assert!(!seen[r.index()], "duplicate index for {r:?}");
            seen[r.index()] = true;
        }
        assert!(seen.iter().all(|s| *s));
    }

    #[test]
    fn test_close_reason_size_is_eight_bytes() {
        assert!(std::mem::size_of::<CloseReason>() <= 8);
    }

    #[test]
    fn test_close_style_graceful_reasons_are_not_abortive() {
        for r in [
            CloseReason::PeerClosed,
            CloseReason::Idle,
            CloseReason::Drained,
            CloseReason::SessionClosed,
        ] {
            assert!(!r.is_abortive());
        }
        assert!(CloseReason::DrainDeadline.is_abortive());
        assert!(CloseReason::Killed(KillReason(0)).is_abortive());
    }

    #[test]
    fn test_from_io_timed_out_maps_to_read_timeout() {
        let e: NetError = io::Error::from(io::ErrorKind::TimedOut).into();
        assert!(matches!(
            e,
            NetError::TimedOut {
                kind: TimeoutKind::Read
            }
        ));
    }

    #[test]
    fn test_from_io_reset_maps_to_connection_reset() {
        let e: NetError = io::Error::from(io::ErrorKind::ConnectionReset).into();
        assert!(matches!(e, NetError::ConnectionReset));
        assert_eq!(e.class(), ErrorClass::Peer);
    }

    #[test]
    fn test_into_io_keeps_kind_and_source() {
        let io_err: io::Error = NetError::BrokenPipe.into();
        assert_eq!(io_err.kind(), io::ErrorKind::BrokenPipe);
        assert!(io_err.get_ref().is_some());
    }

    #[test]
    fn test_retryable_connect_timeout_true_reset_false() {
        let addr: SocketAddr = "127.0.0.1:1".parse().unwrap();
        assert!(
            NetError::ConnectTimeout {
                addr,
                after: Duration::from_secs(1)
            }
            .is_retryable()
        );
        assert!(!NetError::ConnectionReset.is_retryable());
        assert!(
            NetError::Limit {
                which: LimitKind::BufferBudget
            }
            .is_retryable()
        );
    }

    #[test]
    fn test_display_says_what_to_do() {
        let addr: SocketAddr = "0.0.0.0:443".parse().unwrap();
        let text = NetError::AddrInUse { addr }.to_string();
        assert!(text.contains("0.0.0.0:443"));
        assert!(text.contains("hand-off"));
    }

    #[test]
    fn test_os_error_code_present_only_for_os_errors() {
        let e = NetError::Os {
            op: "x",
            source: io::Error::from_raw_os_error(5),
        };
        assert_eq!(e.os_error_code(), Some(5));
        assert_eq!(NetError::NotFound.os_error_code(), None);
    }

    #[test]
    fn test_timeout_kind_maps_to_close_reason() {
        assert_eq!(
            CloseReason::for_timeout(TimeoutKind::Idle),
            CloseReason::Idle
        );
        assert_eq!(
            CloseReason::for_timeout(TimeoutKind::Write),
            CloseReason::WriteTimeout
        );
    }
}