tk_pool/
error_log.rs

1//! ErrorLog trait and default implementations
2//!
3use std::fmt;
4use std::net::SocketAddr;
5use std::marker::PhantomData;
6
7use config::{NewErrorLog};
8
9/// A reason connection pool being shut down
10///
11/// This value is passed to ``ErrorLog::pool_shutting_down`` method.
12#[derive(Debug)]
13pub enum ShutdownReason {
14    /// Request stream is shutting down
15    ///
16    /// This usually means that all clones of ``queue::Pool`` object are
17    /// destroyed
18    RequestStreamClosed,
19    /// Address stream is closed
20    ///
21    /// Shutting down address stream is commonly used to force shut down
22    /// connection pool, even if there are users. Users will get an error
23    /// on the next `start_send`.
24    AddressStreamClosed,
25    #[doc(hidden)]
26    __Nonexhaustive,
27}
28
29
30/// The thrait that has appropriate hooks for logging different events
31///
32/// There is a default ``WarnLogger``, but the idea is that you may give
33/// connection pool a name, change logging levels, and do other interesting
34/// stuff in your own error log handler.
35pub trait ErrorLog {
36    /// Connection error type that is returned by connect/hanshake function
37    type ConnectionError;
38    /// Error when sending request returned by Sink
39    type SinkError;
40    /// Error when establishing a new connection
41    fn connection_error(&self, _addr: SocketAddr, _e: Self::ConnectionError) {}
42    /// Error when sending a request
43    ///
44    /// This also means connection is closed
45    fn sink_error(&self, _addr: SocketAddr, _e: Self::SinkError) {}
46    /// Pool is started to shut down for the specified reason
47    fn pool_shutting_down(&self, _reason: ShutdownReason) {}
48    /// Pool is fully closed at this moment
49    fn pool_closed(&self) {}
50}
51
52
53/// A constructor for a default error logger
54pub struct WarnLogger;
55
56/// An instance of default error logger
57pub struct WarnLoggerInstance<C, S>(PhantomData<* const (C, S)>);
58
59impl<C, S> Clone for WarnLoggerInstance<C, S> {
60    fn clone(&self) -> Self {
61        WarnLoggerInstance(PhantomData)
62    }
63}
64
65impl<C: fmt::Display, S: fmt::Display> NewErrorLog<C, S> for WarnLogger {
66    type ErrorLog = WarnLoggerInstance<C, S>;
67    fn construct(self) -> Self::ErrorLog {
68        WarnLoggerInstance(PhantomData)
69    }
70}
71
72impl<C, S> ErrorLog for WarnLoggerInstance<C, S>
73    where C: fmt::Display,
74          S: fmt::Display,
75{
76    type ConnectionError = C;
77    type SinkError = S;
78    fn connection_error(&self, addr: SocketAddr, e: Self::ConnectionError) {
79        warn!("Connecting to {} failed: {}", addr, e);
80    }
81    fn sink_error(&self, addr: SocketAddr, e: Self::SinkError) {
82        warn!("Connection to {} errored: {}", addr, e);
83    }
84    /// Starting to shut down pool
85    fn pool_shutting_down(&self, reason: ShutdownReason) {
86        warn!("Shutting down connection pool: {}", reason);
87    }
88    /// This is triggered when pool done all the work and shut down entirely
89    fn pool_closed(&self) {
90    }
91}
92
93impl fmt::Display for ShutdownReason {
94    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95        use self::ShutdownReason::*;
96        f.write_str(match *self {
97            RequestStreamClosed => "request stream closed",
98            AddressStreamClosed => "address stream closed",
99            __Nonexhaustive => unreachable!(),
100        })
101    }
102}