Merge pull request #281 from AppFlowy-IO/client-log

feat: add client timestamp to header
This commit is contained in:
Zack 2024-01-31 13:18:42 +08:00 committed by GitHub
commit d85dcfeb14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 28 deletions

16
Cargo.lock generated
View File

@ -44,21 +44,6 @@ dependencies = [
"tracing",
]
[[package]]
name = "actix-cors"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0346d8c1f762b41b458ed3145eea914966bb9ad20b9be0d6d463b20d45586370"
dependencies = [
"actix-utils",
"actix-web",
"derive_more",
"futures-util",
"log",
"once_cell",
"smallvec",
]
[[package]]
name = "actix-http"
version = "3.5.1"
@ -513,7 +498,6 @@ name = "appflowy-cloud"
version = "0.1.0"
dependencies = [
"actix",
"actix-cors",
"actix-http",
"actix-identity",
"actix-router",

View File

@ -13,7 +13,6 @@ actix-rt = "2"
actix-web-actors = { version = "4.2.0" }
actix-service = "2.0.2"
actix-identity = "0.6.0"
actix-cors = "0.6.5"
actix-router = "0.5.2"
actix-session = { version = "0.8", features = ["redis-rs-tls-session"] }
openssl = { version = "0.10.62", features = ["vendored"] }
@ -134,7 +133,6 @@ shared-entity = { path = "libs/shared-entity" }
app-error = { path = "libs/app_error" }
serde_json = "1.0.111"
serde = { version = "1.0.195", features = ["derive"] }
serde_repr = "0.1.18"
bytes = "1.5.0"
workspace-template = { path = "libs/workspace-template" }
uuid = { version = "1.6.1", features = ["v4"] }
@ -160,9 +158,6 @@ codegen-units = 16
debug = true
lto = false
[profile.dev.package.sqlx-macros]
opt-level = 3
[patch.crates-io]
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }
collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "fe8f08fcc99ea56c78bfb746ccb0cd308126141d" }

View File

@ -987,7 +987,9 @@ impl Client {
}
pub async fn ws_url(&self, device_id: &str) -> Result<String, AppResponseError> {
self.refresh_if_required().await?;
self
.refresh_if_expired(chrono::Local::now().timestamp())
.await?;
let access_token = self.access_token()?;
Ok(format!("{}/{}/{}", self.ws_addr, access_token, device_id))
@ -1151,13 +1153,12 @@ impl Client {
.into_data()
}
pub async fn refresh_if_required(&self) -> Result<(), AppResponseError> {
// Refresh token if given timestamp is close to the token expiration time
pub async fn refresh_if_expired(&self, ts: i64) -> Result<(), AppResponseError> {
let expires_at = self.token_expires_at()?;
// Refresh token if it's about to expire
let time_now_sec = chrono::Local::now().timestamp();
if time_now_sec + 10 > expires_at {
// Add 10 seconds buffer
if ts + 30 > expires_at {
// Add 30 seconds buffer
self.refresh_token().await?;
}
Ok(())
@ -1169,7 +1170,8 @@ impl Client {
method: Method,
url: &str,
) -> Result<RequestBuilder, AppResponseError> {
self.refresh_if_required().await?;
let ts_now = chrono::Local::now().timestamp();
self.refresh_if_expired(ts_now).await?;
let access_token = self.access_token()?;
trace!("start request: {}, method: {}", url, method);
@ -1177,6 +1179,7 @@ impl Client {
.cloud_client
.request(method, url)
.header("client-version", CLIENT_API_VERSION)
.header("client-timestamp", ts_now.to_string())
.bearer_auth(access_token);
Ok(request_builder)
}