-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cookbook #23
Comments
How do I hook up timers to the networking stack? For example, how do I send a UDP packet every 5 seconds? Getting a a timer set-up is really easy: let interval = Interval::new_interval(Duration::from_secs(5));
let task = interval.map_err(|e| ()).for_each(|_| {
println!("Timer has fired");
future::done(Ok(()))
});
tokio::run(task); Sending a single message on a socket is pretty easy, too let local = "0.0.0.0:12345".parse().unwrap();
let target = "10.11.12.13:12345".parse().unwrap();
let socket = UdpSocket::bind(&local).unwrap();
let task = socket.send_dgram("hello".as_bytes(), &target).then(|r| {
if let Ok(s) = r {
println!("Successfully send bytes to socket");
// could send another message using `s` now if we wanted
} else {
println!("Failed to send bytes");
}
future::done(Ok(()))
});
tokio::run(task); But how do I combine them? The hard part seems to be that |
What's the pattern for taking "events" from multiple sources and handling them all in the same handler? For example, waiting for network data, waiting for keyboard input, and a timer? My guess is that you would take your streams and map them all to the same wrapper type (probably an enum), and then use |
Ok, I seeded the cookbook outline here. Please provide additional items via PR 👍 |
Could you please add another small seed that shows the structure for providing actual cookbook entries? (I mean the actual example code, not just the description of the cookbook entry). Should we just create new .md files in a new |
I would probably follow the structure of https://rust-lang-nursery.github.io/rust-cookbook/. A new folder for If you are trying to get started and creating the structure is not something you want to do in the PR, just post a gist here and I can use that to setup the structure. |
If we could organize a group effort in the Tokio channels, I bet we could get spans of code from dozens of existing projects that demonstrate these tasks. |
@rrichardson sure, however you want to do it 👍 |
A topic that I'm interested in is client connection pooling. I've been thinking about toying around with the Cassandra database protocol that communicates over TCP and supports asynchronous connections. There can be multiple connections to multiple DB servers and each connection can be used to send multiple requests (each request is assigned a stream ID that will also be added to the response, so that the driver knows what query to complete). Maybe a high level concept of that description could be implemented? That would be a pretty involved example and maybe it's not suitable for this cookbook, but I just thought I'd spit it out here. |
A connection pool is definitely a good example... I wonder if it should be included as a dedicated page in the Transports section. Also, maybe the cookbook should link to examples throughout the guide as well in cases where the example is more complex. |
The connection pool example would be a great addition to the transports section as it shows how to implement a more complex client protocol implementation. Then the transports section has both client and server protocol implementation examples covered. IMO it's also refreshing to see stuff besides HTTP. |
Below is a wish-list that I've distilled from https://github.com/tokio-rs/doc-push/blob/master/outline/cookbook.md for quick reference. I'll update it with source/gist links as we find them.
|
@rrichardson very nice list |
A fairly simple one, but something I ran into: spawning tasks on the current thread only (without using the default runtime), lending handle references to spawn tasks on the same thread. |
A full example of |
I would like cookbook recipe for limiting the number of in-flight requests. E.g. when I'm feeding a channel from a separate thread that is read from tokio as a Stream; and only want |
Another example: Download files using an HTTP client in parallel and write them to files. |
I just wrote this exact thing for work. I could probably sanitize it and throw it in. On a side note: What is the scope of this cookbook now? Async/Await and Tokio v0.2+ ? |
Yes, async/await and 0.2 :) I am hoping to get this done around the time the final lands... 3 months maybe, 6 months definitely. |
What kinds of things should be in the cookbook?
Prior discussion
The text was updated successfully, but these errors were encountered: