Skip to content

Commit a7bed75

Browse files
authored
feat: Make mailbox sizes configurable with sensible defaults (#149)
2 parents 901c722 + 5527e68 commit a7bed75

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

crates/libtortillas/src/engine/actor.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ pub struct EngineActor {
4848
pub(crate) actor_ref: ActorRef<EngineActor>,
4949

5050
pub(crate) default_piece_storage_strategy: PieceStorageStrategy,
51+
52+
/// Mailbox size for each torrent instance
53+
pub(crate) mailbox_size: usize,
5154
}
5255

5356
pub(crate) type EngineActorArgs = (
@@ -60,6 +63,11 @@ pub(crate) type EngineActorArgs = (
6063
Option<PeerId>,
6164
// Strategy for storing pieces of the torrent.
6265
PieceStorageStrategy,
66+
// Mailbox size for each torrent instance
67+
// Defaults to 64
68+
//
69+
// If 0 is provided, the mailbox size will be unbounded
70+
Option<usize>,
6371
);
6472

6573
impl Actor for EngineActor {
@@ -78,7 +86,8 @@ impl Actor for EngineActor {
7886
async fn on_start(
7987
args: Self::Args, actor_ref: kameo::prelude::ActorRef<Self>,
8088
) -> Result<Self, Self::Error> {
81-
let (tcp_addr, utp_addr, udp_addr, peer_id, default_piece_storage_strategy) = args;
89+
let (tcp_addr, utp_addr, udp_addr, peer_id, default_piece_storage_strategy, mailbox_size) =
90+
args;
8291

8392
let tcp_addr = tcp_addr.unwrap_or_else(|| SocketAddr::from(([0, 0, 0, 0], 0)));
8493
// Should this be port 6881?
@@ -102,6 +111,7 @@ impl Actor for EngineActor {
102111
peer_id,
103112
actor_ref,
104113
default_piece_storage_strategy,
114+
mailbox_size: mailbox_size.unwrap_or(64),
105115
})
106116
}
107117

crates/libtortillas/src/engine/messages.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use kameo::{
22
Actor, Reply, mailbox,
33
prelude::{ActorRef, Context, Message},
44
};
5-
use tracing::error;
5+
use tracing::{error, warn};
66

77
use super::EngineActor;
88
use crate::{
@@ -105,7 +105,17 @@ impl Message<EngineRequest> for EngineActor {
105105
None,
106106
self.default_piece_storage_strategy.clone(),
107107
),
108-
mailbox::unbounded(),
108+
// if the size is 0, we use an unbounded mailbox
109+
match self.mailbox_size {
110+
0 => {
111+
warn!(
112+
?info_hash,
113+
"Spawning torrent with unbounded mailbox; this could drastically increase memory usage"
114+
);
115+
mailbox::unbounded()
116+
}
117+
size => mailbox::bounded(size),
118+
},
109119
);
110120

111121
self.actor_ref.link(&torrent_ref).await;

crates/libtortillas/src/engine/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,28 @@ impl Engine {
100100
/// Strategy for storing pieces of the torrent.
101101
#[builder(default)]
102102
piece_storage_strategy: PieceStorageStrategy,
103+
/// The mailbox size for each torrent instance.
104+
///
105+
/// In simple terms, this is the number of messages that each torrent
106+
/// instance can have in queue.
107+
///
108+
/// If `Some(0)` is provided, the mailbox will be unbounded (no limit).
109+
/// If `None` is provided, a sensible default is used.
110+
///
111+
/// Higher values increase memory usage but reduce sender backpressure
112+
/// when the mailbox is busy, which can improve throughput. Lower values
113+
/// do the inverse.
114+
///
115+
/// Default: `64` when `None` is provided.
116+
mailbox_size: Option<usize>,
103117
) -> Self {
104118
let args: EngineActorArgs = (
105119
tcp_addr,
106120
utp_addr,
107121
udp_addr,
108122
Some(custom_id),
109123
piece_storage_strategy,
124+
mailbox_size,
110125
);
111126

112127
let actor = EngineActor::spawn(args);

0 commit comments

Comments
 (0)