Expand description
A bounded tokio mpsc channel that bounds the queue by the total weight of the messages in it, rather than by the number of messages.
Each type sent through the channel reports a weight via Weigh - usually its
size in bytes, but any additive measure works (rows, estimated cost, etc.). The
channel has a fixed weight budget; a send waits until the messages already in
the channel leave enough room for the new one, then goes through. This lets you
cap the memory (or any other weighed resource) a producer/consumer pipeline
holds at once, even when messages vary a lot in size.
A tokio::sync::Semaphore tracks the budget: one permit per weight unit. A
message takes permits equal to its weight while it is in the channel and while
the receiver still holds the Lease that recv returns. The permits go
back to the budget when the Lease is dropped, which frees room for more sends.
Holding the Lease while you use the value keeps the bound accurate; dropping
it (or calling Lease::into_inner) frees the room immediately.
use weighted_mpsc::channel;
// Cap the in-flight bytes at 1 MiB; the count buffer (16) is a backstop.
let (tx, mut rx) = channel::<Vec<u8>>(16, 1024 * 1024);
tx.send(vec![0u8; 256 * 1024]).await.unwrap();
// `msg` derefs to the Vec<u8>; the budget is held until `msg` is dropped.
let msg = rx.recv().await.unwrap();
assert_eq!(msg.len(), 256 * 1024);Structs§
- Builder
- Builder for a weighted channel. Use
channelfor the common case. - Lease
- A received message together with the budget it holds.
- Weighted
Receiver - The receiving half of a weighted channel.
- Weighted
Sender - The sending half of a weighted channel. Cloneable and shareable across tasks.
Enums§
- Oversized
- What to do with a message that weighs more than the whole budget.
- Send
Error - The error returned by
WeightedSender::send. - TrySend
Error - The error returned by
WeightedSender::try_send.
Traits§
- Weigh
- A value that can report its own weight in the unit the channel’s budget uses.
Functions§
- channel
- Convenience constructor for a weighted channel with the default policy
(
Oversized::Allow,min_weight1). For anything else, useBuilder.