@@ -14,13 +14,14 @@ pub(crate) struct JobToken {
14
14
token : Option < Acquired > ,
15
15
/// A pool to which `token` should be returned. `pool` is optional, as one might want to release a token straight away instead
16
16
/// of storing it back in the pool - see [`Self::forget()`] function for that.
17
- pool : Option < Sender < Option < Acquired > > > ,
17
+ pool : Option < Sender < Option < Result < Acquired , crate :: Error > > > > ,
18
18
}
19
19
20
20
impl Drop for JobToken {
21
21
fn drop ( & mut self ) {
22
22
if let Some ( pool) = & self . pool {
23
- let _ = pool. send ( self . token . take ( ) ) ;
23
+ // Always send back an Ok() variant as we know that the acquisition for this token has succeeded.
24
+ let _ = pool. send ( self . token . take ( ) . map ( |token| Ok ( token) ) ) ;
24
25
}
25
26
}
26
27
}
@@ -43,8 +44,8 @@ impl JobToken {
43
44
/// for reuse if we know we're going to request another token after freeing the current one.
44
45
pub ( crate ) struct JobTokenServer {
45
46
helper : HelperThread ,
46
- tx : Sender < Option < Acquired > > ,
47
- rx : Receiver < Option < Acquired > > ,
47
+ tx : Sender < Option < Result < Acquired , crate :: Error > > > ,
48
+ rx : Receiver < Option < Result < Acquired , crate :: Error > > > ,
48
49
}
49
50
50
51
impl JobTokenServer {
@@ -58,12 +59,12 @@ impl JobTokenServer {
58
59
tx. send ( None ) . unwrap ( ) ;
59
60
let pool = tx. clone ( ) ;
60
61
let helper = client. into_helper_thread ( move |acq| {
61
- let _ = pool. send ( Some ( acq. unwrap ( ) ) ) ;
62
+ let _ = pool. send ( Some ( acq. map_err ( |e| e . into ( ) ) ) ) ;
62
63
} ) ?;
63
64
Ok ( Self { helper, tx, rx } )
64
65
}
65
66
66
- pub ( crate ) fn acquire ( & self ) -> JobToken {
67
+ pub ( crate ) fn acquire ( & self ) -> Result < JobToken , crate :: Error > {
67
68
let token = if let Ok ( token) = self . rx . try_recv ( ) {
68
69
// Opportunistically check if there's a token that can be reused.
69
70
token
@@ -72,10 +73,15 @@ impl JobTokenServer {
72
73
self . helper . request_token ( ) ;
73
74
self . rx . recv ( ) . unwrap ( )
74
75
} ;
75
- JobToken {
76
+ let token = if let Some ( token) = token {
77
+ Some ( token?)
78
+ } else {
79
+ None
80
+ } ;
81
+ Ok ( JobToken {
76
82
token,
77
83
pool : Some ( self . tx . clone ( ) ) ,
78
- }
84
+ } )
79
85
}
80
86
}
81
87
0 commit comments