Skip to main content

Route

Struct Route 

Source
pub struct Route {
    pub input: Endpoint,
    pub output: Endpoint,
    pub options: RouteOptions,
}
Expand description

Defines a single message processing route from an input to an output.

Fields§

§input: Endpoint

The input/source endpoint for the route.

§output: Endpoint

The output/sink endpoint for the route.

§options: RouteOptions

(Optional) Fine-tuning options for the route’s execution.

Implementations§

Source§

impl Route

Source

pub fn new(input: Endpoint, output: Endpoint) -> Self

Creates a new route with default concurrency (1) and batch size (128).

§Arguments
  • input - The input/source endpoint for the route
  • output - The output/sink endpoint for the route
Source

pub fn get(name: &str) -> Option<Self>

Retrieves a registered (and running) route by name.

Source

pub fn list() -> Vec<String>

Returns a list of all registered route names.

Source

pub async fn deploy(&self, name: &str) -> Result<()>

Registers the route and starts it. If a route with the same name is already running, it will be stopped first.

§Examples
use mq_bridge::{Route, models::Endpoint};

let route = Route::new(Endpoint::new_memory("in", 10), Endpoint::new_memory("out", 10));
route.deploy("global_route").await.unwrap();
assert!(Route::get("global_route").is_some());
Source

pub async fn stop(name: &str) -> bool

Stops a running route by name and removes it from the registry.

Source

pub async fn create_publisher(&self) -> Result<Publisher>

Creates a new Publisher configured for this route’s output. This is useful if you want to send messages to the same destination as this route.

§Examples
use mq_bridge::{Route, models::Endpoint};

let route = Route::new(Endpoint::new_memory("in", 10), Endpoint::new_memory("out", 10));
let publisher = route.create_publisher().await;
assert!(publisher.is_ok());
Source

pub async fn connect_to_output( &self, name: &str, ) -> Result<Box<dyn MessageConsumer>>

Creates a consumer connected to the route’s output. This is primarily useful for integration tests to verify messages reaching the destination.

Source

pub fn check( &self, name: &str, allowed_endpoints: Option<&[&str]>, ) -> Result<()>

Validates the route configuration, checking if endpoints are supported and correctly configured. Core types like file, memory, and response are always supported.

§Arguments
  • name - The name of the route
  • allowed_endpoints - An optional list of allowed endpoint types
Source

pub async fn run(&self, name_str: &str) -> Result<RouteHandle>

Runs the message processing route with concurrency, error handling, and graceful shutdown.

This function spawns the necessary background tasks to process messages. It waits asynchronously until the route is successfully initialized (i.e., connections are established) or until a timeout occurs. The name_str parameter is just used for logging and tracing.

It returns a JoinHandle for the main route task and a Sender channel that can be used to signal a graceful shutdown. The result is typically converted into a RouteHandle for easier management.

§Examples
let route = Route::new(Endpoint::new_memory("in", 10), Endpoint::new_memory("out", 10));

// Start the route (blocks until initialized) and convert to RouteHandle
let handle: RouteHandle = route.run("my_route").await?.into();

// Stop the route later
handle.stop().await;
handle.join().await?;
Source

pub async fn run_until_err( &self, name: &str, shutdown_rx: Option<Receiver<()>>, ready_tx: Option<Sender<()>>, ) -> Result<bool>

The core logic of running the route, designed to be called within a reconnect loop.

Source

pub fn with_options(self, options: RouteOptions) -> Self

Source

pub fn with_concurrency(self, concurrency: usize) -> Self

Source

pub fn with_batch_size(self, batch_size: usize) -> Self

Source

pub fn with_commit_concurrency_limit(self, limit: usize) -> Self

Source

pub fn with_handler(self, handler: impl Handler + 'static) -> Self

Source

pub fn add_handler<T, H, Args>(self, type_name: &str, handler: H) -> Self
where T: DeserializeOwned + Send + Sync + 'static, H: IntoTypedHandler<T, Args>, Args: Send + Sync + 'static,

Registers a typed handler for the route.

The handler can accept either:

  • fn(T) -> Future<Output = Result<Handled, HandlerError>>
  • fn(T, MessageContext) -> Future<Output = Result<Handled, HandlerError>>
§Examples

#[derive(Deserialize)]
struct MyData {
    id: u32,
}

async fn my_handler(data: MyData) -> Result<Handled, HandlerError> {
    Ok(Handled::Ack)
}

let route = Route::new(Endpoint::new_memory("in", 10), Endpoint::new_memory("out", 10))
    .add_handler("my_type", my_handler);
Source

pub fn add_handlers<T, H, Args>(self, handlers: HashMap<&str, H>) -> Self
where T: DeserializeOwned + Send + Sync + 'static, H: IntoTypedHandler<T, Args>, Args: Send + Sync + 'static,

Trait Implementations§

Source§

impl Clone for Route

Source§

fn clone(&self) -> Route

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Route

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Route

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Route

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Route

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Route

§

impl !RefUnwindSafe for Route

§

impl Send for Route

§

impl Sync for Route

§

impl Unpin for Route

§

impl !UnwindSafe for Route

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,