Skip to content

Commit

Permalink
Initial skeleton of the cookbook
Browse files Browse the repository at this point in the history
  • Loading branch information
eminence committed Oct 9, 2018
1 parent 4efcb1f commit f251542
Show file tree
Hide file tree
Showing 16 changed files with 148 additions and 137 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cookbook/book/
11 changes: 11 additions & 0 deletions cookbook/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
This folder contains the source for the tokio cookbook, in mdbook format.

*Note*: This file just documents this folder and isn't part of the book itself.

To build the book, first install mdbook:

cargo install mdbook

And then build it:

mdbook build
5 changes: 5 additions & 0 deletions cookbook/book.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[book]
title = "Tokio Cookbook"
description = "Collection of useful and common patterns for tokio"
authors = ["Tokio Community"]
src = "src"
11 changes: 11 additions & 0 deletions cookbook/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Summary

[About](about.md)
- [Application structure](application-structure.md)
- [Concurrency](concurrency.md)
- [File system](file-system.md)
- [Futures, Streams, Sinks](futures-streams-sinks.md)
- [Networkings](networking.md)
- [Operating System](operating-system.md)
- [Time](time.md)
- [Transports](transports.md)
6 changes: 6 additions & 0 deletions cookbook/src/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# About

A collection of simple examples that demonstrate good practices to accomplish
common tasks with Tokio.

[Discussion](https://github.com/tokio-rs/doc-push/issues/23)
37 changes: 37 additions & 0 deletions cookbook/src/application-structure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Application structure

## Background tasks

This shows how to spawn a task to perform work in the background. For example,
updating configuration on an interval.

How is the task shutdown when the application is shutdown.

## Shutting down an application

How to signal to an application to shutdown.

* Listen for `ctrl-c`
* Gracefully shutdown open sockets
* Background tasks shutdown
* Setting a timeout

## Managing state between tasks with message passing

An actor like pattern is used a lot with Tokio applications.

When there is a resource or state that needs to be accessed from many tasks, one
way to handle this is to spawn a task dedicated to managing that resource. Then,
all other tasks interact with the state via message passing.

This section probably can use multiple cookbook examples.

* Mutating a hashmap
* Managing a transport (see [here](https://github.com/tokio-rs/tokio/issues/86#issuecomment-358108788))

## Performing CPU intensive operations

This cannot be done from the event loop and must be run on a thread pool
somehow.


4 changes: 4 additions & 0 deletions cookbook/src/concurrency.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Concurrency

TODO

6 changes: 6 additions & 0 deletions cookbook/src/file-system.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# File system

## Send the contents of a file via a `TcpStream`.

How should this be done?

17 changes: 17 additions & 0 deletions cookbook/src/futures-streams-sinks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Futures, Streams, Sinks

## Returning different future types from different branches.

* Box
* `Either`

## Process events from multiple sources

See
[here](https://github.com/tokio-rs/doc-push/issues/23#issuecomment-426481892)
and [here](https://github.com/tokio-rs/tokio/issues/86#issuecomment-358186680).

> 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?
7 changes: 7 additions & 0 deletions cookbook/src/networking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Networking

## Connect a `TcpStream` by hostname (DNS).

How should this be done?

and automatically send a message via the `Sink` half.
19 changes: 19 additions & 0 deletions cookbook/src/operating-system.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Operating System

Most of the items from [Rust's
cookbook](https://rust-lang-nursery.github.io/rust-cookbook/intro.html#operating-system)
applies here.

## Reading from STDIN

TODO

## Writing to STDOUT

While `println` could work, this can block. What is the safe way to do this from
Tokio?

## Child processes

How to spawn & manage child processes from Tokio?

11 changes: 11 additions & 0 deletions cookbook/src/time.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Time

## Send a UDP packet every 5 seconds

Combo interval and networking. See [here](https://github.com/tokio-rs/doc-push/issues/23#issuecomment-426477390)

## Limit a Stream by total duration

Terminate a stream when 30 seconds has elapsed. Then show how the number of
messages can be capped as well by using `take`

6 changes: 6 additions & 0 deletions cookbook/src/transports.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Transports

## Transparently send message when error is received on `Stream`

This is a form of automatic error handling at the transport level. Implement a
transport decorator that intercepts errors on the `Stream` half of a certain kind
2 changes: 1 addition & 1 deletion outline/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ A set of guides showing how to use Tokio in different contexts:
A collection of simple examples that demonstrate good practices to accomplish
common tasks with Tokio.

[Details](cookbook.md)
[Details](../cookbook/src/about.md)

<a name="internals"></a>
# Internals
Expand Down
130 changes: 0 additions & 130 deletions outline/cookbook.md

This file was deleted.

12 changes: 6 additions & 6 deletions outline/overview.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Covers the very basics of what Tokio is, what it's used for and important facts on its usage and performance.

# Guide:

[Overview](https://tokio.rs/docs/overview/)

Covers the very basics of what Tokio is, what it's used for and important facts on its usage and performance.

# Guide:

[Overview](https://tokio.rs/docs/overview/)

# Issues

0 comments on commit f251542

Please sign in to comment.