Skip to content

Commit

Permalink
Merge pull request #281 from AppFlowy-IO/client-log
Browse files Browse the repository at this point in the history
feat: add client timestamp to header
  • Loading branch information
speed2exe authored Jan 31, 2024
2 parents c16d56d + 719d579 commit d85dcfe
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 28 deletions.
16 changes: 0 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down Expand Up @@ -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"] }
Expand All @@ -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" }
Expand Down
17 changes: 10 additions & 7 deletions libs/client-api/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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(())
Expand All @@ -1169,14 +1170,16 @@ 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);
let request_builder = self
.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)
}
Expand Down

0 comments on commit d85dcfe

Please sign in to comment.