tokio-rs / doc-push

Tokio doc blitz effort - A concerted effort to improve Tokio's documentation.
MIT License
50 stars 7 forks source link

Highlight oneshot channels as a convenient primitive #62

Open jonhoo opened 5 years ago

jonhoo commented 5 years ago

When I build my own futures, especially when they involve asynchronous I/O, I often end up using oneshot channels to notify about request completions. In particular,I very frequently use the pattern where you spawn a long-running "connection" future that reads from a

futures::sync::mpsc::Receiver<(Request, future::sync::oneshot::Sender<Response>)>

and sends responses on the oneshot channels as it handles incoming requests one at a time.

Pointing users in this direction might be good. A good example to refer them to might be tokio-zookeeper, because there's also a live-coding session where I build up that pattern here.

carllerche commented 5 years ago

I would guess that it should be part of this page?

carllerche commented 5 years ago

Relates to #67

flxo commented 5 years ago

A note or example about oneshot::spawn for any kind of "cancellation" might be also good to add.

jonhoo commented 5 years ago

Oooh, that's cool! I didn't even know about that.

flxo commented 5 years ago

I just stumbled across this PR that adds tokio::spawn_handle and thought it's worth to mention it here. It basically provides the same functionality as oneshot::spawn.