Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
net wg presentation slide data

Network services WG


Network services WG

  1. Async IO
  2. Library support for network services

Library support

Help us decide what to prioritize the rest of this afternoon!


async/await


Concat two pages

  1. HTTP GET to URL 1
  2. HTTP GET to URL 2
  3. Combine the bodies of the two responses

(Ignore that this is embarrassingly parallel.)


Concat two pages: blocking IO


Client API

impl Client {
    fn get(&mut self, url: &Url)
        -> io::Result<String> { ... }
}

Concat two pages: blocking IO

fn concat_pages(client: &mut Client, url1: &Url, url2: &Url)
    -> io::Result<String>
{
    let page1 = client.get(url1)?;
    let page2 = client.get(url2)?;
    Ok(page1 + &page2)
}

Concat two pages: futures


Client API

impl Client {
    fn get(&mut self, url: &Url)
        -> impl Future<Item = String, Error = io::Error>
    { ... }
}

Concat two pages: futures

fn concat_pages(client: Arc<Mutex<Client>>,
                url1: &Url, url2: &Url)
    -> io::Result<String>
{
    let client2 = client.clone();
    let url2 = url2.to_owned();

    client.lock().get(url1)
          .and_then(move |page1| {
              client2.lock().get(&url2).join(Ok(page1))
          })
          .map(|(page2, page1)| {
              page2 + &page1
          })
}

Concat two pages: async/await


Client API

impl Client {
    fn get(&mut self, url: &Url)
        -> impl Future<Item = String, Error = io::Error>
    { ... }
}

(The same API!)


Concat two pages: async/await

async fn concat_pages(client: &mut Client,
                      url1: &Url, url2: &Url)
    -> io::Result<String>
{
    let page1 = await(client.get(url1))?;
    let page2 = await(client.get(url2))?;
    Ok(page1 + &page2)
}

async fn

  • async fn foo() -> T is a function that returns impl Future<Item = T>
  • Inside the body, you return T; your function body compiles to a stackless coroutine

await

  • Transforms a Future<Item = T> into T
  • Can only await inside of an async fn
  • Yields control of the coroutine until the future resolves

Library support

  • Move Future, IntoFuture, and task into libcore
  • Leave everything else in the ecosystem for now
trait Future {
    type Item;
    fn poll(self: Pin<Self>,
            ctx: &mut task::Context) -> Async<Self::Item>;
}

From here to there

  • RFCs: April - May
  • Implementation & experimentation: May - September
  • Discuss tomorrow morning
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment