Skip to content

Commit d4f72c1

Browse files
authored
inbound: Simplify protocol-detection skipping (#1031)
The inbound proxy's layer that switches between routed and passthru stacks uses an unnecessary explict type for its predicate. In order to setup for further changes to this stack, this change eliminate the specialized type in favor of a closure. We also ditch the use of IndexSet as there's no functional need for it.
1 parent a254414 commit d4f72c1

File tree

3 files changed

+17
-37
lines changed

3 files changed

+17
-37
lines changed

linkerd/app/inbound/src/lib.rs

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,20 @@ use linkerd_app_core::{
2828
proxy::tcp,
2929
serve, svc, tls,
3030
transport::{self, listen::Bind, ClientAddr, Local, OrigDstAddr, Remote, ServerAddr},
31-
Error, NameMatch, ProxyRuntime,
31+
Error, NameMatch, Never, ProxyRuntime,
3232
};
33-
use std::{convert::TryFrom, fmt::Debug, future::Future, time::Duration};
33+
use std::{collections::HashSet, convert::TryFrom, fmt::Debug, future::Future, time::Duration};
3434
use tracing::{debug_span, info_span};
3535

3636
#[derive(Clone, Debug)]
3737
pub struct Config {
3838
pub allow_discovery: NameMatch,
3939
pub proxy: ProxyConfig,
4040
pub require_identity_for_inbound_ports: RequireIdentityForPorts,
41-
pub disable_protocol_detection_for_ports: SkipByPort,
41+
pub disable_protocol_detection_for_ports: HashSet<u16>,
4242
pub profile_idle_timeout: Duration,
4343
}
4444

45-
#[derive(Clone, Debug)]
46-
pub struct SkipByPort(std::sync::Arc<indexmap::IndexSet<u16>>);
47-
4845
#[derive(Clone, Debug)]
4946
pub struct Inbound<S> {
5047
config: Config,
@@ -230,6 +227,7 @@ where
230227
let disable_detect = self.config.disable_protocol_detection_for_ports.clone();
231228
let require_id = self.config.require_identity_for_inbound_ports.clone();
232229
let config = self.config.proxy.clone();
230+
233231
self.clone()
234232
.push_http_router(profiles)
235233
.push_http_server()
@@ -262,15 +260,21 @@ where
262260
))
263261
.instrument(|_: &_| debug_span!("proxy"))
264262
.push_switch(
265-
disable_detect,
263+
move |t: T| {
264+
let OrigDstAddr(addr) = t.param();
265+
if !disable_detect.contains(&addr.port()) {
266+
Ok::<_, Never>(svc::Either::A(t))
267+
} else {
268+
Ok(svc::Either::B(TcpAccept::port_skipped(t)))
269+
}
270+
},
266271
self.clone()
267272
.push_tcp_forward(server_port)
268273
.stack
269274
.push_map_target(TcpEndpoint::from)
270275
.push(self.runtime.metrics.transport.layer_accept())
271-
.push_map_target(TcpAccept::port_skipped)
272-
.check_new_service::<T, _>()
273-
.instrument(|_: &T| debug_span!("forward"))
276+
.check_new_service::<TcpAccept, _>()
277+
.instrument(|_: &TcpAccept| debug_span!("forward"))
274278
.into_inner(),
275279
)
276280
.check_new_service::<T, I>()
@@ -294,30 +298,6 @@ where
294298
}
295299
}
296300

297-
// === impl SkipByPort ===
298-
299-
impl From<indexmap::IndexSet<u16>> for SkipByPort {
300-
fn from(ports: indexmap::IndexSet<u16>) -> Self {
301-
SkipByPort(ports.into())
302-
}
303-
}
304-
305-
impl<T> svc::Predicate<T> for SkipByPort
306-
where
307-
T: svc::Param<OrigDstAddr>,
308-
{
309-
type Request = svc::Either<T, T>;
310-
311-
fn check(&mut self, t: T) -> Result<Self::Request, Error> {
312-
let OrigDstAddr(addr) = t.param();
313-
if !self.0.contains(&addr.port()) {
314-
Ok(svc::Either::A(t))
315-
} else {
316-
Ok(svc::Either::B(t))
317-
}
318-
}
319-
}
320-
321301
fn stack_labels(proto: &'static str, name: &'static str) -> metrics::StackLabels {
322302
metrics::StackLabels::inbound(proto, name)
323303
}

linkerd/app/inbound/src/test_util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Config, RequireIdentityForPorts, SkipByPort};
1+
use crate::{Config, RequireIdentityForPorts};
22
pub use futures::prelude::*;
33
use linkerd_app_core::{
44
config,
@@ -48,7 +48,7 @@ pub fn default_config() -> Config {
4848
detect_protocol_timeout: Duration::from_secs(10),
4949
},
5050
require_identity_for_inbound_ports: RequireIdentityForPorts::from(None),
51-
disable_protocol_detection_for_ports: SkipByPort::from(indexmap::IndexSet::default()),
51+
disable_protocol_detection_for_ports: Default::default(),
5252
profile_idle_timeout: Duration::from_millis(500),
5353
}
5454
}

linkerd/app/src/env.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ pub fn parse_config<S: Strings>(strings: &S) -> Result<super::Config, EnvError>
528528
require_identity_for_inbound_ports: require_identity_for_inbound_ports.into(),
529529
profile_idle_timeout: dst_profile_idle_timeout?
530530
.unwrap_or(DEFAULT_DESTINATION_PROFILE_IDLE_TIMEOUT),
531-
disable_protocol_detection_for_ports: inbound_opaque_ports.into(),
531+
disable_protocol_detection_for_ports: inbound_opaque_ports.into_iter().collect(),
532532
}
533533
};
534534

0 commit comments

Comments
 (0)