gsquire / singleflight

Rust port of Go's singleflight package
https://crates.io/crates/singleflight
MIT License
13 stars 1 forks source link

async #2

Open anacrolix opened 4 years ago

anacrolix commented 4 years ago

How do I use this with async work? I get something like the error

expected an `Fn<()>` closure, found `impl core::future::future::Future`
gsquire commented 4 years ago

Hey, sorry for the delayed reply. Can you share some more of your code? I'm not sure this has to do directly with this crate but I can try and help debug it a bit.

anacrolix commented 4 years ago

I want to call Group::work with an async function. Something like g.work(async { ... }). I'm not sure how to bundle up an async function so that it can go into something expecting an Fn<()> closure.

gsquire commented 4 years ago

I could be wrong but I don't think you can construct asynchronous closures. It doesn't have anything to do with that trait.

On Sun, Feb 2, 2020 at 21:00 Matt Joiner notifications@github.com wrote:

I want to call Group::work with an async function. Something like g.work(async { ... }). I'm not sure how to bundle up an async function so that it can go into something expecting an Fn<()> closure.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/gsquire/singleflight/issues/2?email_source=notifications&email_token=AAUKW5VPK4AFQHKGPK4J5YTRA6QHDA5CNFSM4KORQOHKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKSP3QQ#issuecomment-581238210, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAUKW5U3STAFNXEY5B74LRTRA6QHDANCNFSM4KORQOHA .

-- Sent from Gmail Mobile

anacrolix commented 4 years ago

Thanks, it does seem like async function traits are not yet available in the language. In the meanwhile, would it be appropriate to have a work function that accepts a future?

gsquire commented 4 years ago

I am going to have to think about it. I am worried that it would defeat the purpose of non-blocking functions if I mix in wait groups and locks. Because in my mind, if we acquire a lock for the group when work is called we essentially block that thread from doing any execution if another thread holds that lock. So an async function can't make progress. Does that make sense or am I not seeing this right?

anacrolix commented 4 years ago

I think it's okay to lock the Group while checking for existing work, and marking that we're doing it, and then unlocking while the work is done. I have created my own Singleflight in the meanwhile, that may serve as inspiration.

gsquire commented 4 years ago

Nice, thanks for sharing. I don't know if I will be able to make a patch to add that any time soon but I will be happy to review a PR if you submit one.