feat: encode batch create collab data as binary (#242)

This commit is contained in:
Nathan.fooo 2024-01-03 06:07:21 +08:00 committed by GitHub
parent b48bf402c7
commit 6d0612ffaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 55 additions and 19 deletions

1
Cargo.lock generated
View File

@ -1751,6 +1751,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"app-error", "app-error",
"bincode",
"chrono", "chrono",
"collab-entity", "collab-entity",
"serde", "serde",

View File

@ -729,10 +729,15 @@ impl Client {
workspace_id: workspace_id.to_string(), workspace_id: workspace_id.to_string(),
params_list: params, params_list: params,
}; };
let resp = self let resp = self
.http_client_with_auth(Method::POST, &url) .http_client_with_auth(Method::POST, &url)
.await? .await?
.json(&payload) .body(
payload
.to_bytes()
.map_err(|err| AppError::Internal(err.into()))?,
)
.send() .send()
.await?; .await?;
log_request_id(&resp); log_request_id(&resp);

View File

@ -18,3 +18,4 @@ anyhow = "1.0.75"
tracing = "0.1" tracing = "0.1"
serde_repr = "0.1.16" serde_repr = "0.1.16"
app-error = { workspace = true } app-error = { workspace = true }
bincode = "1.3.3"

View File

@ -31,6 +31,13 @@ impl CreateCollabParams {
workspace_id: workspace_id.to_string(), workspace_id: workspace_id.to_string(),
} }
} }
pub fn to_bytes(&self) -> Result<Vec<u8>, bincode::Error> {
bincode::serialize(self)
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self, bincode::Error> {
bincode::deserialize(bytes)
}
} }
impl Deref for CreateCollabParams { impl Deref for CreateCollabParams {
@ -81,6 +88,16 @@ pub struct BatchCreateCollabParams {
pub params_list: Vec<CollabParams>, pub params_list: Vec<CollabParams>,
} }
impl BatchCreateCollabParams {
pub fn to_bytes(&self) -> Result<Vec<u8>, bincode::Error> {
bincode::serialize(self)
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self, bincode::Error> {
bincode::deserialize(bytes)
}
}
#[derive(Debug, Clone, Validate, Serialize, Deserialize)] #[derive(Debug, Clone, Validate, Serialize, Deserialize)]
pub struct DeleteCollabParams { pub struct DeleteCollabParams {
#[validate(custom = "validate_not_empty_str")] #[validate(custom = "validate_not_empty_str")]

View File

@ -27,6 +27,7 @@ http {
listen 80; listen 80;
listen 443 ssl; listen 443 ssl;
client_max_body_size 5M;
underscores_in_headers on; underscores_in_headers on;
@ -55,7 +56,6 @@ http {
location /api { location /api {
proxy_set_header X-Request-Id $request_id; proxy_set_header X-Request-Id $request_id;
proxy_pass http://appflowy_cloud:8000; proxy_pass http://appflowy_cloud:8000;
client_max_body_size 6M;
} }
# Minio Web UI # Minio Web UI

View File

@ -253,14 +253,18 @@ async fn create_collab_handler(
#[instrument(skip(state, payload), err)] #[instrument(skip(state, payload), err)]
async fn batch_create_collab_handler( async fn batch_create_collab_handler(
user_uuid: UserUuid, user_uuid: UserUuid,
payload: Json<BatchCreateCollabParams>, payload: Bytes,
state: Data<AppState>, state: Data<AppState>,
) -> Result<Json<AppResponse<()>>> { ) -> Result<Json<AppResponse<()>>> {
let payload = BatchCreateCollabParams::from_bytes(&payload).map_err(|err| {
AppError::InvalidRequest(format!("Failed to parse BatchCreateCollabParams: {}", err))
})?;
payload.validate().map_err(AppError::from)?; payload.validate().map_err(AppError::from)?;
let BatchCreateCollabParams { let BatchCreateCollabParams {
workspace_id, workspace_id,
params_list, params_list,
} = payload.into_inner(); } = payload;
if params_list.is_empty() { if params_list.is_empty() {
return Err(AppError::InvalidRequest("Empty collab params list".to_string()).into()); return Err(AppError::InvalidRequest("Empty collab params list".to_string()).into());

View File

@ -1,6 +1,6 @@
use actix_http::header::HeaderName; use actix_http::header::HeaderName;
use std::future::{ready, Ready}; use std::future::{ready, Ready};
use tracing::{Instrument, Level}; use tracing::{span, Instrument, Level};
use actix_service::{forward_ready, Service, Transform}; use actix_service::{forward_ready, Service, Transform};
use actix_web::dev::{ServiceRequest, ServiceResponse}; use actix_web::dev::{ServiceRequest, ServiceResponse};
@ -49,22 +49,22 @@ where
let fut = self.service.call(req); let fut = self.service.call(req);
Box::pin(fut) Box::pin(fut)
} else { } else {
let request_id = match get_request_id(&req) { let request_id = get_request_id(&req).unwrap_or_else(|| {
Some(request_id) => request_id, let request_id = uuid::Uuid::new_v4().to_string();
None => { if let Ok(header_value) = HeaderValue::from_str(&request_id) {
let request_id = uuid::Uuid::new_v4().to_string(); req
if let Ok(header_value) = HeaderValue::from_str(&request_id) { .headers_mut()
req .insert(HeaderName::from_static(X_REQUEST_ID), header_value);
.headers_mut() }
.insert(HeaderName::from_static(X_REQUEST_ID), header_value); request_id
} });
request_id
}, let span = match get_payload_size(&req) {
Some(size) => span!(Level::INFO, "request", request_id = %request_id, payload_size = size),
None => span!(Level::INFO, "request", request_id = %request_id),
}; };
let span = tracing::span!(Level::INFO, "request_id", request_id = %request_id);
let fut = self.service.call(req); let fut = self.service.call(req);
Box::pin(async move { Box::pin(async move {
let mut res = fut.instrument(span).await?; let mut res = fut.instrument(span).await?;
@ -92,3 +92,11 @@ pub fn get_request_id(req: &ServiceRequest) -> Option<String> {
None => None, None => None,
} }
} }
fn get_payload_size(req: &ServiceRequest) -> Option<usize> {
req
.headers()
.get("content-length")
.and_then(|val| val.to_str().ok())
.and_then(|val| val.parse::<usize>().ok())
}

View File

@ -29,7 +29,7 @@ async fn batch_insert_collab_success_test() {
let params_list = (0..5) let params_list = (0..5)
.map(|_| CollabParams { .map(|_| CollabParams {
object_id: Uuid::new_v4().to_string(), object_id: Uuid::new_v4().to_string(),
encoded_collab_v1: vec![0, 200], encoded_collab_v1: vec![0u8; 1024 * 1024],
collab_type: CollabType::Document, collab_type: CollabType::Document,
override_if_exist: false, override_if_exist: false,
}) })