/may_http

Coroutine based HTTP library for Rust.

Primary LanguageRustApache License 2.0Apache-2.0

may_http

Travis Build Status

Coroutine based HTTP library for Rust.

Overview

may_http is a fast, coroutine based HTTP implementation that can be used as lower level layer for http servers and clients.

Some of the implementation logic comes from hyper 0.10.x branch

But most of the logic are re-written for ergonomical usage and performance consideration.

Thanks to the httparse and http crates, they make may_http only focus on the transportation logic.

Example

Hello World Server:

extern crate http;
extern crate may_http;

use http::header::*;
use may_http::server::*;

fn hello(_req: Request, rsp: &mut Response) {
    rsp.headers_mut()
        .append(CONTENT_TYPE, "text/plain; charset=utf-8".parse().unwrap());
    rsp.send(b"Hello World!").unwrap();
}

fn main() {
    let server = HttpServer::new(hello).start("127.0.0.1:8080").unwrap();
    server.wait();
}

Simple Client

for a more complicated client example, you can ref wrk-rs

extern crate http;
extern crate may_http;

use http::Uri;
use std::io::{self, Read};
use may_http::client::*;

fn client_get(uri: Uri) -> io::Result<()> {
    let mut client = {
        let host = uri.host().unwrap_or("127.0.0.1");
        let port = uri.port().unwrap_or(80);
        HttpClient::connect((host, port))?
    };

    let mut s = String::new();
    for _ in 0..100 {
        let uri = uri.clone();
        let mut rsp = client.get(uri)?;
        rsp.read_to_string(&mut s)?;
        println!("get rsp={}", s);
        s.clear();
    }
    Ok(())
}

fn main() {
    let uri: Uri = "http://127.0.0.1:8080/".parse().unwrap();
    client_get(uri).unwrap();
}

Performance

The data only benched on one thread hello server, compared with hyper master branch which is power by tokio and future.

hyper

$ wrk http://127.0.0.1:3000 -d 10 -t 1 -c 200     
Running 10s test @ http://127.0.0.1:3000
  1 threads and 200 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     2.95ms  272.35us   8.49ms   94.67%
    Req/Sec    68.13k     1.99k   70.36k    87.00%
  677836 requests in 10.05s, 83.39MB read
Requests/sec:  67466.95
Transfer/sec:      8.30MB
wrk http://127.0.0.1:3000 -d 10 -t 1 -c 200  2.79s user 5.97s system 87% cpu 10.051 total

may_http

$ wrk http://127.0.0.1:8080 -d 10 -t 1 -c 200
Running 10s test @ http://127.0.0.1:8080
  1 threads and 200 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     2.45ms  330.62us  10.91ms   96.82%
    Req/Sec    81.79k     2.83k   84.95k    81.00%
  814731 requests in 10.03s, 111.11MB read
Requests/sec:  81253.47
Transfer/sec:     11.08MB
wrk http://127.0.0.1:8080 -d 10 -t 1 -c 200  2.75s user 6.89s system 96% cpu 10.030 total

License

This project is licensed under either of

at your option.