Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions asyncband/src/pool/bounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ impl<M: ManageObject> Pool<M> {
Some(object) => {
let mut unready_object = UnreadyObject {
state: Some(object),
pool: Arc::downgrade(self),
recycle_cancelled_strategy: self.config.recycle_cancelled_strategy,
pool: self,
};

let state = unready_object.state();
Expand Down Expand Up @@ -556,34 +555,32 @@ impl<M: ManageObject> Object<M> {
/// If the check fails, `detach()` should be called to permanently remove the object
/// from the pool. If dropped without calling either method (due to being cancelled),
/// the behavior depends on the pool's [`RecycleCancelledStrategy`] configuration.
struct UnreadyObject<M: ManageObject> {
struct UnreadyObject<'a, M: ManageObject> {
state: Option<ObjectState<M::Object>>,
pool: Weak<Pool<M>>,
recycle_cancelled_strategy: RecycleCancelledStrategy,
// The enclosing `get` call keeps the pool alive through cancellation cleanup.
pool: &'a Arc<Pool<M>>,
}

impl<M: ManageObject> Drop for UnreadyObject<M> {
impl<M: ManageObject> Drop for UnreadyObject<'_, M> {
fn drop(&mut self) {
if let Some(mut state) = self.state.take() {
if let Some(pool) = self.pool.upgrade() {
match self.recycle_cancelled_strategy {
RecycleCancelledStrategy::Detach => {
pool.detach_object(&mut state.o);
}
RecycleCancelledStrategy::ReturnToPool => {
pool.restore_idle(state);
}
match self.pool.config.recycle_cancelled_strategy {
RecycleCancelledStrategy::Detach => {
self.pool.detach_object(&mut state.o);
}
RecycleCancelledStrategy::ReturnToPool => {
self.pool.restore_idle(state);
}
}
}
}
}

impl<M: ManageObject> UnreadyObject<M> {
impl<M: ManageObject> UnreadyObject<'_, M> {
fn ready(mut self, permit: OwnedSemaphorePermit) -> Object<M> {
// INVARIANT: `state` is `Some` until this object becomes ready, detaches, or is dropped.
let state = Some(self.state.take().unwrap());
let pool = self.pool.clone();
let pool = Arc::downgrade(self.pool);
Object {
state,
permit,
Expand All @@ -593,9 +590,7 @@ impl<M: ManageObject> UnreadyObject<M> {

fn detach(&mut self) {
if let Some(mut state) = self.state.take() {
if let Some(pool) = self.pool.upgrade() {
pool.detach_object(&mut state.o);
}
self.pool.detach_object(&mut state.o);
}
}

Expand Down
39 changes: 17 additions & 22 deletions asyncband/src/pool/unbounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,7 @@ impl<T, M: ManageObject<Object = T>> Pool<T, M> {
Some(object) => {
let mut unready_object = UnreadyObject {
state: Some(object),
pool: Arc::downgrade(self),
recycle_cancelled_strategy: self.config.recycle_cancelled_strategy,
pool: self,
};

let state = unready_object.state();
Expand Down Expand Up @@ -571,46 +570,42 @@ impl<T, M: ManageObject<Object = T>> Object<T, M> {
/// If the check fails, `detach()` should be called to permanently remove the object
/// from the pool. If dropped without calling either method (due to being cancelled),
/// the behavior depends on the pool's [`RecycleCancelledStrategy`] configuration.
struct UnreadyObject<T, M: ManageObject<Object = T>> {
state: Option<ObjectState<T>>,
pool: Weak<Pool<T, M>>,
recycle_cancelled_strategy: RecycleCancelledStrategy,
struct UnreadyObject<'a, M: ManageObject> {
state: Option<ObjectState<M::Object>>,
// The enclosing `get` call keeps the pool alive through cancellation cleanup.
pool: &'a Arc<Pool<M::Object, M>>,
}

impl<T, M: ManageObject<Object = T>> Drop for UnreadyObject<T, M> {
impl<M: ManageObject> Drop for UnreadyObject<'_, M> {
fn drop(&mut self) {
if let Some(mut state) = self.state.take() {
if let Some(pool) = self.pool.upgrade() {
match self.recycle_cancelled_strategy {
RecycleCancelledStrategy::Detach => {
pool.detach_object(&mut state.o);
}
RecycleCancelledStrategy::ReturnToPool => {
pool.restore_idle(state);
}
match self.pool.config.recycle_cancelled_strategy {
RecycleCancelledStrategy::Detach => {
self.pool.detach_object(&mut state.o);
}
RecycleCancelledStrategy::ReturnToPool => {
self.pool.restore_idle(state);
}
}
}
}
}

impl<T, M: ManageObject<Object = T>> UnreadyObject<T, M> {
fn ready(mut self) -> Object<T, M> {
impl<M: ManageObject> UnreadyObject<'_, M> {
fn ready(mut self) -> Object<M::Object, M> {
// INVARIANT: `state` is `Some` until this object becomes ready, detaches, or is dropped.
let state = Some(self.state.take().unwrap());
let pool = self.pool.clone();
let pool = Arc::downgrade(self.pool);
Object { state, pool }
}

fn detach(&mut self) {
if let Some(mut state) = self.state.take() {
if let Some(pool) = self.pool.upgrade() {
pool.detach_object(&mut state.o);
}
self.pool.detach_object(&mut state.o);
}
}

fn state(&mut self) -> &mut ObjectState<T> {
fn state(&mut self) -> &mut ObjectState<M::Object> {
// INVARIANT: `state` is `Some` until this object becomes ready, detaches, or is dropped.
self.state.as_mut().unwrap()
}
Expand Down
22 changes: 22 additions & 0 deletions tests-integration/tests/pool_test/recycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ macro_rules! recycle_contract {
}
}

#[test]
fn recycled_object_can_outlive_the_pool() {
for detach in [false, true] {
let manager = Manager::default();
let pool = pool(1, manager.clone(), None);
drop(ready(pool.get()).unwrap());
let object = ready(pool.get()).unwrap();
assert_eq!(object.status().recycle_count(), 1);

let weak_pool = Arc::downgrade(&pool);
drop(pool);
assert!(weak_pool.upgrade().is_none());
assert_eq!(*object, 0);
if detach {
assert_eq!(object.detach(), 0);
} else {
drop(object);
}
assert!(manager.detached().is_empty());
}
}

#[test]
fn cancelling_one_validation_leaves_other_idle_objects_available() {
let manager = Manager::default();
Expand Down
Loading