forked from Furumi/furumi-web
Bound the federated tracks one import lookup keeps
Only each peer catalogue was limited to 4 MiB, so one lookup of up to 16 untrusted owners could bring hundreds of thousands of made-up tracks, and the pass kept 64 such lookups. A lookup now keeps at most 5,000 tracks; a catalogue whose new tracks do not fit counts as lost (too large). The pass remembers 8 lookups, enough for folded spellings and collaboration parts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e7d970c60d
commit
4b73de46ec
1 file changed
+53
-12
@@ -29,8 +29,13 @@ use crate::library_import::store::{AMBIGUOUS_LOCAL, Resolution};
|
|||||||
|
|
||||||
/// Artist groups searched at once by all imports of this process.
|
/// Artist groups searched at once by all imports of this process.
|
||||||
static GROUPS: Semaphore = Semaphore::const_new(2);
|
static GROUPS: Semaphore = Semaphore::const_new(2);
|
||||||
/// Lookups remembered during one pass.
|
/// Lookups remembered during one pass. Each artist group is looked up once;
|
||||||
const CACHE_NAMES: usize = 64;
|
/// the cache serves folded spellings and the parts of collaborations.
|
||||||
|
const CACHE_NAMES: usize = 8;
|
||||||
|
/// Federated tracks (unique content ids) kept from one lookup. Peers are not
|
||||||
|
/// trusted: the 4 MiB limit per catalogue alone lets one lookup bring
|
||||||
|
/// hundreds of thousands of made-up tracks.
|
||||||
|
const MAX_FOUND_TRACKS: usize = 5_000;
|
||||||
|
|
||||||
/// `resolution_note` when the federation node is off or stopped.
|
/// `resolution_note` when the federation node is off or stopped.
|
||||||
pub const FEDERATION_UNAVAILABLE: &str = "federation_unavailable";
|
pub const FEDERATION_UNAVAILABLE: &str = "federation_unavailable";
|
||||||
@@ -68,16 +73,19 @@ impl Found {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Takes the tracks of a catalogue with a verifiable content id and a
|
/// Takes the tracks of a catalogue with a verifiable content id and a
|
||||||
/// source the network can serve.
|
/// source the network can serve, up to [`MAX_FOUND_TRACKS`]. `false` when
|
||||||
fn add_catalog(&mut self, owner: &str, artist: &CatalogArtist) {
|
/// some new tracks did not fit: the catalogue counts as too large.
|
||||||
|
fn add_catalog(&mut self, owner: &str, artist: &CatalogArtist) -> bool {
|
||||||
self.names.push(artist.name.clone());
|
self.names.push(artist.name.clone());
|
||||||
|
let mut complete = true;
|
||||||
for release in &artist.releases {
|
for release in &artist.releases {
|
||||||
for track in &release.tracks {
|
for track in &release.tracks {
|
||||||
self.add_track(owner, &artist.name, track, &release.title, release.year);
|
complete &=
|
||||||
|
self.add_track(owner, &artist.name, track, &release.title, release.year);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for appearance in &artist.appears_on {
|
for appearance in &artist.appears_on {
|
||||||
self.add_track(
|
complete &= self.add_track(
|
||||||
owner,
|
owner,
|
||||||
&artist.name,
|
&artist.name,
|
||||||
&appearance.track,
|
&appearance.track,
|
||||||
@@ -85,6 +93,7 @@ impl Found {
|
|||||||
appearance.year,
|
appearance.year,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
complete
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_track(
|
fn add_track(
|
||||||
@@ -94,16 +103,19 @@ impl Found {
|
|||||||
track: &CatalogTrack,
|
track: &CatalogTrack,
|
||||||
release: &str,
|
release: &str,
|
||||||
year: Option<i32>,
|
year: Option<i32>,
|
||||||
) {
|
) -> bool {
|
||||||
let Some(content_id) = track
|
let Some(content_id) = track
|
||||||
.content_id
|
.content_id
|
||||||
.as_deref()
|
.as_deref()
|
||||||
.and_then(music_dht::normalize_content_id)
|
.and_then(music_dht::normalize_content_id)
|
||||||
else {
|
else {
|
||||||
return;
|
return true;
|
||||||
};
|
};
|
||||||
if track.title.trim().is_empty() || !usable_source(owner, &track.item_id) {
|
if track.title.trim().is_empty() || !usable_source(owner, &track.item_id) {
|
||||||
return;
|
return true;
|
||||||
|
}
|
||||||
|
if self.tracks.len() >= MAX_FOUND_TRACKS && !self.by_content.contains_key(&content_id) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
let artists = if track.artists.is_empty() {
|
let artists = if track.artists.is_empty() {
|
||||||
vec![artist.to_owned()]
|
vec![artist.to_owned()]
|
||||||
@@ -131,6 +143,7 @@ impl Found {
|
|||||||
},
|
},
|
||||||
std::slice::from_ref(&owner.to_owned()),
|
std::slice::from_ref(&owner.to_owned()),
|
||||||
);
|
);
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a track; another owner of a known content id counts, and the
|
/// Adds a track; another owner of a known content id counts, and the
|
||||||
@@ -316,7 +329,9 @@ impl<'a> NetworkSearch<'a> {
|
|||||||
Ok(None) => break,
|
Ok(None) => break,
|
||||||
Ok(Some(Ok((owner, artist)))) => {
|
Ok(Some(Ok((owner, artist)))) => {
|
||||||
counts.catalogs += 1;
|
counts.catalogs += 1;
|
||||||
found.add_catalog(&owner, &artist);
|
if !found.add_catalog(&owner, &artist) {
|
||||||
|
counts.catalog_errors += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Errors describe the transport or the peer, never the artist name.
|
// Errors describe the transport or the peer, never the artist name.
|
||||||
Ok(Some(Err(CatalogError::Catalog(error)))) => {
|
Ok(Some(Err(CatalogError::Catalog(error)))) => {
|
||||||
@@ -649,8 +664,8 @@ mod tests {
|
|||||||
}],
|
}],
|
||||||
};
|
};
|
||||||
let mut found = Found::default();
|
let mut found = Found::default();
|
||||||
found.add_catalog("peer-1", &artist);
|
assert!(found.add_catalog("peer-1", &artist));
|
||||||
found.add_catalog("peer-2", &artist);
|
assert!(found.add_catalog("peer-2", &artist));
|
||||||
assert_eq!(found.tracks.len(), 1);
|
assert_eq!(found.tracks.len(), 1);
|
||||||
let (fed, owners) = &found.tracks[0];
|
let (fed, owners) = &found.tracks[0];
|
||||||
assert_eq!(owners, &["peer-1", "peer-2"]);
|
assert_eq!(owners, &["peer-1", "peer-2"]);
|
||||||
@@ -747,6 +762,32 @@ mod tests {
|
|||||||
assert_eq!(nothing.candidates.unwrap().as_array().unwrap().len(), 1);
|
assert_eq!(nothing.candidates.unwrap().as_array().unwrap().len(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn one_lookup_keeps_a_bounded_number_of_tracks() {
|
||||||
|
let content_id = |n: usize| format!("b3:{n:064x}");
|
||||||
|
let release = |tracks: std::ops::Range<usize>| CatalogRelease {
|
||||||
|
title: "Album".into(),
|
||||||
|
tracks: tracks
|
||||||
|
.map(|n| track("Song", Some(content_id(n)), &format!("item-{n}")))
|
||||||
|
.collect(),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let artist = |tracks| CatalogArtist {
|
||||||
|
name: "Artist".into(),
|
||||||
|
releases: vec![release(tracks)],
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let mut found = Found::default();
|
||||||
|
assert!(found.add_catalog("peer-1", &artist(0..MAX_FOUND_TRACKS)));
|
||||||
|
// Known tracks still gain owners; new ones beyond the limit are dropped.
|
||||||
|
assert!(found.add_catalog("peer-2", &artist(0..10)));
|
||||||
|
assert!(!found.add_catalog("peer-3", &artist(5..MAX_FOUND_TRACKS + 5)));
|
||||||
|
assert_eq!(found.tracks.len(), MAX_FOUND_TRACKS);
|
||||||
|
assert_eq!(found.tracks[0].1, ["peer-1", "peer-2"]);
|
||||||
|
assert_eq!(found.tracks[MAX_FOUND_TRACKS - 1].1, ["peer-1", "peer-3"]);
|
||||||
|
assert!(!found.by_content.contains_key(&content_id(MAX_FOUND_TRACKS)));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn only_latin_diacritics_are_folded_for_the_network() {
|
fn only_latin_diacritics_are_folded_for_the_network() {
|
||||||
assert_eq!(latin_folded("Beyoncé").as_deref(), Some("beyonce"));
|
assert_eq!(latin_folded("Beyoncé").as_deref(), Some("beyonce"));
|
||||||
|
|||||||
Reference in new issue
Block a user