Skip to content

Commit d85dcfe

Browse files
authored
Merge pull request #281 from AppFlowy-IO/client-log
feat: add client timestamp to header
2 parents c16d56d + 719d579 commit d85dcfe

File tree

3 files changed

+10
-28
lines changed

3 files changed

+10
-28
lines changed

Cargo.lock

Lines changed: 0 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ actix-rt = "2"
1313
actix-web-actors = { version = "4.2.0" }
1414
actix-service = "2.0.2"
1515
actix-identity = "0.6.0"
16-
actix-cors = "0.6.5"
1716
actix-router = "0.5.2"
1817
actix-session = { version = "0.8", features = ["redis-rs-tls-session"] }
1918
openssl = { version = "0.10.62", features = ["vendored"] }
@@ -134,7 +133,6 @@ shared-entity = { path = "libs/shared-entity" }
134133
app-error = { path = "libs/app_error" }
135134
serde_json = "1.0.111"
136135
serde = { version = "1.0.195", features = ["derive"] }
137-
serde_repr = "0.1.18"
138136
bytes = "1.5.0"
139137
workspace-template = { path = "libs/workspace-template" }
140138
uuid = { version = "1.6.1", features = ["v4"] }
@@ -160,9 +158,6 @@ codegen-units = 16
160158
debug = true
161159
lto = false
162160

163-
[profile.dev.package.sqlx-macros]
164-
opt-level = 3
165-
166161
[patch.crates-io]
167162
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
168163
collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }

libs/client-api/src/http.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,9 @@ impl Client {
987987
}
988988

989989
pub async fn ws_url(&self, device_id: &str) -> Result<String, AppResponseError> {
990-
self.refresh_if_required().await?;
990+
self
991+
.refresh_if_expired(chrono::Local::now().timestamp())
992+
.await?;
991993

992994
let access_token = self.access_token()?;
993995
Ok(format!("{}/{}/{}", self.ws_addr, access_token, device_id))
@@ -1151,13 +1153,12 @@ impl Client {
11511153
.into_data()
11521154
}
11531155

1154-
pub async fn refresh_if_required(&self) -> Result<(), AppResponseError> {
1156+
// Refresh token if given timestamp is close to the token expiration time
1157+
pub async fn refresh_if_expired(&self, ts: i64) -> Result<(), AppResponseError> {
11551158
let expires_at = self.token_expires_at()?;
11561159

1157-
// Refresh token if it's about to expire
1158-
let time_now_sec = chrono::Local::now().timestamp();
1159-
if time_now_sec + 10 > expires_at {
1160-
// Add 10 seconds buffer
1160+
if ts + 30 > expires_at {
1161+
// Add 30 seconds buffer
11611162
self.refresh_token().await?;
11621163
}
11631164
Ok(())
@@ -1169,14 +1170,16 @@ impl Client {
11691170
method: Method,
11701171
url: &str,
11711172
) -> Result<RequestBuilder, AppResponseError> {
1172-
self.refresh_if_required().await?;
1173+
let ts_now = chrono::Local::now().timestamp();
1174+
self.refresh_if_expired(ts_now).await?;
11731175

11741176
let access_token = self.access_token()?;
11751177
trace!("start request: {}, method: {}", url, method);
11761178
let request_builder = self
11771179
.cloud_client
11781180
.request(method, url)
11791181
.header("client-version", CLIENT_API_VERSION)
1182+
.header("client-timestamp", ts_now.to_string())
11801183
.bearer_auth(access_token);
11811184
Ok(request_builder)
11821185
}

0 commit comments

Comments
 (0)