Skip to content

Commit 8950a35

Browse files
committed
dataplane: replace interval with sleep
1 parent 6a13061 commit 8950a35

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

monad-dataplane/src/ban_expiry.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
// You should have received a copy of the GNU General Public License
1414
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515

16-
use std::{collections::VecDeque, net::IpAddr, sync::Arc, time::Duration};
16+
use std::{collections::VecDeque, net::IpAddr, pin::Pin, sync::Arc, time::Duration};
1717

1818
use monoio::{
1919
select,
20-
time::{interval, Instant, Interval},
20+
time::{sleep, Instant, Sleep},
2121
};
2222
use tokio::sync::mpsc;
23-
use tracing::{debug, info};
23+
use tracing::{debug, info, trace};
2424

2525
use crate::addrlist::Addrlist;
2626

@@ -30,13 +30,12 @@ pub(crate) async fn task(
3030
ban_duration: Duration,
3131
) {
3232
let mut queue: VecDeque<(IpAddr, Instant)> = VecDeque::new();
33-
let mut ticker: Option<Interval> = None;
34-
33+
let mut ticker: Option<Pin<Box<Sleep>>> = None;
3534
loop {
3635
select! {
3736
_ = async {
3837
match &mut ticker {
39-
Some(t) => t.tick().await,
38+
Some(t) => t.await,
4039
None => std::future::pending().await,
4140
}
4241
} => {
@@ -53,18 +52,20 @@ pub(crate) async fn task(
5352
});
5453
queue.pop_front();
5554
} else {
56-
ticker = Some(interval(ban_duration - timestamp.elapsed()));
55+
trace!("renew ticker");
56+
ticker = Some(Box::pin(sleep(ban_duration - timestamp.elapsed())));
5757
break;
5858
}
5959
}
6060
queue.is_empty().then(|| ticker = None);
6161
},
6262
banned = banned_connections.recv() => {
6363
match banned {
64-
Some(banned) => {
65-
queue.push_back(banned);
64+
Some((addr, timestamp)) => {
65+
debug!(%addr, ?timestamp, "ban");
66+
queue.push_back((addr, timestamp));
6667
if ticker.is_none() {
67-
ticker = Some(interval(ban_duration));
68+
ticker = Some(Box::pin(sleep(ban_duration)));
6869
}
6970
},
7071
None => {

0 commit comments

Comments
 (0)