@@ -31,7 +31,7 @@ use crate::Key;
3131pub struct Service < ' a > {
3232 inner : Arc < api:: Service < ' a > > ,
3333 aes_key : Option < Arc < Key > > ,
34- session : Arc < api:: Session < ' a > > ,
34+ session : Arc < api:: Session < ' static > > ,
3535 algorithm : Algorithm ,
3636}
3737
@@ -225,6 +225,27 @@ impl<'a> Service<'a> {
225225 }
226226}
227227
228+ impl Drop for Service < ' _ > {
229+ fn drop ( & mut self ) {
230+ let session = Arc :: clone ( & self . session ) ;
231+ #[ cfg( feature = "tokio" ) ]
232+ {
233+ let _ = tokio:: spawn ( async move {
234+ let _ = session. close ( ) . await ;
235+ } ) ;
236+ }
237+ #[ cfg( feature = "async-std" ) ]
238+ {
239+ blocking:: unblock ( move || {
240+ futures_lite:: future:: block_on ( async move {
241+ let _ = session. close ( ) . await ;
242+ } )
243+ } )
244+ . detach ( ) ;
245+ }
246+ }
247+ }
248+
228249#[ cfg( test) ]
229250#[ cfg( feature = "tokio" ) ]
230251mod tests {
@@ -265,15 +286,11 @@ mod tests {
265286 async fn encrypted_session ( ) {
266287 let service = Service :: encrypted ( ) . await . unwrap ( ) ;
267288 assert ! ( service. default_collection( ) . await . is_ok( ) ) ;
268-
269- service. session . close ( ) . await . unwrap ( ) ;
270289 }
271290
272291 #[ tokio:: test]
273292 async fn plain_session ( ) {
274293 let service = Service :: plain ( ) . await . unwrap ( ) ;
275294 assert ! ( service. default_collection( ) . await . is_ok( ) ) ;
276-
277- service. session . close ( ) . await . unwrap ( ) ;
278295 }
279296}
0 commit comments