Skip to main content

FastCaller

Struct FastCaller 

Source
pub struct FastCaller { /* private fields */ }
Expand description

Sends fast-lane tasks and waits for their replies.

FastCaller::call adds the task to the fast task stream with a reply key of its own and a deadline, blocks on the reply key, deletes it, and returns the result, or Error::Timeout when none came in time. See FastWorker for a full exchange.

use std::time::Duration;
use legit_streams::{Error, FastCaller, MemoryStreams, Task};

// No worker serves this stream, so the call times out...
let caller = FastCaller::new(MemoryStreams::new(), "hash_tasks:fast", "hash");
let task = Task::builder("app", "query", "0").field("text_data", "hello").build()?;
match caller.call(&task, Duration::from_millis(5)) {
    Err(Error::Timeout { .. }) => { /* fall back */ }
    other => panic!("{other:?}"),
}

Implementations§

Source§

impl FastCaller

Source

pub fn new( streams: impl Streams + 'static, stream: impl Into<String>, kind: impl Into<String>, ) -> Self

A caller sending to the fast task stream stream, for the fast lane of kind, the {kind} of its reply keys, on the system clock. The kind has no default: a call checks it with is_valid_kind.

use legit_streams::{FastCaller, MemoryStreams};

let caller = FastCaller::new(MemoryStreams::new(), "hash_tasks:fast", "hash");
Source

pub fn clock(self, clock: Clock) -> Self

Take deadlines from clock rather than the system clock, such as a manual clock in tests.

use legit_streams::{Clock, FastCaller, MemoryStreams};

let caller = FastCaller::new(MemoryStreams::new(), "hash_tasks:fast", "hash").clock(Clock::manual(0));
Source

pub const fn limits(self, limits: Limits) -> Self

Set the bounds on a reply entry.

use legit_streams::{FastCaller, Limits, MemoryStreams};

let limits = Limits { max_entry_bytes: 1024 * 1024, ..Limits::default() };
let caller = FastCaller::new(MemoryStreams::new(), "hash_tasks:fast", "hash").limits(limits);
Source

pub fn call(&self, task: &Task, timeout: Duration) -> Result<TaskResult>

Send task and wait up to timeout for its result.

The task goes to the fast task stream with two fields set, replacing any it has: reply_to, its reply key rpc_reply_to(source, kind, task_id), right after v, and deadline_ms, the caller’s clock plus timeout in Unix milliseconds, last. The call then reads the reply key from its start, XREAD … BLOCK <ms> STREAMS <key> 0: the key is this call’s alone, so a reply that arrived before the read began is the first entry there, and none is missed. Once a reply is read, or the wait ends, the key is deleted. A timeout under a millisecond waits a millisecond.

A reply counts only when it’s a result whose task_id is the task’s own and whose status is exactly success or error. Anything else on the key isn’t a reply: the call logs it, skips it and goes on reading the key until the deadline, so a stray entry ends as a timeout, never as a result.

The result comes back as it was written, success or error: check TaskResult::is_success.

The task’s id must carry at least 64 random bits, as the {app}_{ULID} id the builder generates does: the reply key is named after it, and an id another party can guess is a key it can answer. A caller that sets its own id with TaskBuilder::task_id owns that.

§Errors
  • Error::Timeout when no reply came within timeout. The call has deleted its task from the stream, so no worker starts it, and its reply key, and a reply that comes later expires with the key. Fall back.
  • Error::InvalidConfig when the caller’s stream begins with results: or dead:, and Error::InvalidValue when the reply key wouldn’t pass is_valid_rpc_reply_to, such as for a caller whose kind fails is_valid_kind; nothing is sent.
  • Error::TooLarge when the request, the task with reply_to, deadline_ms and, for a task read without one, v added, is over the default Limits; nothing is sent.
  • Error::Redis when the task can’t be sent or the key can’t be read; after a failed read the task and key are deleted as on a timeout.
use std::time::Duration;
use legit_streams::{Error, FastCaller, MemoryStreams, Task};

let caller = FastCaller::new(MemoryStreams::new(), "hash_tasks:fast", "Text");
let task = Task::builder("app", "query", "0").build()?;
assert!(matches!(caller.call(&task, Duration::from_secs(1)), Err(Error::InvalidValue { .. })));

let caller = FastCaller::new(MemoryStreams::new(), "results:app:hash", "hash");
assert!(matches!(caller.call(&task, Duration::from_secs(1)), Err(Error::InvalidConfig(_))));

Trait Implementations§

Source§

impl Clone for FastCaller

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for FastCaller

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for FastCaller

§

impl !UnwindSafe for FastCaller

§

impl Forget for FastCaller

§

impl Freeze for FastCaller
where *const ArcInner<dyn Streams> is !null: Freeze, PhantomData<ArcInner<dyn Streams>>: Freeze, *const u8 is !null: Freeze, PhantomData<u8>: Freeze, *const ArcInner<Inner> is !null: Freeze, PhantomData<ArcInner<Inner>>: Freeze,

§

impl Move for FastCaller

§

impl Send for FastCaller
where Arc<dyn Streams>: Send, RawVecInner: Send, Arc<Inner>: Send,

§

impl Sync for FastCaller
where Arc<dyn Streams>: Sync, RawVecInner: Sync, Arc<Inner>: Sync,

§

impl Unpin for FastCaller
where Arc<dyn Streams>: Unpin, *const u8 is !null: Unpin, Arc<Inner>: Unpin,

§

impl UnsafeUnpin for FastCaller
where *const ArcInner<dyn Streams> is !null: UnsafeUnpin, PhantomData<ArcInner<dyn Streams>>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<u8>: UnsafeUnpin, *const ArcInner<Inner> is !null: UnsafeUnpin, PhantomData<ArcInner<Inner>>: UnsafeUnpin,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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

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

fn try_from(value: U) -> Result<T, !>

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