2
2
//!
3
3
//! Requires the `runtime` Cargo feature (enabled by default).
4
4
5
- use futures:: { FutureExt , executor} ;
5
+ use crate :: { Client , RUNTIME } ;
6
+ use futures:: { executor, FutureExt } ;
6
7
use log:: error;
7
8
use std:: fmt;
9
+ use std:: future:: Future ;
8
10
use std:: path:: Path ;
11
+ use std:: pin:: Pin ;
9
12
use std:: str:: FromStr ;
10
- use std:: sync:: { mpsc, Arc , Mutex } ;
13
+ use std:: sync:: { mpsc, Arc } ;
11
14
use std:: time:: Duration ;
12
- use tokio_executor:: Executor ;
13
- use tokio_postgres:: tls:: { MakeTlsConnect , TlsConnect } ;
14
- use tokio_postgres:: { Error , Socket } ;
15
-
16
15
#[ doc( inline) ]
17
16
pub use tokio_postgres:: config:: { ChannelBinding , SslMode , TargetSessionAttrs } ;
18
-
19
- use crate :: { Client , RUNTIME } ;
17
+ use tokio_postgres :: tls :: { MakeTlsConnect , TlsConnect } ;
18
+ use tokio_postgres :: { Error , Socket } ;
20
19
21
20
/// Connection configuration.
22
21
///
@@ -94,8 +93,7 @@ use crate::{Client, RUNTIME};
94
93
#[ derive( Clone ) ]
95
94
pub struct Config {
96
95
config : tokio_postgres:: Config ,
97
- // this is an option since we don't want to boot up our default runtime unless we're actually going to use it.
98
- executor : Option < Arc < Mutex < dyn Executor + Send > > > ,
96
+ spawner : Option < Arc < dyn Fn ( Pin < Box < dyn Future < Output = ( ) > + Send > > ) + Sync + Send > > ,
99
97
}
100
98
101
99
impl fmt:: Debug for Config {
@@ -117,7 +115,7 @@ impl Config {
117
115
pub fn new ( ) -> Config {
118
116
Config {
119
117
config : tokio_postgres:: Config :: new ( ) ,
120
- executor : None ,
118
+ spawner : None ,
121
119
}
122
120
}
123
121
@@ -242,14 +240,14 @@ impl Config {
242
240
self
243
241
}
244
242
245
- /// Sets the executor used to run the connection futures.
243
+ /// Sets the spawner used to run the connection futures.
246
244
///
247
245
/// Defaults to a postgres-specific tokio `Runtime`.
248
- pub fn executor < E > ( & mut self , executor : E ) -> & mut Config
246
+ pub fn spawner < F > ( & mut self , spawn : F ) -> & mut Config
249
247
where
250
- E : Executor + ' static + Send ,
248
+ F : Fn ( Pin < Box < dyn Future < Output = ( ) > + Send > > ) + ' static + Sync + Send ,
251
249
{
252
- self . executor = Some ( Arc :: new ( Mutex :: new ( executor ) ) ) ;
250
+ self . spawner = Some ( Arc :: new ( spawn ) ) ;
253
251
self
254
252
}
255
253
@@ -261,22 +259,20 @@ impl Config {
261
259
T :: Stream : Send ,
262
260
<T :: TlsConnect as TlsConnect < Socket > >:: Future : Send ,
263
261
{
264
- let ( client, connection) = match & self . executor {
265
- Some ( executor ) => {
262
+ let ( client, connection) = match & self . spawner {
263
+ Some ( spawn ) => {
266
264
let ( tx, rx) = mpsc:: channel ( ) ;
267
265
let config = self . config . clone ( ) ;
268
266
let connect = async move {
269
267
let r = config. connect ( tls) . await ;
270
268
let _ = tx. send ( r) ;
271
269
} ;
272
- executor . lock ( ) . unwrap ( ) . spawn ( Box :: pin ( connect) ) . unwrap ( ) ;
270
+ spawn ( Box :: pin ( connect) ) ;
273
271
rx. recv ( ) . unwrap ( ) ?
274
272
}
275
273
None => {
276
274
let connect = self . config . connect ( tls) ;
277
- RUNTIME . handle ( ) . enter ( || {
278
- executor:: block_on ( connect)
279
- } ) ?
275
+ RUNTIME . handle ( ) . enter ( || executor:: block_on ( connect) ) ?
280
276
}
281
277
} ;
282
278
@@ -285,13 +281,9 @@ impl Config {
285
281
error ! ( "postgres connection error: {}" , e)
286
282
}
287
283
} ) ;
288
- match & self . executor {
289
- Some ( executor) => {
290
- executor
291
- . lock ( )
292
- . unwrap ( )
293
- . spawn ( Box :: pin ( connection) )
294
- . unwrap ( ) ;
284
+ match & self . spawner {
285
+ Some ( spawn) => {
286
+ spawn ( Box :: pin ( connection) ) ;
295
287
}
296
288
None => {
297
289
RUNTIME . spawn ( connection) ;
@@ -314,7 +306,7 @@ impl From<tokio_postgres::Config> for Config {
314
306
fn from ( config : tokio_postgres:: Config ) -> Config {
315
307
Config {
316
308
config,
317
- executor : None ,
309
+ spawner : None ,
318
310
}
319
311
}
320
312
}
0 commit comments