Skip to content
プロジェクト

概要

Rust製スタックフルコルーチンライブラリ

開発ツール

この数値の測り方

できること

  • The stackful coroutine implementation is based on generator;
  • Support schedule on a configurable number of threads for multi-core systems;
  • Support coroutine version of a local storage (CLS);
  • Support efficient asynchronous network I/O;

計測したスター増加

24時間
0
7日間
0
15日間
+1
1か月
+7

期間ごとの純増数です。期間は重複しており、日別の値ではありません。未計測の値は空欄です。スター数は注目度の指標であり、品質の保証ではありません。

May

May is a high-performance library for programming stackful coroutines with which you can easily develop and maintain massive concurrent programs. It can be thought as the Rust version of the popular Goroutine.


Table of contents


Features

  • The stackful coroutine implementation is based on generator;
  • Support schedule on a configurable number of threads for multi-core systems;
  • Support coroutine version of a local storage (CLS);
  • Support efficient asynchronous network I/O;
  • Support a completion-based io_uring I/O backend on Linux (opt-in), including regular-file access through CoIo;
  • Support efficient timer management;
  • Support standard synchronization primitives, a semaphore, an MPMC channel, etc;
  • Support cancellation of coroutines;
  • Support graceful panic handling that will not affect other coroutines;
  • Support scoped coroutine creation;
  • Support general selection for all the coroutine API;
  • All the coroutine API are compatible with the standard library semantics;
  • All the coroutine API can be safely called in multi-threaded context;
  • Both stable, beta, and nightly channels are supported;
  • x86_64 GNU/Linux, x86_64 Windows, x86_64 macOS, AArch64 GNU/Linux, and AArch64 macOS are supported.

Usage

A naive echo server implemented with May:

#[macro_use]
extern crate may;

use may::net::TcpListener;
use std::io::{Read, Write};

fn main() {
    let listener = TcpListener::bind("127.0.0.1:8000").unwrap();
    while let Ok((mut stream, _)) = listener.accept() {
        go!(move || {
            let mut buf = vec![0; 1024 * 16]; // alloc in heap!
            while let Ok(n) = stream.read(&mut buf) {
                if n == 0 {
                    break;
                }
                stream.write_all(&buf[0..n]).unwrap();
            }
        });
    }
}

Linux I/O backends

Linux builds can use either the readiness-based epoll backend or the completion-based io_uring backend:

  • epoll (default): a plain cargo build uses the epoll backend. The equivalent explicit feature set is --no-default-features --features "io_cancel io_timeout work_steal".
  • io_uring (opt-in): cargo build --features io_uring.

The io_uring backend requires Linux 5.11+ (IORING_FEAT_EXT_ARG); the wakeup poll uses the 5.13+ multishot form when available and falls back to one-shot re-arming on 5.11–5.12. It also requires Rust 1.84+ while the feature is enabled (rcu_cell). If ring creation is not permitted (old kernel, restricted container), scheduler initialization fails with a clear error; use the epoll feature set above instead.

The submission-queue size can be set before the scheduler starts:

may::config().set_io_uring_ring_entries(2048);

With the io_uring backend, CoIo can also wrap regular files. Regular files ignore O_NONBLOCK, so their reads and writes are always submitted as IORING_OP_READ/IORING_OP_WRITE; a coroutine waiting on storage yields its worker thread instead of blocking it. See parallel file reads and parallel file copy.


More examples

The CPU heavy load examples

The I/O heavy bound examples


Performance

You can refer to https://www.techempower.com/benchmarks/#section=data-r23 to get may_minihttp comparisons with other most popular frameworks.


Caveat

There is a detailed document that describes May's main restrictions. In general, there are four things you should follow when writing programs that use coroutines:

  • Don't call thread-blocking API (It will hurt the performance);
  • Carefully use Thread Local Storage (access TLS in coroutine might trigger undefined behavior).

It's considered unsafe with the following pattern:

set_tls();
// Or another coroutine API that would cause scheduling:
coroutine::yield_now();
use_tls();

but it's safe if your code is not sensitive about the previous state of TLS. Or there is no coroutines scheduling between set TLS and use TLS.

  • Don't run CPU bound tasks for long time, but it's ok if you don't care about fairness;
  • Don't exceed the coroutine stack. There is a guard page for each coroutine stack. When stack overflow occurs, it will trigger segment fault error.

Note:

The first three rules are common when using cooperative asynchronous libraries in Rust. Even using a futures-based system also have these limitations. So what you should really focus on is a coroutine stack size, make sure it's big enough for your applications.


How to tune a stack size

If you want to tune your coroutine stack size, please check out this document.


License

May is licensed under either of the following, at your option:

関連プロジェクトを探す

読み込み中…