Skip to main content

temporalio_client/
request_extensions.rs

1//! Request extensions for tonic gRPC requests.
2//!
3//! These types can be inserted into tonic request extensions to modify behavior of retires or other
4//! request handling logic.
5
6use crate::RetryOptions;
7use std::time::Duration;
8
9/// A request extension that, when set, should make the retry behavior consider this call to be a
10/// [CallType::TaskLongPoll](crate::CallType::TaskLongPoll)
11#[derive(Copy, Clone, Debug)]
12pub struct IsWorkerTaskLongPoll;
13
14/// A request extension that, when set, and a call is being processed by a retrying client, allows
15/// the caller to request certain matching errors to short-circuit-return immediately and not follow
16/// normal retry logic.
17#[derive(Copy, Clone, Debug)]
18pub struct NoRetryOnMatching {
19    /// Return true if the passed-in gRPC error should be immediately returned to the caller
20    pub predicate: fn(&tonic::Status) -> bool,
21}
22
23/// A request extension that forces overriding the current retry policy of the [crate::Connection].
24#[derive(Clone, Debug)]
25pub struct RetryConfigForCall(pub RetryOptions);
26
27/// Per-call payload/memo size error limits, attached to a request's extensions by a caller that
28/// wants error-level enforcement on this call.
29#[doc(hidden)]
30#[derive(Debug, Clone, Copy)]
31pub struct PayloadErrorLimits {
32    /// Blob (payload) size error threshold, in bytes.
33    pub blob: usize,
34    /// Memo size error threshold, in bytes.
35    pub memo: usize,
36}
37
38/// Extension trait for tonic requests to set default timeouts
39pub trait RequestExt {
40    /// Set a timeout for a request if one is not already specified in the metadata
41    fn set_default_timeout(&mut self, duration: Duration);
42}
43
44impl<T> RequestExt for tonic::Request<T> {
45    fn set_default_timeout(&mut self, duration: Duration) {
46        if !self.metadata().contains_key("grpc-timeout") {
47            self.set_timeout(duration)
48        }
49    }
50}