broker_tokio/time/error.rs
1use self::Kind::*;
2use std::error;
3use std::fmt;
4
5/// Errors encountered by the timer implementation.
6///
7/// Currently, there are two different errors that can occur:
8///
9/// * `shutdown` occurs when a timer operation is attempted, but the timer
10/// instance has been dropped. In this case, the operation will never be able
11/// to complete and the `shutdown` error is returned. This is a permanent
12/// error, i.e., once this error is observed, timer operations will never
13/// succeed in the future.
14///
15/// * `at_capacity` occurs when a timer operation is attempted, but the timer
16/// instance is currently handling its maximum number of outstanding delays.
17/// In this case, the operation is not able to be performed at the current
18/// moment, and `at_capacity` is returned. This is a transient error, i.e., at
19/// some point in the future, if the operation is attempted again, it might
20/// succeed. Callers that observe this error should attempt to [shed load]. One
21/// way to do this would be dropping the future that issued the timer operation.
22///
23/// [shed load]: https://en.wikipedia.org/wiki/Load_Shedding
24#[derive(Debug)]
25pub struct Error(Kind);
26
27#[derive(Debug)]
28enum Kind {
29 Shutdown,
30 AtCapacity,
31}
32
33impl Error {
34 /// Create an error representing a shutdown timer.
35 pub fn shutdown() -> Error {
36 Error(Shutdown)
37 }
38
39 /// Returns `true` if the error was caused by the timer being shutdown.
40 pub fn is_shutdown(&self) -> bool {
41 match self.0 {
42 Kind::Shutdown => true,
43 _ => false,
44 }
45 }
46
47 /// Create an error representing a timer at capacity.
48 pub fn at_capacity() -> Error {
49 Error(AtCapacity)
50 }
51
52 /// Returns `true` if the error was caused by the timer being at capacity.
53 pub fn is_at_capacity(&self) -> bool {
54 match self.0 {
55 Kind::AtCapacity => true,
56 _ => false,
57 }
58 }
59}
60
61impl error::Error for Error {}
62
63impl fmt::Display for Error {
64 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
65 use self::Kind::*;
66 let descr = match self.0 {
67 Shutdown => "timer is shutdown",
68 AtCapacity => "timer is at capacity and cannot create a new entry",
69 };
70 write!(fmt, "{}", descr)
71 }
72}