Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub mod twin;
pub(crate) mod twinfields;
pub mod unpack;
pub(crate) mod unpacklimits;
pub(crate) mod unpackslots;
pub(crate) mod url;
pub mod urlcoord;
pub mod util;
Expand Down
21 changes: 17 additions & 4 deletions rust/src/livefeed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ struct Waiting<'a> {
waiting: bool,
/// Article writes reported when the extraction last looked (see `crate::livewrite`).
writes: u64,
/// The feed's own write count when it last looked (`VolumeFeed::writes`).
own: u64,
}

impl Waiting<'_> {
Expand All @@ -153,17 +155,28 @@ impl Waiting<'_> {
on_wait,
waiting: false,
writes: crate::livewrite::write_count(),
own: 0,
}
}

/// Waits for anything new: a volume, or an article written into a volume being read.
fn idle(&mut self) {
/// When something of the feed's own set was written meanwhile (`feed.writes()`), the
/// caller is told it works again before its next step decodes it (a direct unpack takes
/// its unpack slot back then: it decodes only while it holds one). Writes of other
/// downloads wake it to look, but do not take a slot.
fn idle(&mut self, feed: &dyn VolumeFeed) {
if !self.waiting {
self.waiting = true;
self.own = feed.writes();
(self.on_wait)(true);
Comment on lines +170 to 171

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve writes arriving before the idle snapshot

For live 7z/ZIP or split-file extraction, an article can arrive after the decoder reports no progress but before idle() snapshots feed.writes(). That write is then included in self.own, and the callback releases the slot despite new input already being available. If no further writes arrive, the comparison below never calls busy(), yet feed_members or join_live proceeds to decode or copy that input on its next iteration; their progress callback runs only afterward. With UnpackSlots=1, another extraction can acquire the released slot and run concurrently. Track the write generation from before the unsuccessful processing attempt, or reacquire admission before processing newly available input.

Useful? React with 👍 / 👎.

}
crate::livewrite::wait_any(self.writes, IDLE);
self.writes = crate::livewrite::write_count();
let own = feed.writes();
if own != self.own {
self.own = own;
self.busy();
}
}
fn busy(&mut self) {
if self.waiting {
Expand Down Expand Up @@ -911,7 +924,7 @@ pub fn extract(
}
}
}
waiting.idle();
waiting.idle(&*vols.feed);
};
waiting.busy();
vols.expect = lens;
Expand Down Expand Up @@ -1167,7 +1180,7 @@ fn feed_members(
log(1, &why);
return fail(&members, &empties, why);
} else {
waiting.idle();
waiting.idle(&*vols.feed);
}
}
remove_temp_dir(&dest);
Expand Down Expand Up @@ -1377,7 +1390,7 @@ fn join_live(
log(1, &why);
return fail(why);
} else {
waiting.idle();
waiting.idle(&*vols.feed);
}
}
let size = piece_len.unwrap_or(0) * (n as u64 - 1) + last_len;
Expand Down
16 changes: 16 additions & 0 deletions rust/src/livewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ static VOLUMES: std::sync::LazyLock<Mutex<HashMap<i32, Vec<Arc<LiveVolume>>>>> =
/// Bumped on every reported write, for live extractions that poll (see [`wait_any`]).
static WRITES: (Mutex<u64>, Condvar) = (Mutex::new(0), Condvar::new());

/// Writes (and forgets) reported per download, so an extraction can tell whether anything
/// of its own download was written (see [`nzb_write_count`]).
static NZB_WRITES: std::sync::LazyLock<Mutex<HashMap<i32, u64>>> =
std::sync::LazyLock::new(|| Mutex::new(HashMap::new()));

fn bump_nzb(nzb_id: i32) {
*lock(&NZB_WRITES).entry(nzb_id).or_insert(0) += 1;
}

/// How many writes (and forgets) download `nzb_id` reported so far.
pub fn nzb_write_count(nzb_id: i32) -> u64 {
lock(&NZB_WRITES).get(&nzb_id).copied().unwrap_or(0)
}

/// Where a live extraction read packed bytes from.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ReadKind {
Expand Down Expand Up @@ -396,6 +410,7 @@ fn report(nzb_id: i32, file_id: i32, path: Option<&Path>, offset: u64, len: u64)
if first_head {
note_head(nzb_id);
}
bump_nzb(nzb_id);
let (m, cv) = &WRITES;
*lock(m) += 1;
cv.notify_all();
Expand All @@ -422,6 +437,7 @@ pub fn forget(nzb_id: i32) {
w.wake();
}
}
bump_nzb(nzb_id);
let (m, cv) = &WRITES;
*lock(m) += 1;
cv.notify_all();
Expand Down
6 changes: 5 additions & 1 deletion rust/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1784,7 +1784,7 @@ pub(crate) fn known_enum_names(option: &str) -> Option<&'static [&'static CStr]>
/// Emits all default option `(name, value)` pairs for `Options::InitDefaults`.
pub fn init_defaults_with(disable_curses: bool, mut set_opt: impl FnMut(&CStr, &CStr)) {
let output_mode = if disable_curses { c"color" } else { c"curses" };
let defaults: [(&CStr, &CStr); 134] = [
let defaults: [(&CStr, &CStr); 135] = [
(c"MainDir", c"~/downloads"),
(c"WebDir", c""),
(c"ConfigTemplate", c""),
Expand Down Expand Up @@ -1897,6 +1897,7 @@ pub fn init_defaults_with(disable_curses: bool, mut set_opt: impl FnMut(&CStr, &
(c"Unpack", c"yes"),
(c"DirectUnpack", c"yes"),
(c"FeedThreads", c"0"),
(c"UnpackSlots", c"0"),
(c"UseTempUnpackDir", c"yes"),
(c"UnpackCleanupDisk", c"yes"),
(c"UnrarCmd", c""),
Expand Down Expand Up @@ -2180,6 +2181,8 @@ pub struct NzbgetRsParsedOptions {
/// Worker threads for a 7z LZMA2 member's independent blocks in live extraction
/// (`livefeed::feed_threads`): 0 picks, 1 is none, more is that many (at most 64).
pub feed_threads: i32,
/// Extractions that run at once (`unpackslots::capacity_for`): 0 picks, n is n (at most 64).
pub unpack_slots: i32,
}

impl NzbgetRsParsedOptions {
Expand Down Expand Up @@ -2708,6 +2711,7 @@ pub fn init_scalar_options_host<H: OptionsHost>(
.parse_int_value(c"LightConnections", 10)
.clamp(0, crate::light_pool::LIGHT_MAX);
s.feed_threads = host.parse_int_value(c"FeedThreads", 10).clamp(0, 64);
s.unpack_slots = host.parse_int_value(c"UnpackSlots", 10).clamp(0, 64);
s.url_force = i32::from(host.parse_bool_value(c"UrlForce"));
s.cert_check = i32::from(host.parse_bool_value(c"CertCheck"));
s.reorder_files = i32::from(host.parse_bool_value(c"ReorderFiles"));
Expand Down
Loading
Loading