create_request_reply

Function create_request_reply 

Source
pub fn create_request_reply<T>() -> (ResponseChannel<T>, Receiver<T>)
Expand description

Create a request-reply pair with proper channel wrapping

This is a convenience function that creates both sides of a request-reply communication pattern. The sender is wrapped in Arc<Mutex<Option<...>>> for use in agent messages, while the receiver is returned directly for awaiting the response.

§Returns

A tuple of (ResponseChannel<T>, oneshot::Receiver<T>) where:

  • The ResponseChannel should be included in your request message
  • The Receiver should be awaited by the web handler

§Example

use crate::agents::request_reply::create_request_reply;

let (response_tx, rx) = create_request_reply();
let request = MyRequest {
    data: "foo".to_string(),
    response_tx,
};

// Send request to agent
agent_handle.send(request).await;

// Wait for response
let response = rx.await.expect("Agent dropped response channel");