Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 06d5d64

Browse files
committed
set connection state to unknown before sending proxy request
1 parent 0c56e87 commit 06d5d64

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

libsqlx/src/database/proxy/connection.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,27 @@ use crate::Result;
1010

1111
use super::WaitFrameNoCb;
1212

13+
#[derive(Debug, Default)]
14+
enum State {
15+
Txn,
16+
#[default]
17+
Idle,
18+
Unknown
19+
}
20+
21+
impl State {
22+
/// Returns `true` if the state is [`Idle`].
23+
///
24+
/// [`Idle`]: State::Idle
25+
#[must_use]
26+
fn is_idle(&self) -> bool {
27+
matches!(self, Self::Idle)
28+
}
29+
}
30+
1331
#[derive(Debug, Default)]
1432
pub(crate) struct ConnState {
15-
is_txn: bool,
33+
state: State,
1634
last_frame_no: Option<FrameNo>,
1735
}
1836

@@ -123,6 +141,9 @@ where
123141
state: self.state.clone(),
124142
};
125143

144+
// set the connection state to unknown before executing on the remote
145+
self.state.lock().state = State::Unknown;
146+
126147
self.conn
127148
.execute_program(&self.pgm, Box::new(builder))
128149
.unwrap();
@@ -144,7 +165,7 @@ where
144165
pgm: &Program,
145166
builder: Box<dyn ResultBuilder>,
146167
) -> crate::Result<()> {
147-
if !self.state.lock().is_txn && pgm.is_read_only() {
168+
if self.state.lock().state.is_idle() && pgm.is_read_only() {
148169
if let Some(frame_no) = self.state.lock().last_frame_no {
149170
(self.wait_frame_no_cb)(frame_no);
150171
}
@@ -162,6 +183,9 @@ where
162183
// rollback(&mut self.conn.read_db);
163184
Ok(())
164185
} else {
186+
// we set the state to unknown because until we have received from the actual
187+
// connection state from the primary.
188+
self.state.lock().state = State::Unknown;
165189
let builder = ExtractFrameNoBuilder {
166190
builder,
167191
state: self.state.clone(),
@@ -243,7 +267,11 @@ impl ResultBuilder for ExtractFrameNoBuilder {
243267
) -> Result<bool, QueryResultBuilderError> {
244268
let mut state = self.state.lock();
245269
state.last_frame_no = frame_no;
246-
state.is_txn = is_txn;
270+
if is_txn {
271+
state.state = State::Txn;
272+
} else {
273+
state.state = State::Idle;
274+
}
247275
self.builder.finnalize(is_txn, frame_no)
248276
}
249277
}

0 commit comments

Comments
 (0)