@@ -8,11 +8,14 @@ use crate::{
8
8
sync:: SyncContext ,
9
9
BatchRows , Error , Result , Statement , Transaction , TransactionBehavior ,
10
10
} ;
11
- use std:: sync:: Arc ;
11
+ use std:: sync:: {
12
+ atomic:: { AtomicBool , Ordering } ,
13
+ Arc ,
14
+ } ;
12
15
use std:: time:: Duration ;
13
16
use tokio:: sync:: Mutex ;
14
17
15
- use super :: { statement :: SyncedStatement , transaction:: SyncedTx } ;
18
+ use super :: transaction:: SyncedTx ;
16
19
17
20
#[ derive( Clone ) ]
18
21
pub struct SyncedConnection {
@@ -21,6 +24,7 @@ pub struct SyncedConnection {
21
24
pub read_your_writes : bool ,
22
25
pub context : Arc < Mutex < SyncContext > > ,
23
26
pub state : Arc < Mutex < State > > ,
27
+ pub needs_pull : Arc < AtomicBool > ,
24
28
}
25
29
26
30
impl SyncedConnection {
@@ -89,7 +93,7 @@ impl SyncedConnection {
89
93
_ => {
90
94
* state = predicted_end_state;
91
95
false
92
- } ,
96
+ }
93
97
} ;
94
98
95
99
Ok ( should_execute_local)
@@ -106,6 +110,10 @@ impl Conn for SyncedConnection {
106
110
107
111
async fn execute_batch ( & self , sql : & str ) -> Result < BatchRows > {
108
112
if self . should_execute_local ( sql) . await ? {
113
+ if self . needs_pull . swap ( false , Ordering :: Relaxed ) {
114
+ let mut context = self . context . lock ( ) . await ;
115
+ crate :: sync:: try_pull ( & mut context, & self . local ) . await ?;
116
+ }
109
117
self . local . execute_batch ( sql)
110
118
} else {
111
119
self . remote . execute_batch ( sql) . await
@@ -114,6 +122,10 @@ impl Conn for SyncedConnection {
114
122
115
123
async fn execute_transactional_batch ( & self , sql : & str ) -> Result < BatchRows > {
116
124
if self . should_execute_local ( sql) . await ? {
125
+ if self . needs_pull . swap ( false , Ordering :: Relaxed ) {
126
+ let mut context = self . context . lock ( ) . await ;
127
+ crate :: sync:: try_pull ( & mut context, & self . local ) . await ?;
128
+ }
117
129
self . local . execute_transactional_batch ( sql) ?;
118
130
Ok ( BatchRows :: empty ( ) )
119
131
} else {
@@ -123,6 +135,10 @@ impl Conn for SyncedConnection {
123
135
124
136
async fn prepare ( & self , sql : & str ) -> Result < Statement > {
125
137
if self . should_execute_local ( sql) . await ? {
138
+ if self . needs_pull . swap ( false , Ordering :: Relaxed ) {
139
+ let mut context = self . context . lock ( ) . await ;
140
+ crate :: sync:: try_pull ( & mut context, & self . local ) . await ?;
141
+ }
126
142
Ok ( Statement {
127
143
inner : Box :: new ( LibsqlStmt ( self . local . prepare ( sql) ?) ) ,
128
144
} )
@@ -132,16 +148,10 @@ impl Conn for SyncedConnection {
132
148
} ;
133
149
134
150
if self . read_your_writes {
135
- Ok ( Statement {
136
- inner : Box :: new ( SyncedStatement {
137
- conn : self . local . clone ( ) ,
138
- context : self . context . clone ( ) ,
139
- inner : stmt,
140
- } ) ,
141
- } )
142
- } else {
143
- Ok ( stmt)
151
+ self . needs_pull . store ( true , Ordering :: Relaxed ) ;
144
152
}
153
+
154
+ Ok ( stmt)
145
155
}
146
156
}
147
157
0 commit comments