Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(connection_gateway): share single Redis connection per batch (#1866)

Will Hutchinson9269b5027c6a
diff --git a/rust/cloud-storage/connection_gateway/src/api/message/mod.rs b/rust/cloud-storage/connection_gateway/src/api/message/mod.rs
index 4cc7e33e9e..a4e4d2e626 100644
--- a/rust/cloud-storage/connection_gateway/src/api/message/mod.rs
+++ b/rust/cloud-storage/connection_gateway/src/api/message/mod.rs
@@ -52,6 +52,18 @@ pub async fn send_message_handler(
Path(entity): Path<Entity<'static>>,
Json(body): Json<SendMessageBody>,
) -> Result<(StatusCode, JsonResponse<SendMessageResponse>), (StatusCode, String)> {
+ let redis_connection = ctx
+ .context
+ .get_multiplexed_async_connection()
+ .await
+ .map_err(|e| {
+ tracing::error!(error=?e, "unable to get redis connection");
+ (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ "unable to send message".to_string(),
+ )
+ })?;
+
let res = send_message_to_entity(
&ctx,
&entity,
@@ -59,6 +71,7 @@ pub async fn send_message_handler(
message_type: body.message_type.clone(),
data: body.message.to_string(),
},
+ redis_connection,
)
.await
.map_err(|e| {
@@ -91,7 +104,20 @@ pub async fn batch_send_message_handler(
) -> Result<(StatusCode, JsonResponse<SendMessageResponse>), (StatusCode, String)> {
let now = Instant::now();
+ let redis_connection = ctx
+ .context
+ .get_multiplexed_async_connection()
+ .await
+ .map_err(|e| {
+ tracing::error!(error=?e, "unable to get redis connection");
+ (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ "unable to send message".to_string(),
+ )
+ })?;
+
let all_receipts = try_join_all(body.entities.iter().map(|entity| {
+ let redis_connection = redis_connection.clone();
send_message_to_entity(
&ctx,
entity,
@@ -99,6 +125,7 @@ pub async fn batch_send_message_handler(
message_type: body.message_type.clone(),
data: body.message.to_string(),
},
+ redis_connection,
)
}))
.await
@@ -138,7 +165,20 @@ pub async fn batch_send_unique_messages_handler(
) -> Result<(StatusCode, JsonResponse<SendMessageResponse>), (StatusCode, String)> {
let now = Instant::now();
+ let redis_connection = ctx
+ .context
+ .get_multiplexed_async_connection()
+ .await
+ .map_err(|e| {
+ tracing::error!(error=?e, "unable to get redis connection");
+ (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ "unable to send message".to_string(),
+ )
+ })?;
+
let all_receipts = try_join_all(body.messages.iter().map(|message| {
+ let redis_connection = redis_connection.clone();
send_message_to_entity(
&ctx,
&message.entity,
@@ -146,6 +186,7 @@ pub async fn batch_send_unique_messages_handler(
message_type: message.message_type.clone(),
data: message.message_content.to_string(),
},
+ redis_connection,
)
}))
.await
diff --git a/rust/cloud-storage/connection_gateway/src/service/sender.rs b/rust/cloud-storage/connection_gateway/src/service/sender.rs
index e5be709ea1..7cfaeb3c95 100644
--- a/rust/cloud-storage/connection_gateway/src/service/sender.rs
+++ b/rust/cloud-storage/connection_gateway/src/service/sender.rs
@@ -9,6 +9,7 @@ use crate::service::redis::MessageWithConnection;
use anyhow::Result;
use futures::future::try_join_all;
use model_entity::Entity;
+use redis::aio::MultiplexedConnection;
use std::collections::HashMap;
use std::time::Instant;
@@ -17,18 +18,18 @@ pub struct Delivery {
pub active: bool,
}
-#[tracing::instrument(skip(ctx))]
+#[tracing::instrument(skip(ctx, redis_connection))]
pub async fn send_message_to_entity<Ctx>(
ctx: Ctx,
entity: &Entity<'_>,
message: Message,
+ redis_connection: MultiplexedConnection,
) -> Result<Vec<MessageReceipt>>
where
Ctx: AsRef<ApiContext> + AsRef<Config> + Copy,
{
tracing::trace!("sending message to entity");
let api_context: &ApiContext = ctx.as_ref();
- let redis_connection = api_context.get_multiplexed_async_connection().await?;
let instant = Instant::now();
let connections = api_context
diff --git a/rust/cloud-storage/connection_gateway/src/service/tracker.rs b/rust/cloud-storage/connection_gateway/src/service/tracker.rs
index 15dcbec61c..a8fe345173 100644
--- a/rust/cloud-storage/connection_gateway/src/service/tracker.rs
+++ b/rust/cloud-storage/connection_gateway/src/service/tracker.rs
@@ -156,7 +156,8 @@ pub async fn notify_tracking_change(ctx: ConnectionContext<'_>, entity: &Entity<
})?,
};
- send_message_to_entity(ctx, entity, message).await?;
+ let redis_connection = ctx.api_context.get_multiplexed_async_connection().await?;
+ send_message_to_entity(ctx, entity, message, redis_connection).await?;
Ok(())
}