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
// Copyright 2023 litep2p developers
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use crate::{
codec::ProtocolCodec,
protocol::request_response::{
handle::{InnerRequestResponseEvent, RequestResponseCommand, RequestResponseHandle},
REQUEST_TIMEOUT,
},
types::protocol::ProtocolName,
DEFAULT_CHANNEL_SIZE,
};
use tokio::sync::mpsc::{channel, Receiver, Sender};
use std::{
sync::{atomic::AtomicUsize, Arc},
time::Duration,
};
/// Request-response protocol configuration.
pub struct Config {
/// Protocol name.
pub(crate) protocol_name: ProtocolName,
/// Fallback names for the main protocol name.
pub(crate) fallback_names: Vec<ProtocolName>,
/// Timeout for outbound requests.
pub(crate) timeout: Duration,
/// Codec used by the protocol.
pub(crate) codec: ProtocolCodec,
/// TX channel for sending events to the user protocol.
pub(super) event_tx: Sender<InnerRequestResponseEvent>,
/// RX channel for receiving commands from the user protocol.
pub(crate) command_rx: Receiver<RequestResponseCommand>,
/// Next ephemeral request ID.
pub(crate) next_request_id: Arc<AtomicUsize>,
/// Maximum number of concurrent inbound requests.
pub(crate) max_concurrent_inbound_request: Option<usize>,
}
impl Config {
/// Create new [`Config`].
pub fn new(
protocol_name: ProtocolName,
fallback_names: Vec<ProtocolName>,
max_message_size: usize,
timeout: Duration,
max_concurrent_inbound_request: Option<usize>,
) -> (Self, RequestResponseHandle) {
let (event_tx, event_rx) = channel(DEFAULT_CHANNEL_SIZE);
let (command_tx, command_rx) = channel(DEFAULT_CHANNEL_SIZE);
let next_request_id = Default::default();
let handle = RequestResponseHandle::new(event_rx, command_tx, Arc::clone(&next_request_id));
(
Self {
event_tx,
command_rx,
protocol_name,
fallback_names,
next_request_id,
timeout,
max_concurrent_inbound_request,
codec: ProtocolCodec::UnsignedVarint(Some(max_message_size)),
},
handle,
)
}
/// Get protocol name.
pub(crate) fn protocol_name(&self) -> &ProtocolName {
&self.protocol_name
}
}
/// Builder for [`Config`].
pub struct ConfigBuilder {
/// Protocol name.
pub(crate) protocol_name: ProtocolName,
/// Fallback names for the main protocol name.
pub(crate) fallback_names: Vec<ProtocolName>,
/// Maximum message size.
max_message_size: Option<usize>,
/// Timeout for outbound requests.
timeout: Option<Duration>,
/// Maximum number of concurrent inbound requests.
max_concurrent_inbound_request: Option<usize>,
}
impl ConfigBuilder {
/// Create new [`ConfigBuilder`].
pub fn new(protocol_name: ProtocolName) -> Self {
Self {
protocol_name,
fallback_names: Vec::new(),
max_message_size: None,
timeout: Some(REQUEST_TIMEOUT),
max_concurrent_inbound_request: None,
}
}
/// Set maximum message size.
pub fn with_max_size(mut self, max_message_size: usize) -> Self {
self.max_message_size = Some(max_message_size);
self
}
/// Set fallback names.
pub fn with_fallback_names(mut self, fallback_names: Vec<ProtocolName>) -> Self {
self.fallback_names = fallback_names;
self
}
/// Set timeout for outbound requests.
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
/// Specify the maximum number of concurrent inbound requests. By default the number of inbound
/// requests is not limited.
///
/// If a new request is received while the number of inbound requests is already at a maximum,
/// the request is dropped.
pub fn with_max_concurrent_inbound_requests(
mut self,
max_concurrent_inbound_requests: usize,
) -> Self {
self.max_concurrent_inbound_request = Some(max_concurrent_inbound_requests);
self
}
/// Build [`Config`].
pub fn build(mut self) -> (Config, RequestResponseHandle) {
Config::new(
self.protocol_name,
self.fallback_names,
self.max_message_size.take().expect("maximum message size to be set"),
self.timeout.take().expect("timeout to exist"),
self.max_concurrent_inbound_request,
)
}
}