1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//! Call options for requests
//!
//! See [`CallOpt`] for more details.
use std::time::Duration;
use faststr::FastStr;
use metainfo::{FastStrMap, TypeMap};
use volo::{client::Apply, context::Context};
use crate::{context::ClientContext, error::ClientError};
/// Call options for requests
#[derive(Debug, Default)]
pub struct CallOpt {
/// Timeout of the whole request
///
/// This timeout includes connect, sending request headers, receiving response headers, but
/// without receiving streaming data.
pub timeout: Option<Duration>,
/// Additional information of the endpoint.
///
/// Users can use `tags` to store custom data, such as the datacenter name or the region name,
/// which can be used by the service discoverer.
pub tags: TypeMap,
/// A optimized typemap for storing additional information of the endpoint.
///
/// Use [`FastStrMap`] instead of [`TypeMap`] can reduce the Box allocation.
///
/// This is mainly for performance optimization.
pub faststr_tags: FastStrMap,
}
impl CallOpt {
/// Create a new [`CallOpt`].
#[inline]
pub fn new() -> Self {
Self::default()
}
/// Set a timeout for the [`CallOpt`].
pub fn set_timeout(&mut self, timeout: Duration) {
self.timeout = Some(timeout);
}
/// Consume current [`CallOpt`] and return a new one with the given timeout.
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
/// Check if [`CallOpt`] tags contain entry.
#[inline]
pub fn contains<T: 'static>(&self) -> bool {
self.tags.contains::<T>()
}
/// Insert a tag into this [`CallOpt`].
#[inline]
pub fn insert<T: Send + Sync + 'static>(&mut self, val: T) {
self.tags.insert(val);
}
/// Consume current [`CallOpt`] and return a new one with the tag.
#[inline]
pub fn with<T: Send + Sync + 'static>(mut self, val: T) -> Self {
self.tags.insert(val);
self
}
/// Get a reference to a tag previously inserted on this [`CallOpt`].
#[inline]
pub fn get<T: 'static>(&self) -> Option<&T> {
self.tags.get::<T>()
}
/// Check if [`CallOpt`] tags contain entry.
#[inline]
pub fn contains_faststr<T: 'static>(&self) -> bool {
self.faststr_tags.contains::<T>()
}
/// Insert a tag into this [`CallOpt`].
#[inline]
pub fn insert_faststr<T: Send + Sync + 'static>(&mut self, val: FastStr) {
self.faststr_tags.insert::<T>(val);
}
/// Consume current [`CallOpt`] and return a new one with the tag.
#[inline]
pub fn with_faststr<T: Send + Sync + 'static>(mut self, val: FastStr) -> Self {
self.faststr_tags.insert::<T>(val);
self
}
/// Get a reference to a tag previously inserted on this [`CallOpt`].
#[inline]
pub fn get_faststr<T: 'static>(&self) -> Option<&FastStr> {
self.faststr_tags.get::<T>()
}
}
impl Apply<ClientContext> for CallOpt {
type Error = ClientError;
fn apply(self, cx: &mut ClientContext) -> Result<(), Self::Error> {
{
let callee = cx.rpc_info_mut().callee_mut();
if !self.tags.is_empty() {
callee.tags.extend(self.tags);
}
if !self.faststr_tags.is_empty() {
callee.faststr_tags.extend(self.faststr_tags);
}
}
{
let config = cx.rpc_info_mut().config_mut();
if self.timeout.is_some() {
config.set_timeout(self.timeout);
}
}
Ok(())
}
}