Skip to content

Commit 1ad1d43

Browse files
authored
Merge pull request #1677 from tursodatabase/fix_1676
Retry primary handshake 3 times instead of 100 in embedded replicas
2 parents 0c19b79 + e249401 commit 1ad1d43

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

libsql-replication/src/replicator.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ pub struct Replicator<C, I> {
139139
injector: I,
140140
state: ReplicatorState,
141141
frames_synced: usize,
142+
max_handshake_retries: usize,
142143
}
143144

144145
const INJECTOR_BUFFER_CAPACITY: usize = 10;
@@ -185,14 +186,20 @@ where
185186
injector,
186187
state: ReplicatorState::NeedHandshake,
187188
frames_synced: 0,
189+
max_handshake_retries: HANDSHAKE_MAX_RETRIES,
188190
}
189191
}
190192

191-
/// for a handshake on next call to replicate.
193+
/// force a handshake on next call to replicate.
192194
pub fn force_handshake(&mut self) {
193195
self.state = ReplicatorState::NeedHandshake;
194196
}
195197

198+
/// configure number of handshake retries.
199+
pub fn set_primary_handshake_retries(&mut self, retries: usize) {
200+
self.max_handshake_retries = retries;
201+
}
202+
196203
pub fn client_mut(&mut self) -> &mut C {
197204
&mut self.client
198205
}
@@ -208,7 +215,7 @@ where
208215

209216
pub async fn try_perform_handshake(&mut self) -> Result<(), Error> {
210217
let mut error_printed = false;
211-
for _ in 0..HANDSHAKE_MAX_RETRIES {
218+
for _ in 0..self.max_handshake_retries {
212219
tracing::debug!("Attempting to perform handshake with primary.");
213220
match self.client.handshake().await {
214221
Ok(_) => {

libsql/src/replication/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,15 @@ impl EmbeddedReplicator {
149149
encryption_config: Option<EncryptionConfig>,
150150
perodic_sync: Option<Duration>,
151151
) -> Result<Self> {
152-
let replicator = Arc::new(Mutex::new(
152+
let mut replicator =
153153
Replicator::new_sqlite(
154154
Either::Left(client),
155155
db_path,
156156
auto_checkpoint,
157157
encryption_config,
158-
)
159-
.await?,
160-
));
158+
).await?;
159+
replicator.set_primary_handshake_retries(3);
160+
let replicator = Arc::new(Mutex::new(replicator));
161161

162162
let mut replicator = Self {
163163
replicator,

0 commit comments

Comments
 (0)