@@ -8,6 +8,7 @@ use libsql_hrana::proto::{
88 GetAutocommitStreamReq , PipelineReqBody , PipelineRespBody , SequenceStreamReq ,
99 StoreSqlStreamReq , StreamRequest , StreamResponse , StreamResult ,
1010} ;
11+ use std:: cell:: RefCell ;
1112use std:: sync:: atomic:: { AtomicBool , AtomicI64 , AtomicU64 , Ordering } ;
1213use std:: sync:: Arc ;
1314use tokio:: sync:: Mutex ;
6667 pipeline_url,
6768 cursor_url,
6869 auth_token,
69- sql_id_generator : 0 ,
70- baton : None ,
70+ sql_id_generator : RefCell :: new ( 0 ) ,
71+ baton : RefCell :: new ( None ) ,
7172 } ) ,
7273 } ) ,
7374 }
7778 /// Returns true if request was finalized correctly, false if stream was already closed.
7879 pub ( super ) async fn finalize ( & mut self , req : StreamRequest ) -> Result < bool > {
7980 let mut client = self . inner . stream . lock ( ) . await ;
80- if client. baton . is_none ( ) {
81+ if client. baton . borrow ( ) . is_none ( ) {
8182 tracing:: trace!( "baton not found - skipping finalize for {:?}" , req) ;
8283 return Ok ( false ) ;
8384 }
@@ -298,11 +299,11 @@ where
298299 T : HttpSend ,
299300{
300301 client : T ,
301- baton : Option < String > ,
302+ baton : RefCell < Option < String > > ,
302303 pipeline_url : Arc < str > ,
303304 cursor_url : Arc < str > ,
304305 auth_token : Arc < str > ,
305- sql_id_generator : SqlId ,
306+ sql_id_generator : RefCell < SqlId > ,
306307}
307308
308309impl < T > RawStream < T >
@@ -316,7 +317,7 @@ where
316317
317318 pub async fn open_cursor ( & mut self , batch : Batch ) -> Result < Cursor < T :: Stream > > {
318319 let msg = CursorReq {
319- baton : self . baton . clone ( ) ,
320+ baton : self . baton . borrow ( ) . clone ( ) ,
320321 batch,
321322 } ;
322323 let body = serde_json:: to_string ( & msg) . map_err ( HranaError :: Json ) ?;
@@ -336,7 +337,7 @@ where
336337 } // stream has been closed by the server
337338 Some ( baton) => {
338339 tracing:: trace!( "client stream has been assigned with baton: `{}`" , baton) ;
339- self . baton = Some ( baton)
340+ * self . baton . borrow_mut ( ) = Some ( baton)
340341 }
341342 }
342343 Ok ( cursor)
@@ -349,11 +350,11 @@ where
349350 tracing:: trace!(
350351 "client stream sending {} requests with baton `{}`: {:?}" ,
351352 N ,
352- self . baton. as_deref( ) . unwrap_or_default( ) ,
353+ self . baton. borrow ( ) . as_deref( ) . unwrap_or_default( ) ,
353354 requests
354355 ) ;
355356 let msg = PipelineReqBody {
356- baton : self . baton . clone ( ) ,
357+ baton : self . baton . borrow ( ) . clone ( ) ,
357358 requests : Vec :: from ( requests) ,
358359 } ;
359360 let body = serde_json:: to_string ( & msg) . map_err ( HranaError :: Json ) ?;
@@ -375,7 +376,7 @@ where
375376 } // stream has been closed by the server
376377 Some ( baton) => {
377378 tracing:: trace!( "client stream has been assigned with baton: `{}`" , baton) ;
378- self . baton = Some ( baton)
379+ * self . baton . borrow_mut ( ) = Some ( baton)
379380 }
380381 }
381382
@@ -424,16 +425,17 @@ where
424425 Ok ( ( resp, is_autocommit) )
425426 }
426427
427- fn reset ( & mut self ) {
428- if let Some ( baton) = self . baton . take ( ) {
428+ fn reset ( & self ) {
429+ if let Some ( baton) = self . baton . borrow_mut ( ) . take ( ) {
429430 tracing:: trace!( "closing client stream (baton: `{}`)" , baton) ;
430431 }
431- self . sql_id_generator = 0 ;
432+ * self . sql_id_generator . borrow_mut ( ) = 0 ;
432433 }
433434
434435 fn next_sql_id ( & mut self ) -> SqlId {
435- self . sql_id_generator = self . sql_id_generator . wrapping_add ( 1 ) ;
436- self . sql_id_generator
436+ let mut gen = self . sql_id_generator . borrow_mut ( ) ;
437+ * gen = gen. wrapping_add ( 1 ) ;
438+ * gen
437439 }
438440}
439441
@@ -443,7 +445,7 @@ where
443445 T : HttpSend ,
444446{
445447 fn drop ( & mut self ) {
446- if let Some ( baton) = self . baton . take ( ) {
448+ if let Some ( baton) = self . baton . borrow_mut ( ) . take ( ) {
447449 // only send a close request if stream was ever used to send the data
448450 tracing:: trace!( "closing client stream (baton: `{}`)" , baton) ;
449451 let req = serde_json:: to_string ( & PipelineReqBody {
0 commit comments