Struct jupiter::commands::Dispatcher

source ·
pub struct Dispatcher { /* private fields */ }
Expand description

Provides a readonly view of a CommandDictionary used to actually dispatch calls of the appropriate queue.

Being a readonly copy, a dispatch can operate without any locking or synchronization overheads.

Implementations§

source§

impl Dispatcher

source

pub async fn invoke( &mut self, request: Request, connection: Option<&Arc<Connection>> ) -> Result<BytesMut, OutputError>

Actually dispatches the given request to the appropriate queue and returns the result.

The result is either a RESP response already marshalled into a byte buffer or an error. Note that application level errors have already been marshalled into RESP errors like “-CLIENT: ….” or “-SERVER: ….”. These errors which are explicitly reported here always signal that the protocol or IO channel might be inconsistent and therefore that the underlying connection to the client should be terminated.

If this command has been issued via a Connection, a reference to it can be passed in. Otherwise (which is most probably testing), None can be used. This connection is only used by built-in commands (QUIT and CLIENT) as it has to interact with the inner workings of the server.

We could always check for these commands before dispatching, but this would have a direct impact on latency and these commands are rare and not performance critical at all. Therefore we delay checking for them as much as possible.

§Example

// Create a new platform with a commands dictionary and a SYS.COMMANDS command...
let platform = Builder::new()
                        .enable_server()
                        .enable_commands()
                        .enable_core_commands()
                        .build()
                        .await;

// Obtain the dispatcher...
let mut dispatcher = platform.require::<CommandDictionary>().dispatcher();

// Create a "SYS.COMMANDS" request...
let request = Request::example(vec!("SYS.COMMANDS"));
// Actually invoke the command via the dispatcher...
let result = dispatcher.invoke(request, None).await;
// We expect to receive a PONG result for our PING command...
assert_eq!(result.is_ok(), true);    

// Create a "PING" request...
let request = Request::example(vec!("PING"));
// Actually invoke the command via the dispatcher...
let result = dispatcher.invoke(request, None).await.unwrap();
// We expect to receive a PONG result for our PING command...
assert_eq!(std::str::from_utf8(&result[..]).unwrap(), "+PONG\r\n");

Auto Trait Implementations§

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> 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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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<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