Cogo is a high-performance library for programming stackful coroutines with which you can easily develop and maintain massive concurrent programs.

Overview

cogo

Way too Easy

Cogo 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][go].

Initial code frok from May and we add Many improvements(Inspired by Golang, parking_lot and crossbeam) and more...

Performance

per

cogo crates

Cogo Powerful standard library

  • cogo/std/http/server An HTTP server is available(Body parsing upcoming)
  • cogo/std/http/client An HTTP Client(TODO) upcoming
  • cogo/std/queue Basic queue data structures
  • cogo/std/sync Includes Mutex/RwLock/WaitGroup/Semphore/chan!()/chan!(1000)...and more..
  • cogo/std/defer Defers evaluation of a block of code until the end of the scope.
  • cogo/std/map Provides the same concurrency map as Golang, with SyncHashMap and SyncBtreeMap.It is suitable for concurrent environments with too many reads and too few writes
  • cogo/std/time Improve the implementation of a high performance time
  • cogo/std/lazy Thread/coroutine safe global variable,Lazy struct,OnceCell

Crates based on cogo implementation

  • cdbc Database Drivers include mysql, Postgres, AND SQLite
  • fast_log High-performance log impl
  • cogo-redis TODO: an redis client.
  • cogo-grpc TODO: an grpc server/client.

Features

  • The stackful coroutine implementation is based on [generator][generator];

  • Support schedule on a configurable number of threads for multi-core systems;

  • Support coroutine version of a local storage ([CLS][cls]);

  • Support efficient asynchronous network I/O;

  • 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 Mac, aarch64 Linux OS are supported.

  • Support High performance chan(like golang)

  • Support WaitGroup Support(like golang)

  • Support defer!() (like golang)

  • Support Rustls

  • Support Time (like golang)

  • Support error/err!() (like golang)

  • Support select match Ok(v)/Err(e) (like golang)

  • Support Lazy/OnceCell

  • Support SyncMap(like golang)

  • Support Ticker(like golang)

Usage

A naive echo server implemented with Cogo:

#[macro_use]
extern crate cogo;

use cogo::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();
            }
        });
    }
}

More examples

The I/O heavy bound examples

Caveat

There is a detailed [document][caveat] that describes Cogo'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

cogo::config().set_stack_size(8*1024);//default is 4k=4*1024,Multiple of 4kb is recommended
You might also like...
Stack-based programming language which emulates the look and feel of the 60s

Cauchemar Cauchemar is a stack-based programming language inspired by FORTH but more arcane. Emulates the look and feel of a programming language from

TypeRust - simple Rust playground where you can build or run your Rust code and share it with others

Rust playground Welcome to TypeRust! This is a simple Rust playground where you can build or run your Rust code and share it with others. There are a

🐱 A high-speed JIT programming language and its runtime, meow~

🐱 A high-speed JIT programming language and its runtime, meow~

Rust 核心库和标准库的源码级中文翻译,可作为 IDE 工具的智能提示 (Rust core library and standard library translation. can be used as IntelliSense for IDE tools)

Rust 标准库中文版 这是翻译 Rust 库 的地方, 相关源代码来自于 https://github.com/rust-lang/rust。 如果您不会说英语,那么拥有使用中文的文档至关重要,即使您会说英语,使用母语也仍然能让您感到愉快。Rust 标准库是高质量的,不管是新手还是老手,都可以从中

 Gecko is a high-level, general-purpose programming language built on top of the LLVM project.
Gecko is a high-level, general-purpose programming language built on top of the LLVM project.

Gecko is a high-level, general-purpose programming language built on top of the LLVM project. Gecko Technology & principles Gecko is a general-purpose

A type-safe, high speed programming language for scalable systems

A type-safe, high speed programming language for scalable systems! (featuring a cheesy logo!) note: the compiler is unfinished and probably buggy. if

Visualization for Timely Dataflow and Differential Dataflow programs
Visualization for Timely Dataflow and Differential Dataflow programs

DDShow Visualization for Timely Dataflow and Differential Dataflow programs Getting started with ddshow First, install ddshow via cargo. As of now dds

🕶 Assorted checks and validations for writing safer Solana programs.
🕶 Assorted checks and validations for writing safer Solana programs.

