@@ -11,6 +11,9 @@ use crate::database::Database;
11
11
use crate :: type_info:: TypeInfo ;
12
12
use crate :: types:: Type ;
13
13
14
+ #[ cfg( doc) ]
15
+ use crate :: pool:: { PoolConnector , PoolOptions } ;
16
+
14
17
/// A specialized `Result` type for SQLx.
15
18
pub type Result < T , E = Error > = :: std:: result:: Result < T , E > ;
16
19
@@ -104,6 +107,19 @@ pub enum Error {
104
107
#[ error( "attempted to acquire a connection on a closed pool" ) ]
105
108
PoolClosed ,
106
109
110
+ /// A custom error that may be returned from a [`PoolConnector`] implementation.
111
+ #[ error( "error returned from pool connector" ) ]
112
+ PoolConnector {
113
+ #[ source]
114
+ source : BoxDynError ,
115
+
116
+ /// If `true`, `PoolConnector::connect()` is called again in an exponential backoff loop
117
+ /// up to [`PoolOptions::connect_timeout`].
118
+ ///
119
+ /// See [`PoolConnector::connect()`] for details.
120
+ retryable : bool ,
121
+ } ,
122
+
107
123
/// A background worker has crashed.
108
124
#[ error( "attempted to communicate with a crashed background worker" ) ]
109
125
WorkerCrashed ,
@@ -202,11 +218,6 @@ pub trait DatabaseError: 'static + Send + Sync + StdError {
202
218
#[ doc( hidden) ]
203
219
fn into_error ( self : Box < Self > ) -> Box < dyn StdError + Send + Sync + ' static > ;
204
220
205
- #[ doc( hidden) ]
206
- fn is_transient_in_connect_phase ( & self ) -> bool {
207
- false
208
- }
209
-
210
221
/// Returns the name of the constraint that triggered the error, if applicable.
211
222
/// If the error was caused by a conflict of a unique index, this will be the index name.
212
223
///
@@ -244,6 +255,24 @@ pub trait DatabaseError: 'static + Send + Sync + StdError {
244
255
fn is_check_violation ( & self ) -> bool {
245
256
matches ! ( self . kind( ) , ErrorKind :: CheckViolation )
246
257
}
258
+
259
+ /// Returns `true` if this error can be retried when connecting to the database.
260
+ ///
261
+ /// Defaults to `false`.
262
+ ///
263
+ /// For example, the Postgres driver overrides this to return `true` for the following error codes:
264
+ ///
265
+ /// * `53300 too_many_connections`: returned when the maximum connections are exceeded
266
+ /// on the server. Assumed to be the result of a temporary overcommit
267
+ /// (e.g. an extra application replica being spun up to replace one that is going down).
268
+ /// * This error being consistently logged or returned is a likely indicator of a misconfiguration;
269
+ /// the sum of [`PoolOptions::max_connections`] for all replicas should not exceed
270
+ /// the maximum connections allowed by the server.
271
+ /// * `57P03 cannot_connect_now`: returned when the database server is still starting up
272
+ /// and the tcop component is not ready to accept connections yet.
273
+ fn is_retryable_connect_error ( & self ) -> bool {
274
+ false
275
+ }
247
276
}
248
277
249
278
impl dyn DatabaseError {
0 commit comments