volo_http/client/
callopt.rs1use std::time::Duration;
6
7use faststr::FastStr;
8use metainfo::{FastStrMap, TypeMap};
9use volo::{client::Apply, context::Context};
10
11use crate::{context::ClientContext, error::ClientError};
12
13#[derive(Debug, Default)]
15pub struct CallOpt {
16 pub timeout: Option<Duration>,
21 pub tags: TypeMap,
26 pub faststr_tags: FastStrMap,
32}
33
34impl CallOpt {
35 #[inline]
37 pub fn new() -> Self {
38 Self::default()
39 }
40
41 pub fn set_timeout(&mut self, timeout: Duration) {
43 self.timeout = Some(timeout);
44 }
45
46 pub fn with_timeout(mut self, timeout: Duration) -> Self {
48 self.timeout = Some(timeout);
49 self
50 }
51
52 #[inline]
54 pub fn contains<T: 'static>(&self) -> bool {
55 self.tags.contains::<T>()
56 }
57
58 #[inline]
60 pub fn insert<T: Send + Sync + 'static>(&mut self, val: T) {
61 self.tags.insert(val);
62 }
63
64 #[inline]
66 pub fn with<T: Send + Sync + 'static>(mut self, val: T) -> Self {
67 self.tags.insert(val);
68 self
69 }
70
71 #[inline]
73 pub fn get<T: 'static>(&self) -> Option<&T> {
74 self.tags.get::<T>()
75 }
76
77 #[inline]
79 pub fn contains_faststr<T: 'static>(&self) -> bool {
80 self.faststr_tags.contains::<T>()
81 }
82
83 #[inline]
85 pub fn insert_faststr<T: Send + Sync + 'static>(&mut self, val: FastStr) {
86 self.faststr_tags.insert::<T>(val);
87 }
88
89 #[inline]
91 pub fn with_faststr<T: Send + Sync + 'static>(mut self, val: FastStr) -> Self {
92 self.faststr_tags.insert::<T>(val);
93 self
94 }
95
96 #[inline]
98 pub fn get_faststr<T: 'static>(&self) -> Option<&FastStr> {
99 self.faststr_tags.get::<T>()
100 }
101}
102
103impl Apply<ClientContext> for CallOpt {
104 type Error = ClientError;
105
106 fn apply(self, cx: &mut ClientContext) -> Result<(), Self::Error> {
107 {
108 let callee = cx.rpc_info_mut().callee_mut();
109 if !self.tags.is_empty() {
110 callee.tags.extend(self.tags);
111 }
112 if !self.faststr_tags.is_empty() {
113 callee.faststr_tags.extend(self.faststr_tags);
114 }
115 }
116 {
117 let config = cx.rpc_info_mut().config_mut();
118 if self.timeout.is_some() {
119 config.set_timeout(self.timeout);
120 }
121 }
122 Ok(())
123 }
124}