Skip to content

Commit 9296492

Browse files
committed
fix rustfmt
1 parent ab2d001 commit 9296492

File tree

5 files changed

+164
-74
lines changed

5 files changed

+164
-74
lines changed

policy-controller/grpc/src/outbound.rs

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,12 @@ fn to_service(outbound: OutboundPolicy) -> outbound::OutboundPolicy {
237237
outbound::failure_accrual::ConsecutiveFailures {
238238
max_failures,
239239
backoff: Some(outbound::ExponentialBackoff {
240-
min_backoff: convert_duration("min_backoff")(backoff.min_penalty),
241-
max_backoff: convert_duration("max_backoff")(backoff.max_penalty),
240+
min_backoff: convert_duration("min_backoff")(
241+
backoff.min_penalty,
242+
),
243+
max_backoff: convert_duration("max_backoff")(
244+
backoff.max_penalty,
245+
),
242246
jitter_ratio: backoff.jitter,
243247
}),
244248
},
@@ -312,33 +316,39 @@ fn convert_outbound_http_route(
312316

313317
let rules = rules
314318
.into_iter()
315-
.map(|HttpRouteRule { matches, backends, request_timeout }| {
316-
let backends = backends
317-
.into_iter()
318-
.map(convert_http_backend)
319-
.collect::<Vec<_>>();
320-
let dist = if backends.is_empty() {
321-
outbound::http_route::distribution::Kind::FirstAvailable(
322-
outbound::http_route::distribution::FirstAvailable {
323-
backends: vec![outbound::http_route::RouteBackend {
324-
backend: Some(backend.clone()),
325-
filters: vec![],
326-
request_timeout: None,
327-
}],
328-
},
329-
)
330-
} else {
331-
outbound::http_route::distribution::Kind::RandomAvailable(
332-
outbound::http_route::distribution::RandomAvailable { backends },
333-
)
334-
};
335-
outbound::http_route::Rule {
336-
matches: matches.into_iter().map(http_route::convert_match).collect(),
337-
backends: Some(outbound::http_route::Distribution { kind: Some(dist) }),
338-
filters: Default::default(),
339-
request_timeout: request_timeout.and_then(convert_duration("request timeout"))
340-
}
341-
})
319+
.map(
320+
|HttpRouteRule {
321+
matches,
322+
backends,
323+
request_timeout,
324+
}| {
325+
let backends = backends
326+
.into_iter()
327+
.map(convert_http_backend)
328+
.collect::<Vec<_>>();
329+
let dist = if backends.is_empty() {
330+
outbound::http_route::distribution::Kind::FirstAvailable(
331+
outbound::http_route::distribution::FirstAvailable {
332+
backends: vec![outbound::http_route::RouteBackend {
333+
backend: Some(backend.clone()),
334+
filters: vec![],
335+
request_timeout: None,
336+
}],
337+
},
338+
)
339+
} else {
340+
outbound::http_route::distribution::Kind::RandomAvailable(
341+
outbound::http_route::distribution::RandomAvailable { backends },
342+
)
343+
};
344+
outbound::http_route::Rule {
345+
matches: matches.into_iter().map(http_route::convert_match).collect(),
346+
backends: Some(outbound::http_route::Distribution { kind: Some(dist) }),
347+
filters: Default::default(),
348+
request_timeout: request_timeout.and_then(convert_duration("request timeout")),
349+
}
350+
},
351+
)
342352
.collect();
343353

344354
outbound::HttpRoute {
@@ -367,7 +377,9 @@ fn convert_http_backend(backend: Backend) -> outbound::http_route::WeightedRoute
367377
)),
368378
}),
369379
filters: Default::default(),
370-
request_timeout: addr.request_timeout.and_then(convert_duration("backend request timeout"))
380+
request_timeout: addr
381+
.request_timeout
382+
.and_then(convert_duration("backend request timeout")),
371383
}),
372384
}
373385
}
@@ -400,7 +412,9 @@ fn convert_http_backend(backend: Backend) -> outbound::http_route::WeightedRoute
400412
)),
401413
}),
402414
filters: Default::default(),
403-
request_timeout: svc.request_timeout.and_then(convert_duration("backend request timeout"))
415+
request_timeout: svc
416+
.request_timeout
417+
.and_then(convert_duration("backend request timeout")),
404418
}),
405419
},
406420
Backend::Invalid { weight, message } => outbound::http_route::WeightedRouteBackend {
@@ -525,14 +539,19 @@ fn default_queue_config() -> outbound::Queue {
525539
}
526540
}
527541