vipers 😎 Assorted checks and validations for writing safer Solana programs. Motivation Solana's fee mechanism is unlike Ethereum's, in that the numbe

A tray application for Windows that gives you push notifications and instant downloads of new posts, messages and stories posted by models you subscribe to on Onlyfans.

OF-notifier A tray application for Windows that gives you push notifications and instant downloads of new posts, messages and stories posted by models

Comments
  • Coroutines or goroutines?

    Coroutines or goroutines?

    Is this package more like goroutines or coroutines?

    goroutines: green-threads: cooperative-multitasking through context switching using a library or run-time. (goroutines are non-preemptive, I believe, but the runtime decides when to context switch)

    coroutines: cooperative-multitasking through subroutine yield or calls.

    I didn't really understand from the Readme, which describes this package as coroutines, but compares it to go Goroutines/green threads.

    opened by cameronelliott 0
Releases(v0.1.43)
  • v0.1.43(Feb 22, 2022)

  • v0.1.42(Feb 22, 2022)

  • v0.1.41(Feb 20, 2022)

  • v0.1.40(Feb 19, 2022)

  • v0.1.39(Feb 17, 2022)

  • v0.1.38(Feb 17, 2022)

  • v0.1.37(Feb 16, 2022)

  • v0.1.36(Feb 13, 2022)

  • v0.1.35(Feb 8, 2022)

  • v0.1.33(Feb 8, 2022)

    v0.1.33

    • add spawn_blocking method Used to handle blocking operations, such as reading and writing files, and busy CPU calculations for example:
        let v = cogo::spawn_blocking!(|| {
            return 1;
        });
        assert_eq!(v.unwrap(), 1);
    
    Source code(tar.gz)
    Source code(zip)
  • v0.1.32(Feb 6, 2022)

  • v0.1.31(Feb 2, 2022)

    v0.1.31

    • add rustls example
    • fix chan!() more than length
    • chan!() add method try_send()
    • coroutine remove many unsafe method
    • error remove trait ToString
    • select{} macro don't need ;, and add support match Some()/None/Ok()/Err()
    • add err!() macro
    Source code(tar.gz)
    Source code(zip)
  • v0.1.30(Jan 28, 2022)

  • v0.1.29(Jan 28, 2022)

  • v0.1.28(Jan 26, 2022)

  • v0.1.27(Jan 25, 2022)

  • v0.1.26(Jan 25, 2022)

  • v0.1.24(Jan 16, 2022)

    v0.1.24

    • SyncHashMap Each value takes a very small amount of time
    test bench_sync_hash_map_read   ... bench:           8 ns/iter (+/- 0)
    
    • make SyncHashMap and SyncBtreeMap Read and write separation, space for time, suitable for the scenario of more reading and less writing
    Source code(tar.gz)
    Source code(zip)
  • v0.1.23(Jan 14, 2022)

    v0.1.23

    • Complete stable Time
    • Add err_warp! macro
    • Add Ticker
    let mut t = Arc::new(Ticker::new(Duration::from_secs(1)));
            let tclone = t.clone();
            go!(move ||{
                 for x in tclone.as_ref() {
                   println!("tick {}", x);
                }
            });
            sleep(Duration::from_secs(3));
            t.stop();
    
    Source code(tar.gz)
    Source code(zip)
  • v0.1.22(Jan 14, 2022)

  • v0.1.21(Jan 14, 2022)

  • v0.1.20(Jan 13, 2022)

    v0.1.20

    add time impl just like golang for example:

    use std::time::Duration;
    use cogo::std::time::time::Time;
    
    fn main() {
        let mut t = Time::now();
        println!("{}", t);
        let js = serde_json::to_string(&t).unwrap();
        println!("{}", js);
        let from_js = serde_json::from_str::<Time>(&js).unwrap();
        assert_eq!(from_js, t);
    
        t.add(1 * 24 * Duration::from_secs(3600));// add one day
        println!("add one day:{}", t);
        assert_ne!(from_js, t);
    
        assert_eq!(true, t.before(&Time::now())); //befor
    
        assert_eq!(true, Time::now().after(&t)); //after
    
        let formated = t.format("[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory]:[offset_minute]:[offset_second]");
        println!("{}", formated);
    }
    
    2022-01-14T00:55:38.276807+08:00
    "2022-01-14T00:55:38.276807+08:00"
    add one day:2022-01-15T00:55:38.276807+08:00
    2022-01-15 00:55:38 +08:00:00
    
    Source code(tar.gz)
    Source code(zip)
  • v0.1.19(Jan 9, 2022)

    v0.1.19

    • cogo/std/map Provides the same concurrency map as Golang, with SyncHashMap and SyncBtreeMap.It is suitable for concurrent environments with too many reads and too few writes.
    Source code(tar.gz)
    Source code(zip)
  • v0.1.17(Jan 7, 2022)

    v0.1.17

    • Use a safe way to break the IO thread limit( May limits the number of CPU/IO threads to 64 ) . now you can use it for Workstations with more than 64 cpus or other scenarios. with no limit
    Source code(tar.gz)
    Source code(zip)
  • v0.1.16(Jan 6, 2022)

  • v0.1.15(Jan 5, 2022)

    v0.1.15

    • Optimize the scheduler,Efficiency without theft
    • Fixed panic caused by std::panic::set_hook() befor add if thread::panicking() { return;}
    Source code(tar.gz)
    Source code(zip)
  • v0.1.12(Jan 4, 2022)

  • v0.1.9(Jan 3, 2022)

    v0.1.9

    • mpsc channel support buffer(If the buffered message exceeds the limit, the sender blocks until the message is consumed)
    • mpmc channel support buffer(If the buffered message exceeds the limit, the sender blocks until the message is consumed)
    Source code(tar.gz)
    Source code(zip)
  • v0.1.8(Jan 3, 2022)

  • v0.1.2(Dec 28, 2021)

