Skip to content

Commit fdf1be3

Browse files
committed
fix: retry database connection
1 parent 2f41c0a commit fdf1be3

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

Cargo.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "feedback-fusion"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
edition = "2021"
55
license = "MIT"
66

@@ -46,6 +46,7 @@ tonic-reflection = "0.11.0"
4646
tonic-web = "0.11.0"
4747
tokio = { version = "1.37.0", features = ["full"] }
4848
tower = "0.4.13"
49+
tokio-retry = "0.3"
4950
tower-http = { version = "=0.4.4", features = ["trace", "validate-request"] }
5051
tracing = "0.1.39"
5152
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

charts/feedback-fusion/templates/deployment.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ spec:
4848
{{- end }}
4949
livenessProbe:
5050
{{- toYaml .Values.livenessProbe | nindent 12 }}
51-
readinessProbe:
52-
{{- toYaml .Values.readinessProbe | nindent 12 }}
51+
startupProbe:
52+
{{- toYaml .Values.startupProbe | nindent 12 }}
5353
resources:
5454
{{- toYaml .Values.resources | nindent 12 }}
5555
volumeMounts:

charts/feedback-fusion/values.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,12 @@ resources: {}
6565
livenessProbe:
6666
grpc:
6767
port: 8000
68-
readinessProbe:
68+
periodSeconds: 5
69+
startupProbe:
6970
grpc:
7071
port: 8000
72+
periodSeconds: 2
73+
failureThreshold: 10
7174

7275
# Additional volumes on the output Deployment definition.
7376
volumes: []

src/database/mod.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
use crate::{database::migration::Migration, prelude::*};
2525
use rbatis::RBatis;
26+
use tokio_retry::{
27+
strategy::{jitter, FibonacciBackoff},
28+
Retry,
29+
};
2630

2731
pub mod migration;
2832
pub mod schema;
@@ -105,7 +109,23 @@ macro_rules! database_configuration {
105109
#[cfg(feature = $scheme)]
106110
Self::$ident(config) => {
107111
let url = config.to_url($scheme);
108-
connection.init($driver {}, url.as_str())?;
112+
113+
let retry_strategy = FibonacciBackoff::from_millis(500)
114+
.map(jitter)
115+
.take(5);
116+
117+
Retry::spawn(retry_strategy.clone(), || async {
118+
match connection.init($driver {}, url.as_str()) {
119+
Ok(result) => Ok(result),
120+
Err(error) => {
121+
error!("Failed to establish DatabaseConnection, retrying in {}s", retry_strategy.clone().next().unwrap().as_secs());
122+
error!("{}", error);
123+
124+
Err(error)
125+
}
126+
}
127+
}).await?;
128+
109129
connection.intercepts.clear();
110130
connection.intercepts.push(std::sync::Arc::new(rbatis::plugin::intercept_log::LogInterceptor::new(log::LevelFilter::Debug)));
111131

0 commit comments

Comments
 (0)