-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadaptor.rs
More file actions
178 lines (170 loc) · 4.88 KB
/
Copy pathadaptor.rs
File metadata and controls
178 lines (170 loc) · 4.88 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use super::proc::StubProc;
use crate::{
core::{
adaptor::{Adaptor, MaybeAsync},
error::ProcError,
msg::Tvf,
proc::ProcConfig,
service::ServiceError,
},
maybe_async,
};
extern crate self as prosa;
use crate::otel::metrics::Meter;
/// Adaptator trait for the stub processor
///
/// Need to define the process_request method to know what to do with incoming requests
/// ```
/// use prosa::stub::proc::StubProc;
/// use prosa::core::adaptor::{Adaptor, MaybeAsync};
/// use prosa::stub::adaptor::StubAdaptor;
/// use prosa::core::error::ProcError;
/// use prosa::core::msg::Tvf;
/// use prosa::core::service::ServiceError;
///
/// #[derive(Adaptor)]
/// pub struct MyStubAdaptor { }
///
/// impl<M> StubAdaptor<M> for MyStubAdaptor
/// where
/// M: 'static
/// + std::marker::Send
/// + std::marker::Sync
/// + std::marker::Sized
/// + std::clone::Clone
/// + std::fmt::Debug
/// + Tvf
/// + std::default::Default,
/// {
/// fn new(_proc: &StubProc<M>) -> Result<Self, Box<dyn ProcError + Send + Sync>> {
/// Ok(Self {})
/// }
///
/// fn process_request(&self, service_name: &str, request: M) -> MaybeAsync<Result<M, ServiceError>> {
/// let mut msg = request.clone();
/// msg.put_string(1, format!("test service {}", service_name));
/// Ok(msg).into()
/// }
/// }
/// ```
///
/// You also have the possibility to do an async request processing for your stub adaptor:
/// ```
/// use prosa::stub::proc::StubProc;
/// use prosa::core::adaptor::{Adaptor, MaybeAsync};
/// use prosa::stub::adaptor::StubAdaptor;
/// use prosa::core::error::ProcError;
/// use prosa::core::msg::Tvf;
/// use prosa::core::service::ServiceError;
/// use prosa::maybe_async;
///
/// #[derive(Adaptor)]
/// pub struct MyAsyncStubAdaptor { }
///
/// impl<M> StubAdaptor<M> for MyAsyncStubAdaptor
/// where
/// M: 'static
/// + std::marker::Send
/// + std::marker::Sync
/// + std::marker::Sized
/// + std::clone::Clone
/// + std::fmt::Debug
/// + Tvf
/// + std::default::Default,
/// {
/// fn new(_proc: &StubProc<M>) -> Result<Self, Box<dyn ProcError + Send + Sync>> {
/// Ok(Self {})
/// }
///
/// fn process_request(&self, service_name: &str, request: M) -> MaybeAsync<Result<M, ServiceError>> {
/// let service_name = service_name.to_string();
/// maybe_async!(async move {
/// // You can do async things here
/// let mut msg = request.clone();
/// msg.put_string(1, format!("test service {}", service_name));
/// Ok(msg)
/// })
/// }
/// }
/// ```
pub trait StubAdaptor<M>
where
M: 'static
+ std::marker::Send
+ std::marker::Sync
+ std::marker::Sized
+ std::clone::Clone
+ std::fmt::Debug
+ Tvf
+ std::default::Default,
{
/// Method called when the processor spawns
/// This method is called only once so the processing will be thread safe
fn new(proc: &StubProc<M>) -> Result<Self, Box<dyn ProcError + Send + Sync>>
where
Self: Sized;
/// Method to process incoming requests
fn process_request(
&self,
service_name: &str,
request: M,
) -> MaybeAsync<Result<M, ServiceError>>;
}
/// Parot adaptor for the stub processor. Use to respond to a request with the same message
#[derive(Adaptor)]
pub struct StubParotAdaptor {
#[allow(unused)]
meter: Meter,
}
impl<M> StubAdaptor<M> for StubParotAdaptor
where
M: 'static
+ std::marker::Send
+ std::marker::Sync
+ std::marker::Sized
+ std::clone::Clone
+ std::fmt::Debug
+ Tvf
+ std::default::Default,
{
fn new(proc: &StubProc<M>) -> Result<Self, Box<dyn ProcError + Send + Sync>> {
Ok(Self {
meter: proc.get_proc_param().meter("stub_adaptor"),
})
}
fn process_request(
&self,
_service_name: &str,
request: M,
) -> MaybeAsync<Result<M, ServiceError>> {
Ok(request.clone()).into()
}
}
/// Parot adaptor for the stub processor. Use to respond to a request with the same message
#[derive(Adaptor)]
pub struct StubAsyncParotAdaptor {}
impl<M> StubAdaptor<M> for StubAsyncParotAdaptor
where
M: 'static
+ std::marker::Send
+ std::marker::Sync
+ std::marker::Sized
+ std::clone::Clone
+ std::fmt::Debug
+ Tvf
+ std::default::Default,
{
fn new(_proc: &StubProc<M>) -> Result<Self, Box<dyn ProcError + Send + Sync>> {
Ok(Self {})
}
fn process_request(
&self,
_service_name: &str,
request: M,
) -> MaybeAsync<Result<M, ServiceError>> {
maybe_async!(async move {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
Ok(request)
})
}
}