mirror of
https://github.com/librespot-org/librespot.git
synced 2025-02-06 17:47:10 +00:00
Don't use Instant elapsed
It would be so much easier to use elapsed but elapsed could potentially panic is rare cases. See: https://doc.rust-lang.org/std/time/struct.Instant.html#monotonicity
This commit is contained in:
parent
d3ae1cdc55
commit
8e6d452765
3 changed files with 38 additions and 7 deletions
|
@ -252,8 +252,20 @@ impl Session {
|
||||||
if session.is_invalid() {
|
if session.is_invalid() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
let last_ping = session.0.data.read().last_ping.unwrap_or_else(Instant::now);
|
|
||||||
if last_ping.elapsed() >= SESSION_TIMEOUT {
|
// It would be so much easier to use elapsed but elapsed could
|
||||||
|
// potentially panic is rare cases.
|
||||||
|
// See:
|
||||||
|
// https://doc.rust-lang.org/std/time/struct.Instant.html#monotonicity
|
||||||
|
let now = Instant::now();
|
||||||
|
|
||||||
|
let last_ping = session.0.data.read().last_ping.unwrap_or(now);
|
||||||
|
|
||||||
|
let since_last_ping = now
|
||||||
|
.checked_duration_since(last_ping)
|
||||||
|
.unwrap_or(SESSION_TIMEOUT);
|
||||||
|
|
||||||
|
if since_last_ping >= SESSION_TIMEOUT {
|
||||||
session.shutdown();
|
session.shutdown();
|
||||||
// TODO: Optionally reconnect (with cached/last credentials?)
|
// TODO: Optionally reconnect (with cached/last credentials?)
|
||||||
return Err(io::Error::new(
|
return Err(io::Error::new(
|
||||||
|
|
|
@ -114,8 +114,9 @@ impl SpClient {
|
||||||
dst: &mut [u8],
|
dst: &mut [u8],
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
// after a certain number of seconds, the challenge expires
|
// after a certain number of seconds, the challenge expires
|
||||||
const TIMEOUT: u64 = 5; // seconds
|
const TIMEOUT: Duration = Duration::from_secs(5);
|
||||||
let now = Instant::now();
|
|
||||||
|
let then = Instant::now();
|
||||||
|
|
||||||
let md = Sha1::digest(ctx);
|
let md = Sha1::digest(ctx);
|
||||||
|
|
||||||
|
@ -123,9 +124,18 @@ impl SpClient {
|
||||||
let target: i64 = BigEndian::read_i64(&md[12..20]);
|
let target: i64 = BigEndian::read_i64(&md[12..20]);
|
||||||
|
|
||||||
let suffix = loop {
|
let suffix = loop {
|
||||||
if now.elapsed().as_secs() >= TIMEOUT {
|
// It would be so much easier to use elapsed but elapsed could
|
||||||
|
// potentially panic is rare cases.
|
||||||
|
// See:
|
||||||
|
// https://doc.rust-lang.org/std/time/struct.Instant.html#monotonicity
|
||||||
|
if Instant::now()
|
||||||
|
.checked_duration_since(then)
|
||||||
|
.unwrap_or(TIMEOUT)
|
||||||
|
>= TIMEOUT
|
||||||
|
{
|
||||||
return Err(Error::deadline_exceeded(format!(
|
return Err(Error::deadline_exceeded(format!(
|
||||||
"{TIMEOUT} seconds expired"
|
"{} seconds expired",
|
||||||
|
TIMEOUT.as_secs(),
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
src/main.rs
11
src/main.rs
|
@ -1836,7 +1836,16 @@ async fn main() {
|
||||||
warn!("Spirc shut down unexpectedly");
|
warn!("Spirc shut down unexpectedly");
|
||||||
|
|
||||||
let mut reconnect_exceeds_rate_limit = || {
|
let mut reconnect_exceeds_rate_limit = || {
|
||||||
auto_connect_times.retain(|&t| t.elapsed() < RECONNECT_RATE_LIMIT_WINDOW);
|
// It would be so much easier to use elapsed but elapsed could
|
||||||
|
// potentially panic is rare cases.
|
||||||
|
// See:
|
||||||
|
// https://doc.rust-lang.org/std/time/struct.Instant.html#monotonicity
|
||||||
|
let now = Instant::now();
|
||||||
|
|
||||||
|
auto_connect_times.retain(|&t| {
|
||||||
|
now.checked_duration_since(t).unwrap_or(RECONNECT_RATE_LIMIT_WINDOW) < RECONNECT_RATE_LIMIT_WINDOW
|
||||||
|
});
|
||||||
|
|
||||||
auto_connect_times.len() > RECONNECT_RATE_LIMIT
|
auto_connect_times.len() > RECONNECT_RATE_LIMIT
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue