Skip to content

Commit 3bb1e6d

Browse files
ilyaaayadrianbenavides
authored andcommitted
refactor(rust): use localhost constructor for HostnamePort
1 parent d59dbfc commit 3bb1e6d

File tree

8 files changed

+50
-45
lines changed

8 files changed

+50
-45
lines changed

examples/rust/get_started/examples/11-attribute-based-authentication-control-plane.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ async fn start_node(ctx: Context, project_information_path: &str, token: OneTime
109109
// 4. create a tcp outlet with the above policy
110110
tcp.create_outlet(
111111
"outlet",
112-
HostnamePort::new("127.0.0.1", 5000)?,
112+
HostnamePort::localhost(5000),
113113
TcpOutletOptions::new()
114114
.with_incoming_access_control_impl(incoming_access_control)
115115
.with_outgoing_access_control_impl(outgoing_access_control),

implementations/rust/ockam/ockam_api/src/kafka/tests/integration_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ async fn producer__flow_with_mock_kafka__content_encryption_and_decryption(
156156
let mut consumer_mock_kafka = TcpServerSimulator::start("127.0.0.1:0").await;
157157
handle.tcp.create_outlet(
158158
"kafka_consumer_outlet",
159-
HostnamePort::new("127.0.0.1", consumer_mock_kafka.port)?,
159+
HostnamePort::localhost(consumer_mock_kafka.port),
160160
TcpOutletOptions::new(),
161161
)?;
162162

@@ -173,7 +173,7 @@ async fn producer__flow_with_mock_kafka__content_encryption_and_decryption(
173173
let mut producer_mock_kafka = TcpServerSimulator::start("127.0.0.1:0").await;
174174
handle.tcp.create_outlet(
175175
"kafka_producer_outlet",
176-
HostnamePort::new("127.0.0.1", producer_mock_kafka.port)?,
176+
HostnamePort::localhost(producer_mock_kafka.port),
177177
TcpOutletOptions::new(),
178178
)?;
179179
let request =
@@ -207,7 +207,7 @@ async fn producer__flow_with_mock_kafka__content_encryption_and_decryption(
207207
let mut consumer_mock_kafka = TcpServerSimulator::start("127.0.0.1:0").await;
208208
handle.tcp.create_outlet(
209209
"kafka_consumer_outlet",
210-
HostnamePort::new("127.0.0.1", consumer_mock_kafka.port)?,
210+
HostnamePort::localhost(consumer_mock_kafka.port),
211211
TcpOutletOptions::new(),
212212
)?;
213213

implementations/rust/ockam/ockam_api/src/nodes/registry.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,6 @@ mod tests {
339339
}
340340

341341
fn outlet_info(worker_addr: Address) -> OutletInfo {
342-
OutletInfo::new(
343-
HostnamePort::new("127.0.0.1", 0).unwrap(),
344-
Some(&worker_addr),
345-
true,
346-
)
342+
OutletInfo::new(HostnamePort::localhost(0), Some(&worker_addr), true)
347343
}
348344
}

implementations/rust/ockam/ockam_api/src/rendezvous_healthcheck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl RendezvousHealthcheck {
2323
udp_socket_address: SocketAddr,
2424
) -> Result<Self> {
2525
let peer = if udp_socket_address.ip().is_unspecified() {
26-
HostnamePort::new("localhost", udp_socket_address.port())?.to_string()
26+
HostnamePort::localhost(udp_socket_address.port()).to_string()
2727
} else {
2828
udp_socket_address.to_string()
2929
};

implementations/rust/ockam/ockam_api/tests/latency.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub fn measure_buffer_latency_two_nodes_portal() {
150150
.node_manager
151151
.create_inlet(
152152
&first_node.context,
153-
HostnamePort::new("127.0.0.1", 0)?,
153+
HostnamePort::localhost(0),
154154
route![],
155155
route![],
156156
second_node_listen_address

implementations/rust/ockam/ockam_api/tests/portals.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async fn inlet_outlet_local_successful(context: &mut Context) -> ockam::Result<(
4646
.node_manager
4747
.create_inlet(
4848
context,
49-
HostnamePort::new("127.0.0.1", 0)?,
49+
HostnamePort::localhost(0),
5050
route![],
5151
route![],
5252
MultiAddr::from_str("/secure/api/service/outlet")?,
@@ -122,7 +122,7 @@ fn portal_node_goes_down_reconnect() {
122122
.node_manager
123123
.create_inlet(
124124
&first_node.context,
125-
HostnamePort::new("127.0.0.1", 0)?,
125+
HostnamePort::localhost(0),
126126
route![],
127127
route![],
128128
second_node_listen_address
@@ -280,7 +280,7 @@ fn portal_low_bandwidth_connection_keep_working_for_60s() {
280280
.node_manager
281281
.create_inlet(
282282
&first_node.context,
283-
HostnamePort::new("127.0.0.1", 0)?,
283+
HostnamePort::localhost(0),
284284
route![],
285285
route![],
286286
InternetAddress::from(passthrough_server_handle.chosen_addr)
@@ -394,7 +394,7 @@ fn portal_heavy_load_exchanged() {
394394
.node_manager
395395
.create_inlet(
396396
&first_node.context,
397-
HostnamePort::new("127.0.0.1", 0)?,
397+
HostnamePort::localhost(0),
398398
route![],
399399
route![],
400400
second_node_listen_address
@@ -547,7 +547,7 @@ fn test_portal_payload_transfer(outgoing_disruption: Disruption, incoming_disrup
547547
.node_manager
548548
.create_inlet(
549549
&first_node.context,
550-
HostnamePort::new("127.0.0.1", 0)?,
550+
HostnamePort::localhost(0),
551551
route![],
552552
route![],
553553
InternetAddress::from(passthrough_server_handle.chosen_addr)

implementations/rust/ockam/ockam_command/src/kafka/mod.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,31 @@ pub(crate) mod outlet;
1111
pub(crate) mod producer;
1212
pub(crate) mod util;
1313

14-
const KAFKA_DEFAULT_CONSUMER_SERVER: StaticHostnamePort =
15-
StaticHostnamePort::new("127.0.0.1", 4000);
16-
const KAFKA_DEFAULT_PRODUCER_SERVER: StaticHostnamePort =
17-
StaticHostnamePort::new("127.0.0.1", 5000);
14+
const KAFKA_DEFAULT_BOOTSTRAP_ADDRESS: StaticHostnamePort = StaticHostnamePort::localhost(9092);
15+
fn kafka_default_outlet_server() -> SchemeHostnamePort {
16+
KAFKA_DEFAULT_BOOTSTRAP_ADDRESS.try_into().unwrap()
17+
}
18+
19+
const KAFKA_DEFAULT_PROJECT_ROUTE: &str = "/project/default";
20+
fn kafka_default_project_route() -> MultiAddr {
21+
MultiAddr::from_str(KAFKA_DEFAULT_PROJECT_ROUTE).expect("Failed to parse default project route")
22+
}
1823

24+
const KAFKA_DEFAULT_CONSUMER_SERVER: StaticHostnamePort = StaticHostnamePort::localhost(4000);
1925
fn kafka_default_consumer_server() -> SchemeHostnamePort {
2026
KAFKA_DEFAULT_CONSUMER_SERVER.try_into().unwrap()
2127
}
28+
29+
const KAFKA_DEFAULT_INLET_BIND_ADDRESS: StaticHostnamePort = StaticHostnamePort::localhost(4000);
30+
fn kafka_default_inlet_bind_address() -> SchemeHostnamePort {
31+
KAFKA_DEFAULT_INLET_BIND_ADDRESS.try_into().unwrap()
32+
}
33+
34+
const KAFKA_DEFAULT_PRODUCER_SERVER: StaticHostnamePort = StaticHostnamePort::localhost(5000);
2235
fn kafka_default_producer_server() -> SchemeHostnamePort {
2336
KAFKA_DEFAULT_PRODUCER_SERVER.try_into().unwrap()
2437
}
2538

26-
const KAFKA_DEFAULT_BOOTSTRAP_ADDRESS: StaticHostnamePort =
27-
StaticHostnamePort::new("127.0.0.1", 9092);
28-
const KAFKA_DEFAULT_PROJECT_ROUTE: &str = "/project/default";
29-
const KAFKA_DEFAULT_INLET_BIND_ADDRESS: StaticHostnamePort =
30-
StaticHostnamePort::new("127.0.0.1", 4000);
31-
3239
fn kafka_default_outlet_addr() -> String {
3340
DefaultAddress::KAFKA_OUTLET.to_string()
3441
}
@@ -37,18 +44,6 @@ fn kafka_inlet_default_addr() -> String {
3744
DefaultAddress::KAFKA_INLET.to_string()
3845
}
3946

40-
fn kafka_default_project_route() -> MultiAddr {
41-
MultiAddr::from_str(KAFKA_DEFAULT_PROJECT_ROUTE).expect("Failed to parse default project route")
42-
}
43-
44-
fn kafka_default_outlet_server() -> SchemeHostnamePort {
45-
KAFKA_DEFAULT_BOOTSTRAP_ADDRESS.try_into().unwrap()
46-
}
47-
48-
fn kafka_default_inlet_bind_address() -> SchemeHostnamePort {
49-
KAFKA_DEFAULT_INLET_BIND_ADDRESS.try_into().unwrap()
50-
}
51-
5247
pub(crate) fn make_brokers_port_range<T: Into<HostnamePort>>(bootstrap_server: T) -> PortRange {
5348
let boostrap_server_port = bootstrap_server.into().port() as u32;
5449
let start = min(boostrap_server_port + 1, u16::MAX as u32) as u16;

implementations/rust/ockam/ockam_transport_core/src/hostname_port.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ impl StaticHostnamePort {
2121
pub const fn new(hostname: &'static str, port: u16) -> Self {
2222
Self { hostname, port }
2323
}
24+
25+
pub const fn localhost(port: u16) -> Self {
26+
Self {
27+
hostname: "127.0.0.1",
28+
port,
29+
}
30+
}
2431
}
2532

2633
impl TryFrom<StaticHostnamePort> for HostnamePort {
@@ -169,6 +176,13 @@ impl HostnamePort {
169176
port,
170177
})
171178
}
179+
180+
pub fn localhost(port: u16) -> Self {
181+
Self {
182+
hostname: "127.0.0.1".into(),
183+
port,
184+
}
185+
}
172186
}
173187

174188
impl From<SocketAddr> for HostnamePort {
@@ -226,12 +240,12 @@ impl FromStr for HostnamePort {
226240
fn from_str(hostname_port: &str) -> ockam_core::Result<HostnamePort> {
227241
// edge case: only the port is given
228242
if let Ok(port) = hostname_port.parse::<u16>() {
229-
return HostnamePort::new("127.0.0.1", port);
243+
return Ok(HostnamePort::localhost(port));
230244
}
231245

232246
if let Some(port_str) = hostname_port.strip_prefix(':') {
233247
if let Ok(port) = port_str.parse::<u16>() {
234-
return HostnamePort::new("127.0.0.1", port);
248+
return Ok(HostnamePort::localhost(port));
235249
}
236250
}
237251

@@ -258,16 +272,16 @@ mod tests {
258272
#[test]
259273
fn hostname_port_valid_inputs() -> ockam_core::Result<()> {
260274
let valid_cases = vec![
261-
("localhost:80", HostnamePort::new("localhost", 80)?),
275+
("localhost:80", HostnamePort::localhost(80)),
262276
("33domain:80", HostnamePort::new("33domain", 80)?),
263-
("127.0.0.1:80", HostnamePort::new("127.0.0.1", 80)?),
277+
("127.0.0.1:80", HostnamePort::localhost(80)),
264278
("xn--74h.com:80", HostnamePort::new("xn--74h.com", 80)?),
265279
(
266280
"sub.xn_74h.com:80",
267281
HostnamePort::new("sub.xn_74h.com", 80)?,
268282
),
269-
(":80", HostnamePort::new("127.0.0.1", 80)?),
270-
("80", HostnamePort::new("127.0.0.1", 80)?),
283+
(":80", HostnamePort::localhost(80)),
284+
("80", HostnamePort::localhost(80)),
271285
(
272286
"[2001:db8:85a3::8a2e:370:7334]:8080",
273287
HostnamePort::new("[2001:db8:85a3::8a2e:370:7334]", 8080)?,
@@ -286,7 +300,7 @@ mod tests {
286300
let socket_address_cases = vec![
287301
(
288302
SocketAddr::from_str("127.0.0.1:8080").unwrap(),
289-
HostnamePort::new("127.0.0.1", 8080)?,
303+
HostnamePort::localhost(8080),
290304
),
291305
(
292306
SocketAddr::from_str("[2001:db8:85a3::8a2e:370:7334]:8080").unwrap(),

0 commit comments

Comments
 (0)