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
impl FastCaller
Sourcepub fn new(
streams: impl Streams + 'static,
stream: impl Into<String>,
kind: impl Into<String>,
) -> Self
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");Sourcepub fn clock(self, clock: Clock) -> Self
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));Sourcepub const fn limits(self, limits: Limits) -> Self
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);Sourcepub fn call(&self, task: &Task, timeout: Duration) -> Result<TaskResult>
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::Timeoutwhen no reply came withintimeout. 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::InvalidConfigwhen the caller’s stream begins withresults:ordead:, andError::InvalidValuewhen the reply key wouldn’t passis_valid_rpc_reply_to, such as for a caller whose kind failsis_valid_kind; nothing is sent.Error::TooLargewhen the request, the task withreply_to,deadline_msand, for a task read without one,vadded, is over the defaultLimits; nothing is sent.Error::Rediswhen 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(_))));