#logger #message-passing #concurrency

spmc-logger

单生产者多消费者持久化日志记录器

1 个不稳定版本

0.1.0 2023年11月25日

#1056异步

MIT 许可证

9KB
151

spmc-logger

这是一个单写多读的日志记录器。如果消息 x 写入到日志记录器中,则 x 不会从日志记录器中删除,直到 y 个读取器读取了它。

用法

Logger::new(NUM_OF_READERS, LOGGER_BUFFER_SIZE)
// Eg. This means this logger will hold a maximum of 100 messages and will drop a single message when 
// 3 readers have read it.
let l = Logger::new(3, 100)
// writing and reading
// Thread A
l.write("hello world")
// Thread B
// message can be an error if more readers outside the quorum is trying to read a message.
// it can be none if we've read all the messages in the buffer and some if there is a message to be read.

// when the reader reads a message, it also has a `is_valid` key which tells us if the bytes have been
// malformed somewhere between the writing and reading. Think of it as an integrity hash.
match l.read() {
    Ok(Some(message)) => println!(message),
    Ok(None) => (),
    Err(e) => ()
}

示例

src/lib.rs 中的 it_works 测试是一个很好的例子。

依赖项

~5–13MB
~236K SLoC