Skip to content

Commit ad7480d

Browse files
committed
feat: create session in runtime
Signed-off-by: Mauro Sardara <msardara@cisco.com>
1 parent 035464b commit ad7480d

File tree

1 file changed

+62
-45
lines changed

1 file changed

+62
-45
lines changed

data-plane/slimrpc/src/channel.rs

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -616,63 +616,80 @@ impl Channel {
616616
method_name: &str,
617617
ctx: &Context,
618618
) -> Result<Session, Status> {
619-
// Build method-specific subscription name (e.g., org/namespace/app-Service-Method)
620-
let method_subscription_name =
621-
crate::build_method_subscription_name(&self.inner.remote, service_name, method_name);
619+
let service_name = service_name.to_string();
620+
let method_name = method_name.to_string();
621+
let ctx = ctx.clone();
622+
let app = self.inner.app.clone();
623+
let remote = self.inner.remote.clone();
624+
let connection_id = self.inner.connection_id;
625+
let runtime = self.inner.runtime.clone();
626+
627+
// Spawn the entire session creation in a background task
628+
let handle = runtime.spawn(async move {
629+
// Build method-specific subscription name (e.g., org/namespace/app-Service-Method)
630+
let method_subscription_name =
631+
crate::build_method_subscription_name(&remote, &service_name, &method_name);
632+
633+
// Set route if connection_id is provided
634+
if let Some(conn_id) = connection_id {
635+
tracing::debug!(
636+
%service_name,
637+
%method_name,
638+
%method_subscription_name,
639+
connection_id = conn_id,
640+
"Setting route before creating session"
641+
);
642+
643+
if let Err(e) = app.set_route(&method_subscription_name, conn_id).await {
644+
tracing::warn!(
645+
%method_subscription_name,
646+
connection_id = conn_id,
647+
error = %e,
648+
"Failed to set route"
649+
);
650+
}
651+
}
622652

623-
// Set route if connection_id is provided
624-
if let Some(conn_id) = self.inner.connection_id {
653+
// Create the session with optional connection ID for propagation
625654
tracing::debug!(
626655
%service_name,
627656
%method_name,
628657
%method_subscription_name,
629-
connection_id = conn_id,
630-
"Setting route before creating session"
658+
connection_id = ?connection_id,
659+
"Creating session"
631660
);
632661

633-
self.inner
634-
.app
635-
.set_route(&method_subscription_name, conn_id)
662+
// Create session configuration with deadline metadata
663+
let slim_config = slim_session::session_config::SessionConfig {
664+
session_type: ProtoSessionType::PointToPoint,
665+
mls_enabled: false,
666+
max_retries: Some(3),
667+
interval: Some(Duration::from_secs(1)),
668+
initiator: true,
669+
metadata: ctx.metadata().as_map().clone(),
670+
};
671+
672+
// Create session to the method-specific subscription name
673+
let (session_ctx, completion) = app
674+
.create_session(slim_config, method_subscription_name.clone(), None)
636675
.await
637-
.map_err(|e| Status::internal(format!("Failed to set route: {}", e)))?;
638-
}
676+
.map_err(|e| Status::unavailable(format!("Failed to create session: {}", e)))?;
639677

640-
// Create the session with optional connection ID for propagation
641-
tracing::debug!(
642-
%service_name,
643-
%method_name,
644-
%method_subscription_name,
645-
connection_id = ?self.inner.connection_id,
646-
"Creating session"
647-
);
648-
649-
// Create session configuration with deadline metadata
650-
let slim_config = slim_session::session_config::SessionConfig {
651-
session_type: ProtoSessionType::PointToPoint,
652-
mls_enabled: false,
653-
max_retries: Some(3),
654-
interval: Some(Duration::from_secs(1)),
655-
initiator: true,
656-
metadata: ctx.metadata().as_map().clone(),
657-
};
658-
659-
// Create session to the method-specific subscription name
660-
let (session_ctx, completion) = self
661-
.inner
662-
.app
663-
.create_session(slim_config, method_subscription_name.clone(), None)
664-
.await
665-
.map_err(|e| Status::unavailable(format!("Failed to create session: {}", e)))?;
678+
// Wait for session handshake completion
679+
completion
680+
.await
681+
.map_err(|e| Status::unavailable(format!("Session handshake failed: {}", e)))?;
666682

667-
// Wait for session handshake completion
668-
completion
669-
.await
670-
.map_err(|e| Status::unavailable(format!("Session handshake failed: {}", e)))?;
683+
// Wrap the session context for RPC operations
684+
let session = Session::new(session_ctx);
671685

672-
// Wrap the session context for RPC operations
673-
let session = Session::new(session_ctx);
686+
Ok(session)
687+
});
674688

675-
Ok(session)
689+
// Await the spawned task
690+
handle
691+
.await
692+
.map_err(|e| Status::internal(format!("Session creation task failed: {}", e)))?
676693
}
677694

678695
/// Create a context for a specific RPC call with deadline

0 commit comments

Comments
 (0)