528-
fn convert_duration(name: &'static str) -> impl Fn(time::Duration) -> Option<prost_types::Duration> {
542+
fn convert_duration(
543+
name: &'static str,
544+
) -> impl Fn(time::Duration) -> Option<prost_types::Duration> {
529545
move |duration| {
530-
duration.try_into().map_err(|error| {
531-
tracing::error!(%error, "Invalid {name} duration");
532-
prost_types::Duration {
533-
seconds: 315_576_000_000,
534-
nanos: 999_999_999
535-
}
536-
}).ok()
546+
duration
547+
.try_into()
548+
.map_err(|error| {
549+
tracing::error!(%error, "Invalid {name} duration");
550+
prost_types::Duration {
551+
seconds: 315_576_000_000,
552+
nanos: 999_999_999,
553+
}
554+
})
555+
.ok()
537556
}
538-
}
557+
}

policy-controller/k8s/api/src/duration.rs

Lines changed: 74 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::{str::FromStr, time::Duration, fmt};
2-
use serde::{Serialize, Serializer, Deserialize, Deserializer, de};
1+
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
2+
use std::{fmt, str::FromStr, time::Duration};
33

44
#[derive(Copy, Clone, PartialEq, Eq)]
55
pub struct K8sDuration {
@@ -26,13 +26,13 @@ impl From<Duration> for K8sDuration {
2626
fn from(duration: Duration) -> Self {
2727
Self {
2828
duration,
29-
is_negative: false
29+
is_negative: false,
3030
}
3131
}
3232
}
3333

3434
impl From<K8sDuration> for Duration {
35-
fn from(K8sDuration {duration, .. }: K8sDuration) -> Self {
35+
fn from(K8sDuration { duration, .. }: K8sDuration) -> Self {
3636
duration
3737
}
3838
}
@@ -98,7 +98,8 @@ impl FromStr for K8sDuration {
9898
if let Some(unit_start) = s.find(|c: char| c.is_alphabetic()) {
9999
let (val, rest) = s.split_at(unit_start);
100100
let val = val.parse::<f64>()?;
101-
let unit = if let Some(next_numeric_start) = rest.find(|c: char| !c.is_alphabetic()) {
101+
let unit = if let Some(next_numeric_start) = rest.find(|c: char| !c.is_alphabetic())
102+
{
102103
let (unit, rest) = rest.split_at(next_numeric_start);
103104
s = rest;
104105
unit
@@ -108,14 +109,19 @@ impl FromStr for K8sDuration {
108109
};
109110
total += duration_from_units(val, unit)?;
110111
} else if s == "0" {
111-
return Ok(K8sDuration { duration: Duration::from_secs(0), is_negative });
112+
return Ok(K8sDuration {
113+
duration: Duration::from_secs(0),
114+
is_negative,
115+
});
112116
} else {
113117
return Err(ParseError::NoUnit);
114118
}
115119
}
116120

117-
Ok(K8sDuration { duration: total, is_negative })
118-
121+
Ok(K8sDuration {
122+
duration: total,
123+
is_negative,
124+
})
119125
}
120126
}
121127

@@ -131,7 +137,7 @@ impl Serialize for K8sDuration {
131137
impl<'de> Deserialize<'de> for K8sDuration {
132138
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
133139
where
134-
D: Deserializer<'de>
140+
D: Deserializer<'de>,
135141
{
136142
struct Visitor;
137143
impl<'de> de::Visitor<'de> for Visitor {
@@ -141,7 +147,6 @@ impl<'de> Deserialize<'de> for K8sDuration {
141147
f.write_str("a string in Go `time.Duration.String()` format")
142148
}
143149

144-
145150
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
146151
where
147152
E: de::Error,
@@ -204,18 +209,33 @@ mod tests {
204209
("1478s", Duration::from_secs(1478).into()),
205210
// // sign
206211
// {"-5s", -5 * Second},
207-
("-5s", K8sDuration {duration: Duration::from_secs(5), is_negative: true }),
212+
(
213+
"-5s",
214+
K8sDuration {
215+
duration: Duration::from_secs(5),
216+
is_negative: true,
217+
},
218+
),
208219
// {"+5s", 5 * Second},
209220
("+5s", Duration::from_secs(5).into()),
210221
// {"-0", 0},
211-
("-0", K8sDuration { duration: Duration::from_secs(0), is_negative: true }),
222+
(
223+
"-0",
224+
K8sDuration {
225+
duration: Duration::from_secs(0),
226+
is_negative: true,
227+
},
228+
),
212229
// {"+0", 0},
213230
("+0", Duration::from_secs(0).into()),
214231
// // decimal
215232
// {"5.0s", 5 * Second},
216233
("5s", Duration::from_secs(5).into()),
217234
// {"5.6s", 5*Second + 600*Millisecond},
218-
("5.6s", (Duration::from_secs(5) + Duration::from_millis(600)).into()),
235+
(
236+
"5.6s",
237+
(Duration::from_secs(5) + Duration::from_millis(600)).into(),
238+
),
219239
// {"5.s", 5 * Second},
220240
("5.s", Duration::from_secs(5).into()),
221241
// {".5s", 500 * Millisecond},
@@ -225,11 +245,20 @@ mod tests {
225245
// {"1.00s", 1 * Second},
226246
("1.00s", Duration::from_secs(1).into()),
227247
// {"1.004s", 1*Second + 4*Millisecond},
228-
("1.004s", (Duration::from_secs(1) + Duration::from_millis(4)).into()),
248+
(
249+
"1.004s",
250+
(Duration::from_secs(1) + Duration::from_millis(4)).into(),
251+
),
229252
// {"1.0040s", 1*Second + 4*Millisecond},
230-
("1.0040s", (Duration::from_secs(1) + Duration::from_millis(4)).into()),
253+
(
254+
"1.0040s",
255+
(Duration::from_secs(1) + Duration::from_millis(4)).into(),
256+
),
231257
// {"100.00100s", 100*Second + 1*Millisecond},
232-
("100.00100s", (Duration::from_secs(100) + Duration::from_millis(1)).into()),
258+
(
259+
"100.00100s",
260+
(Duration::from_secs(100) + Duration::from_millis(1)).into(),
261+
),
233262
// // different units
234263
// {"10ns", 10 * Nanosecond},
235264
("10ns", Duration::from_nanos(10).into()),
@@ -251,18 +280,34 @@ mod tests {
251280
// {"3h30m", 3*Hour + 30*Minute},
252281
("3h30m", (3 * HOUR + 30 * MINUTE).into()),
253282
// {"10.5s4m", 4*Minute + 10*Second + 500*Millisecond},
254-
("10.5s4m", (4 * MINUTE + Duration::from_secs(10) + Duration::from_millis(500)).into()),
283+
(
284+
"10.5s4m",
285+
(4 * MINUTE + Duration::from_secs(10) + Duration::from_millis(500)).into(),
286+
),
255287
// {"-2m3.4s", -(2*Minute + 3*Second + 400*Millisecond)},
256-
("-2m3.4s", K8sDuration { duration: 2 * MINUTE + Duration::from_secs(3) + Duration::from_millis(400), is_negative: true }),
288+
(
289+
"-2m3.4s",
290+
K8sDuration {
291+
duration: 2 * MINUTE + Duration::from_secs(3) + Duration::from_millis(400),
292+
is_negative: true,
293+
},
294+
),
257295
// {"1h2m3s4ms5us6ns", 1*Hour + 2*Minute + 3*Second + 4*Millisecond + 5*Microsecond + 6*Nanosecond},
258296
(
259297
"1h2m3s4ms5us6ns",
260-
(1 * HOUR + 2 * MINUTE + Duration::from_secs(3) + Duration::from_millis(4)
261-
+ Duration::from_micros(5) + Duration::from_nanos(6)).into()),
298+
(1 * HOUR
299+
+ 2 * MINUTE
300+
+ Duration::from_secs(3)
301+
+ Duration::from_millis(4)
302+
+ Duration::from_micros(5)
303+
+ Duration::from_nanos(6))
304+
.into(),
305+
),
262306
// {"39h9m14.425s", 39*Hour + 9*Minute + 14*Second + 425*Millisecond},
263307
(
264308
"39h9m14.425s",
265-
(39 * HOUR + 9 * MINUTE + Duration::from_secs(14) + Duration::from_millis(425)).into(),
309+
(39 * HOUR + 9 * MINUTE + Duration::from_secs(14) + Duration::from_millis(425))
310+
.into(),
266311
),
267312
// // large value
268313
// {"52763797000ns", 52763797000 * Nanosecond},
@@ -272,7 +317,10 @@ mod tests {
272317
("0.3333333333333333333h", (20 * MINUTE).into()),
273318
// // 9007199254740993 = 1<<53+1 cannot be stored precisely in a float64
274319
// {"9007199254740993ns", (1<<53 + 1) * Nanosecond},
275-
("9007199254740993ns", Duration::from_nanos((1 << 53) + 1).into()),
320+
(
321+
"9007199254740993ns",
322+
Duration::from_nanos((1 << 53) + 1).into(),
323+
),
276324
// Rust Durations can handle larger durations than Go's
277325
// representation, so skip these tests for their precision limits
278326

@@ -292,11 +340,10 @@ mod tests {
292340

293341
// // huge string; issue 15011.
294342
// {"0.100000000000000000000h", 6 * Minute},
295-
("0.100000000000000000000h", (6 * MINUTE).into())
296-
// // This value tests the first overflow check in leadingFraction.
297-
// {"0.830103483285477580700h", 49*Minute + 48*Second + 372539827*Nanosecond},
298-
// }
299-
// ```
343+
("0.100000000000000000000h", (6 * MINUTE).into()), // // This value tests the first overflow check in leadingFraction.
344+
// {"0.830103483285477580700h", 49*Minute + 48*Second + 372539827*Nanosecond},
345+
// }
346+
// ```
300347
];
301348

302349
for (input, expected) in cases {

policy-controller/k8s/api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![deny(warnings, rust_2018_idioms)]
22
#![forbid(unsafe_code)]
33

4+
pub mod duration;
45
pub mod labels;
56
pub mod policy;
6-
pub mod duration;
77

88
pub use self::labels::Labels;
99
pub use k8s_gateway_api as gateway;

policy-controller/k8s/index/src/outbound/index.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,15 +328,33 @@ impl Namespace {
328328
.map(http_route::try_match)
329329
.collect::<Result<_>>()?;
330330

331-
let backend_request_timeout = rule.timeouts.as_ref().and_then(|timeouts| Some(time::Duration::from(timeouts.backend_request?)));
331+
let backend_request_timeout = rule
332+
.timeouts
333+
.as_ref()
334+
.and_then(|timeouts| Some(time::Duration::from(timeouts.backend_request?)));
332335
let backends = rule
333336
.backend_refs
334337
.into_iter()
335338
.flatten()
336-
.filter_map(|b| convert_backend(&self.namespace, b, cluster, service_info, backend_request_timeout))
339+
.filter_map(|b| {
340+
convert_backend(
341+
&self.namespace,
342+
b,
343+
cluster,
344+
service_info,
345+
backend_request_timeout,
346+
)
347+
})
337348
.collect();
338-
let request_timeout = rule.timeouts.as_ref().and_then(|timeouts| Some(time::Duration::from(timeouts.request?)));
339-
Ok(HttpRouteRule { matches, backends, request_timeout })
349+
let request_timeout = rule
350+
.timeouts
351+
.as_ref()
352+
.and_then(|timeouts| Some(time::Duration::from(timeouts.request?)));
353+
Ok(HttpRouteRule {
354+
matches,
355+
backends,
356+
request_timeout,
357+
})
340358
}
341359
}
342360

0 commit comments

Comments
 (0)