Skip to content

Commit

Permalink
Rewrite Reaper as a struct type
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Jan 29, 2024
1 parent 9911e8e commit 4cf6cc1
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions bb8/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ where
if inner.statics.max_lifetime.is_some() || inner.statics.idle_timeout.is_some() {
let start = Instant::now() + inner.statics.reaper_rate;
let interval = interval_at(start.into(), inner.statics.reaper_rate);
schedule_reaping(interval, Arc::downgrade(&inner));
tokio::spawn(
Reaper {
interval,
pool: Arc::downgrade(&inner),
}
.run(),
);
}

Self { inner }
Expand Down Expand Up @@ -223,18 +229,20 @@ where
}
}

fn schedule_reaping<M>(mut interval: Interval, weak_shared: Weak<SharedPool<M>>)
where
M: ManageConnection,
{
spawn(async move {
struct Reaper<M: ManageConnection> {
interval: Interval,
pool: Weak<SharedPool<M>>,
}

impl<M: ManageConnection> Reaper<M> {
async fn run(mut self) {
loop {
let _ = interval.tick().await;
if let Some(inner) = weak_shared.upgrade() {
let _ = self.interval.tick().await;
if let Some(inner) = self.pool.upgrade() {
PoolInner { inner }.reap();
} else {
break;
}
}
});
}
}

0 comments on commit 4cf6cc1

Please sign in to comment.