Skip to content

Add a version of Pool::close that only closes if the Pool is unique #4262

@tgross35

Description

@tgross35

I have found these related issues/pull requests

None found

Description

There isn't an easy way to do a graceful shutdown in a pool may be cloned. E.g. the below may not work as intended:

let p: Pool = make_pool();
let p1 = p.clone();

// May fail if `p` is closed before `p1` is used
thread::spawn(|| do_something(p1));

p.close().await;

If you want both of these features then currently the only option is to separately track how many Pool handles exist. This is redundant though, since Pool already has this information internally.

Prefered solution

Add a way to close the pool only if it is the last one left:

impl Pool {
    /// Close this pool if there are no other clones. Returns `true` if the pool was closed.
    pub async fn close_if_unique(self) -> bool {
        match Arc::into_inner(self.0) {
            Some(inner) => {
                inner.close().await;
                true
             }
             None => false
        }
    }
}

Alternatively or additionally, give access to the count:

impl Pool {
    pub fn handle_count(&self) -> usize {
        Arc::strong_count(&self.0)
    }
}

Is this a breaking change? Why or why not?

Not a breaking change, API addition.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions