Crate stream_cancel[][src]

This crate provides a mechanism for interrupting a Stream.

Any stream can be wrapped in a Valve, which enables it to be remotely terminated through an associated ValveHandle. This can be useful to implement graceful shutdown on "infinite" streams like a TcpListener. Once ValveHandle::close is called on the handle for a given stream's Valve, the stream will yield None to indicate that it has terminated.

extern crate tokio;

use stream_cancel::Valve;
use tokio::prelude::*;
use std::thread;

let listener = tokio::net::TcpListener::bind(&"0.0.0.0:0".parse().unwrap()).unwrap();
let (exit, incoming) = Valve::new(listener.incoming());

let server = thread::spawn(move || {
    // start a tokio echo server
    tokio::run(
        incoming
            .map_err(|e| eprintln!("accept failed = {:?}", e))
            .for_each(|sock| {
                let (reader, writer) = sock.split();
                tokio::spawn(
                    tokio::io::copy(reader, writer)
                        .map(|amt| println!("wrote {:?} bytes", amt))
                        .map_err(|err| eprintln!("IO error {:?}", err)),
                )
            }),
    )
});

// the server thread will normally never exit, since more connections
// can always arrive. however, with a Valve, we can turn off the
// stream of incoming connections to initiate a graceful shutdown
exit.close();
server.join().unwrap();

Structs

Valve

A Valve is wrapper around a Stream that enables the stream to be turned off remotely to initiate a graceful shutdown. When a new Valve is created with Valve::new, a handle to that Valve is also produced; when ValveHandle::close is called on that handle, the wrapped stream will immediately yield None to indicate that it has completed.

ValveHandle

A handle to a wrapped stream.