Owner
co-rs
Make rust concurrency easier
co-rs
Rust library for concurrent data access, using memory-mapped files, zero-copy deserialization, and wait-free synchronization.

mmap-sync mmap-sync is a Rust crate designed to manage high-performance, concurrent data access between a single writer process and multiple reader pr

Cloudflare 97 Jun 26, 2023
Shuttle is a library for testing concurrent Rust code

Shuttle Shuttle is a library for testing concurrent Rust code. It is an implementation of a number of randomized concurrency testing techniques, inclu

Amazon Web Services - Labs 373 Dec 27, 2022
A Matrix bot which can generate "This Week in X" like blog posts

hebbot A Matrix bot which can help to generate periodic / recurrent summary blog posts (also known as "This Week in X"). The bot was inspired by twim-

Häcker Felix 43 Dec 17, 2022
A typed map which can make sure item exist.

Certain Map A typed map which can make sure item exist. What Problem Does It Solve In Rust, we often use Service abstraction for modular structure des

ihc童鞋@提不起劲 27 Jun 26, 2023
Thread-safe clone-on-write container for fast concurrent writing and reading.

sync_cow Thread-safe clone-on-write container for fast concurrent writing and reading. SyncCow is a container for concurrent writing and reading of da

null 40 Jan 16, 2023
Support SIMD low-memory overhead and high-performance adaptive radix tree.

Artful Artful is an adaptive radix tree library for Rust. At a high-level, it's like a BTreeMap. It is based on the implementation of paper, see The A

future 3 Sep 7, 2022
An AI-native lightweight, reliable, and high performance open-source vector database.

What is OasysDB? OasysDB is a vector database that can be used to store and query high-dimensional vectors. Our goal is to make OasysDB fast and easy

Oasys 3 Dec 25, 2023
Rust library for compiling and running other programs.

Exers ?? Exers is a rust library for compiling and running code in different languages and runtimes. Usage example fn main() { // Imports...

olix3001 5 Jun 10, 2023
In this repository you can find modules with code and comments that explain rust syntax and all about Rust lang.

Learn Rust What is this? In this repository you can find modules with code and comments that explain rust syntax and all about Rust lang. This is usef

Domagoj Ratko 5 Nov 5, 2022
A lock-free, partially wait-free, eventually consistent, concurrent hashmap.

A lock-free, partially wait-free, eventually consistent, concurrent hashmap. This map implementation allows reads to always be wait-free on certain pl

Ian Smith 216 Nov 18, 2022