litep2p/protocol/request_response/
config.rs1use crate::{
22 codec::ProtocolCodec,
23 protocol::request_response::{
24 handle::{InnerRequestResponseEvent, RequestResponseCommand, RequestResponseHandle},
25 REQUEST_TIMEOUT,
26 },
27 types::protocol::ProtocolName,
28 DEFAULT_CHANNEL_SIZE,
29};
30
31use tokio::sync::mpsc::{channel, Receiver, Sender};
32
33use std::{
34 sync::{atomic::AtomicUsize, Arc},
35 time::Duration,
36};
37
38pub struct Config {
40 pub(crate) protocol_name: ProtocolName,
42
43 pub(crate) fallback_names: Vec<ProtocolName>,
45
46 pub(crate) timeout: Duration,
48
49 pub(crate) codec: ProtocolCodec,
51
52 pub(super) event_tx: Sender<InnerRequestResponseEvent>,
54
55 pub(crate) command_rx: Receiver<RequestResponseCommand>,
57
58 pub(crate) next_request_id: Arc<AtomicUsize>,
60
61 pub(crate) max_concurrent_inbound_request: Option<usize>,
63}
64
65impl Config {
66 pub fn new(
68 protocol_name: ProtocolName,
69 fallback_names: Vec<ProtocolName>,
70 max_message_size: usize,
71 timeout: Duration,
72 max_concurrent_inbound_request: Option<usize>,
73 ) -> (Self, RequestResponseHandle) {
74 let (event_tx, event_rx) = channel(DEFAULT_CHANNEL_SIZE);
75 let (command_tx, command_rx) = channel(DEFAULT_CHANNEL_SIZE);
76 let next_request_id = Default::default();
77 let handle = RequestResponseHandle::new(event_rx, command_tx, Arc::clone(&next_request_id));
78
79 (
80 Self {
81 event_tx,
82 command_rx,
83 protocol_name,
84 fallback_names,
85 next_request_id,
86 timeout,
87 max_concurrent_inbound_request,
88 codec: ProtocolCodec::UnsignedVarint(Some(max_message_size)),
89 },
90 handle,
91 )
92 }
93
94 pub(crate) fn protocol_name(&self) -> &ProtocolName {
96 &self.protocol_name
97 }
98}
99
100pub struct ConfigBuilder {
102 pub(crate) protocol_name: ProtocolName,
104
105 pub(crate) fallback_names: Vec<ProtocolName>,
107
108 max_message_size: Option<usize>,
110
111 timeout: Option<Duration>,
113
114 max_concurrent_inbound_request: Option<usize>,
116}
117
118impl ConfigBuilder {
119 pub fn new(protocol_name: ProtocolName) -> Self {
121 Self {
122 protocol_name,
123 fallback_names: Vec::new(),
124 max_message_size: None,
125 timeout: Some(REQUEST_TIMEOUT),
126 max_concurrent_inbound_request: None,
127 }
128 }
129
130 pub fn with_max_size(mut self, max_message_size: usize) -> Self {
132 self.max_message_size = Some(max_message_size);
133 self
134 }
135
136 pub fn with_fallback_names(mut self, fallback_names: Vec<ProtocolName>) -> Self {
138 self.fallback_names = fallback_names;
139 self
140 }
141
142 pub fn with_timeout(mut self, timeout: Duration) -> Self {
144 self.timeout = Some(timeout);
145 self
146 }
147
148 pub fn with_max_concurrent_inbound_requests(
154 mut self,
155 max_concurrent_inbound_requests: usize,
156 ) -> Self {
157 self.max_concurrent_inbound_request = Some(max_concurrent_inbound_requests);
158 self
159 }
160
161 pub fn build(mut self) -> (Config, RequestResponseHandle) {
163 Config::new(
164 self.protocol_name,
165 self.fallback_names,
166 self.max_message_size.take().expect("maximum message size to be set"),
167 self.timeout.take().expect("timeout to exist"),
168 self.max_concurrent_inbound_request,
169 )
170 }
171}