@@ -14,13 +14,14 @@ pub(crate) struct JobToken {
1414 token : Option < Acquired > ,
1515 /// A pool to which `token` should be returned. `pool` is optional, as one might want to release a token straight away instead
1616 /// 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 > > > > ,
1818}
1919
2020impl Drop for JobToken {
2121 fn drop ( & mut self ) {
2222 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) ) ) ;
2425 }
2526 }
2627}
@@ -43,8 +44,8 @@ impl JobToken {
4344/// for reuse if we know we're going to request another token after freeing the current one.
4445pub ( crate ) struct JobTokenServer {
4546 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 > > > ,
4849}
4950
5051impl JobTokenServer {
@@ -58,12 +59,12 @@ impl JobTokenServer {
5859 tx. send ( None ) . unwrap ( ) ;
5960 let pool = tx. clone ( ) ;
6061 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 ( ) ) ) ) ;
6263 } ) ?;
6364 Ok ( Self { helper, tx, rx } )
6465 }
6566
66- pub ( crate ) fn acquire ( & self ) -> JobToken {
67+ pub ( crate ) fn acquire ( & self ) -> Result < JobToken , crate :: Error > {
6768 let token = if let Ok ( token) = self . rx . try_recv ( ) {
6869 // Opportunistically check if there's a token that can be reused.
6970 token
@@ -72,10 +73,15 @@ impl JobTokenServer {
7273 self . helper . request_token ( ) ;
7374 self . rx . recv ( ) . unwrap ( )
7475 } ;
75- JobToken {
76+ let token = if let Some ( token) = token {
77+ Some ( token?)
78+ } else {
79+ None
80+ } ;
81+ Ok ( JobToken {
7682 token,
7783 pool : Some ( self . tx . clone ( ) ) ,
78- }
84+ } )
7985 }
8086}
8187
0 commit comments