diff --git a/Cargo.lock b/Cargo.lock index 6613812..1eda03d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -125,16 +125,13 @@ dependencies = [ ] [[package]] -name = "backoff" -version = "0.4.0" +name = "backon" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b62ddb9cb1ec0a098ad4bbf9344d0713fa193ae1a80af55febcff2627b6a00c1" +checksum = "fd0b50b1b78dbadd44ab18b3c794e496f3a139abb9fbc27d9c94c4eebbb96496" dependencies = [ - "futures-core", - "getrandom 0.2.15", - "instant", - "pin-project-lite", - "rand 0.8.5", + "fastrand", + "gloo-timers", "tokio", ] @@ -408,7 +405,7 @@ dependencies = [ name = "driver-kasa" version = "0.1.0" dependencies = [ - "backoff", + "backon", "deranged", "mac_address", "protocol", @@ -462,6 +459,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + [[package]] name = "fnv" version = "1.0.7" @@ -566,17 +569,6 @@ dependencies = [ "slab", ] -[[package]] -name = "getrandom" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" -dependencies = [ - "cfg-if", - "libc", - "wasi 0.11.0+wasi-snapshot-preview1", -] - [[package]] name = "getrandom" version = "0.3.2" @@ -608,6 +600,18 @@ dependencies = [ "url", ] +[[package]] +name = "gloo-timers" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbb143cf96099802033e0d4f4963b19fd2e0b728bcf076cd9cf7f6634f092994" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -965,15 +969,6 @@ version = "2.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4c7245a08504955605670dbf141fceab975f15ca21570696aebe9d2e71576bd" -[[package]] -name = "instant" -version = "0.1.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" -dependencies = [ - "cfg-if", -] - [[package]] name = "is_debug" version = "1.1.0" @@ -1001,7 +996,7 @@ version = "0.1.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38f262f097c174adebe41eb73d66ae9c06b2844fb0da69969647bbddd9b0538a" dependencies = [ - "getrandom 0.3.2", + "getrandom", "libc", ] @@ -1427,8 +1422,6 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ - "libc", - "rand_chacha 0.3.1", "rand_core 0.6.4", ] @@ -1438,20 +1431,10 @@ version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97" dependencies = [ - "rand_chacha 0.9.0", + "rand_chacha", "rand_core 0.9.3", ] -[[package]] -name = "rand_chacha" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" -dependencies = [ - "ppv-lite86", - "rand_core 0.6.4", -] - [[package]] name = "rand_chacha" version = "0.9.0" @@ -1467,9 +1450,6 @@ name = "rand_core" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" -dependencies = [ - "getrandom 0.2.15", -] [[package]] name = "rand_core" @@ -1477,7 +1457,7 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" dependencies = [ - "getrandom 0.3.2", + "getrandom", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 8e5d92d..463bacc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ members = [ resolver = "2" [workspace.dependencies] -backoff = "0.4.0" +backon = "1.5" chrono = "0.4.40" chrono-tz = "0.10.1" deranged = "0.4" diff --git a/driver/kasa/Cargo.toml b/driver/kasa/Cargo.toml index 9c6ad21..a1fb6b5 100644 --- a/driver/kasa/Cargo.toml +++ b/driver/kasa/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -backoff = { workspace = true, features = ["tokio"] } +backon = { workspace = true } deranged = { workspace = true } mac_address = { version = "1.1.8", features = ["serde"] } protocol = { path = "../../protocol" } diff --git a/driver/kasa/src/connection.rs b/driver/kasa/src/connection.rs index 560b6f0..09b5497 100644 --- a/driver/kasa/src/connection.rs +++ b/driver/kasa/src/connection.rs @@ -1,17 +1,15 @@ -use std::{convert::Infallible, io, net::SocketAddr, num::NonZero, time::Duration}; - -use backoff::{ExponentialBackoff, ExponentialBackoffBuilder}; +use crate::messages::{GetSysInfo, GetSysInfoResponse, LB130USSys, SysInfo}; +use backon::{FibonacciBuilder, Retryable}; use protocol::light::{Kelvin, KelvinLight, Light, Rgb, RgbLight}; use snafu::{ResultExt, Snafu}; +use std::{convert::Infallible, io, net::SocketAddr, num::NonZero, time::Duration}; use tokio::{ io::{AsyncReadExt, AsyncWriteExt, BufReader, BufWriter}, - net::{TcpListener, TcpSocket, TcpStream}, - sync::{mpsc, oneshot, OnceCell}, + net::TcpStream, + sync::{mpsc, oneshot}, time::timeout, }; -use crate::messages::{GetSysInfo, GetSysInfoResponse, LB130USSys, SysInfo}; - struct XorEncryption; impl XorEncryption { @@ -90,21 +88,19 @@ async fn lb130us_actor( "connecting for a first time / reconnecting after having gone idle..." ); - match backoff::future::retry_notify( - ExponentialBackoff::default(), - || async { - let stream = TcpStream::connect(addr).await?; - let (reader, writer) = stream.into_split(); + match (|| async { + let stream = TcpStream::connect(addr).await?; + let (reader, writer) = stream.into_split(); - let buf_reader = BufReader::new(reader); - let buf_writer = BufWriter::new(writer); + let buf_reader = BufReader::new(reader); + let buf_writer = BufWriter::new(writer); - Ok((buf_reader, buf_writer)) - }, - |err, duration| { - tracing::error!(?err, ?duration); - }, - ) + Ok((buf_reader, buf_writer)) + }) + .retry(FibonacciBuilder::default()) + .notify(|err: &io::Error, duration| { + tracing::error!(?err, ?duration); + }) .await { Ok(connection) => (connection_cell.insert(connection), message),