mirror of
https://github.com/block/buzz.git
synced 2026-11-28 04:01:45 +00:00
refactor(mobile): remove the dead per-signer cache sweep (#9902)
## Summary The media cache and saved channel-directory sweeps kept, for each saved community, only the files of its current signer, and deleted the files of any other account under the same community id. That state came only from the same-relay credential replacement removed in #9896. Community ids are random, and every remaining path that rewrites a saved community's key keeps the same key (hosted reconnect refuses a different one). So no community can change signer anymore, and the per-signer cleanup is dead. This deletes it. The sweep now keeps whole community directories by id and deletes the directories of removed communities, as before. ## Changes - `MediaDiskCache.removeOrphans` takes the retained community ids and no longer sweeps signer subdirectories. - Delete `OpenChannelDirectoryStorage.removeOrphans`. Its only job was deleting other signers' files; a removed community's directory still goes with its community directory. - Delete the tests and assertions that covered signer replacement. ### Added lines - The new, shorter `MediaDiskCache.removeOrphans` body and doc comment. - The provider's retained set: community ids that have a signer, the same communities the old map kept. - Two doc comment rewordings that no longer mention replaced accounts. - The media cache test's removal check, rewritten for the new signature. ## Validation - Full mobile test suite, analyze, format and file-size checks pass. - No user-visible change, so no screenshots. --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
This commit is contained in:
@@ -73,39 +73,6 @@ class OpenChannelDirectoryStorage {
|
||||
),
|
||||
);
|
||||
|
||||
/// Deletes every saved directory under [root] except each retained
|
||||
/// community's file for its current signer (community id to signer), and
|
||||
/// the pending copy a save of it may be writing. A
|
||||
/// failed delete is retried on the next call. Removed communities lose their
|
||||
/// whole directory to [MediaDiskCache.removeOrphans].
|
||||
static Future<void> removeOrphans(
|
||||
Directory root,
|
||||
Map<String, String?> Function() retained,
|
||||
) async {
|
||||
final communities = Directory(p.join(root.path, 'communities'));
|
||||
if (!await communities.exists()) return;
|
||||
Set<String> keep() => {
|
||||
for (final MapEntry(key: id, value: signer) in retained().entries)
|
||||
if (signer != null) ...{
|
||||
fileFor(root, id, signer).path,
|
||||
'${fileFor(root, id, signer).path}.pending',
|
||||
},
|
||||
};
|
||||
await for (final community in communities.list(followLinks: false)) {
|
||||
if (community is! Directory) continue;
|
||||
final saved = Directory(p.join(community.path, 'channel-directory'));
|
||||
if (!await saved.exists()) continue;
|
||||
await for (final entry in saved.list(followLinks: false)) {
|
||||
if (keep().contains(entry.path)) continue;
|
||||
try {
|
||||
await entry.delete(recursive: true);
|
||||
} on FileSystemException catch (error) {
|
||||
debugPrint('[OpenChannelDirectoryStorage] cleanup failed: $error');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The saved directory, or null when there is none or it is unusable.
|
||||
Future<SavedOpenChannelDirectory?> read() async {
|
||||
final file = await _file;
|
||||
@@ -164,7 +131,8 @@ class OpenChannelDirectoryStorage {
|
||||
}
|
||||
|
||||
/// Stops every later read and write, as when the community or account is
|
||||
/// retired. The file itself is deleted by [removeOrphans].
|
||||
/// retired. The file is deleted with its community, by
|
||||
/// [MediaDiskCache.removeOrphans].
|
||||
void close() => _closed = true;
|
||||
|
||||
Future<void> _discard(File file) async {
|
||||
|
||||
@@ -20,7 +20,6 @@ import '../push/dev_push_lease.dart';
|
||||
import '../push/push_bridge.dart';
|
||||
import '../push/push_lease_revocation_outbox.dart';
|
||||
import '../push/push_subscription.dart';
|
||||
import '../channels/open_channel_directory_storage.dart';
|
||||
import '../relay/media_disk_cache.dart';
|
||||
import '../relay/signed_event_relay.dart';
|
||||
import '../relay/relay_provider.dart' show RelayConfig, pubkeyFromNsec;
|
||||
@@ -434,12 +433,11 @@ class CommunityListNotifier extends AsyncNotifier<List<Community>> {
|
||||
}
|
||||
|
||||
/// Deletes the image caches and saved channel directories of communities no
|
||||
/// longer in the list, and of accounts a community no longer signs in as. A
|
||||
/// failure, or a write that lands after the delete, is swept on the next
|
||||
/// launch.
|
||||
/// longer in the list. A failure, or a write that lands after the delete, is
|
||||
/// swept on the next launch.
|
||||
///
|
||||
/// Sweeps run one at a time, each keeping what the community list holds
|
||||
/// when it starts, so an older sweep can never delete a newer account's
|
||||
/// when it starts, so an older sweep can never delete a newer community's
|
||||
/// cache. [fallback] stands in for the list while it is still loading.
|
||||
Future<void> _removeOrphanMediaCaches({List<Community>? fallback}) =>
|
||||
_mediaSweep = _mediaSweep.then((_) => _sweepMediaCaches(fallback));
|
||||
@@ -454,17 +452,16 @@ class CommunityListNotifier extends AsyncNotifier<List<Community>> {
|
||||
state.isLoading ? fallback ?? state.value : state.value;
|
||||
var communities = current();
|
||||
if (communities == null) return;
|
||||
Map<String, String?> retained() {
|
||||
Set<String> retained() {
|
||||
// A list briefly unavailable mid-sweep keeps the last one seen.
|
||||
communities = current() ?? communities;
|
||||
return {
|
||||
for (final community in communities!)
|
||||
community.id: _channelSnapshotPubkey(community)?.toLowerCase(),
|
||||
if (_channelSnapshotPubkey(community) != null) community.id,
|
||||
};
|
||||
}
|
||||
|
||||
await MediaDiskCache.removeOrphans(root, retained);
|
||||
await OpenChannelDirectoryStorage.removeOrphans(root, retained);
|
||||
} catch (error, stackTrace) {
|
||||
developer.log(
|
||||
'Media cache deletion failed; the next launch will retry',
|
||||
|
||||
@@ -22,8 +22,8 @@ typedef MediaDiskCacheHit = ({Uint8List bytes, bool isStale});
|
||||
/// Built on `flutter_cache_manager`. It keeps the bytes each URL served, so a
|
||||
/// relay thumbnail and its original are separate entries. Each community and
|
||||
/// account has its own directory under the caches root (see
|
||||
/// [mediaCacheRootProvider]), deleted with the community or account. Least recently used files are evicted once
|
||||
/// the directory holds more than [maxBytes].
|
||||
/// [mediaCacheRootProvider]), deleted with its community. Least recently used
|
||||
/// files are evicted once the directory holds more than [maxBytes].
|
||||
///
|
||||
/// Relay media URLs are content hashes and never go stale. Other URLs can
|
||||
/// change in place, so they are served from disk but refetched in the
|
||||
@@ -133,32 +133,19 @@ class MediaDiskCache {
|
||||
p.join(communityDirectory(root, communityId).path, 'media', signer),
|
||||
);
|
||||
|
||||
/// Keeps only each retained community's cache for its current signer
|
||||
/// (community id to signer pubkey) and deletes every other directory under
|
||||
/// [root]: removed communities and replaced accounts, including writes
|
||||
/// that landed after their removal. A failed delete is retried on the
|
||||
/// next call.
|
||||
/// Deletes every directory under [root] except the [retained] communities'
|
||||
/// (by id), including writes that landed after their removal. A failed
|
||||
/// delete is retried on the next call.
|
||||
///
|
||||
/// [retained] is read again before each delete, so a community or account
|
||||
/// added while the sweep runs is kept.
|
||||
/// [retained] is read again before each delete, so a community added while
|
||||
/// the sweep runs is kept.
|
||||
static Future<void> removeOrphans(
|
||||
Directory root,
|
||||
Map<String, String?> Function() retained,
|
||||
) async {
|
||||
Set<String> keep() => {
|
||||
for (final MapEntry(key: id, value: signer) in retained().entries)
|
||||
if (signer != null) directoryFor(root, id, signer).path,
|
||||
};
|
||||
final communities = Directory(p.join(root.path, 'communities'));
|
||||
await _deleteExcept(communities, () {
|
||||
return {for (final path in keep()) p.dirname(p.dirname(path))};
|
||||
});
|
||||
if (!await communities.exists()) return;
|
||||
await for (final community in communities.list(followLinks: false)) {
|
||||
if (community is! Directory) continue;
|
||||
await _deleteExcept(Directory(p.join(community.path, 'media')), keep);
|
||||
}
|
||||
}
|
||||
Set<String> Function() retained,
|
||||
) => _deleteExcept(
|
||||
Directory(p.join(root.path, 'communities')),
|
||||
() => {for (final id in retained()) communityDirectory(root, id).path},
|
||||
);
|
||||
|
||||
/// Deletes the entries of [parent] whose paths are not in [keep].
|
||||
static Future<void> _deleteExcept(
|
||||
|
||||
@@ -144,39 +144,4 @@ void main() {
|
||||
);
|
||||
expect(await storage.read(), isNull);
|
||||
});
|
||||
|
||||
test(
|
||||
'cleanup keeps only each community\'s current account and its save',
|
||||
() async {
|
||||
Future<void> save(String community, String signer) =>
|
||||
OpenChannelDirectoryStorage(
|
||||
Future.value(fileFor(community, signer)),
|
||||
).write(
|
||||
SavedOpenChannelDirectory(
|
||||
metas: [_meta('a', 1)],
|
||||
fullAt: 1,
|
||||
readAt: 1,
|
||||
),
|
||||
);
|
||||
await save('one', 'current');
|
||||
await save('one', 'signed-out');
|
||||
await save('two', 'current');
|
||||
// A save of the retained account in progress, and a stale one.
|
||||
final pending = File('${fileFor('one', 'current').path}.pending')
|
||||
..writeAsStringSync('');
|
||||
final stalePending = File('${fileFor('one', 'signed-out').path}.pending')
|
||||
..writeAsStringSync('');
|
||||
|
||||
await OpenChannelDirectoryStorage.removeOrphans(
|
||||
root,
|
||||
() => {'one': 'current', 'two': null},
|
||||
);
|
||||
|
||||
expect(fileFor('one', 'current').existsSync(), isTrue);
|
||||
expect(fileFor('one', 'signed-out').existsSync(), isFalse);
|
||||
expect(fileFor('two', 'current').existsSync(), isFalse);
|
||||
expect(pending.existsSync(), isTrue);
|
||||
expect(stalePending.existsSync(), isFalse);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -703,10 +703,6 @@ void main() {
|
||||
MediaDiskCache.communityDirectory(mediaRoot, 'restored'),
|
||||
);
|
||||
final oldDirectory = await seedDirectory(community.id, oldKeys.public);
|
||||
final retiredDirectory = await seedDirectory(
|
||||
community.id,
|
||||
nostr.Keys.generate().public,
|
||||
);
|
||||
container = createContainer(
|
||||
overrides: [
|
||||
savedPrefsProvider.overrideWithValue(prefs),
|
||||
@@ -719,7 +715,6 @@ void main() {
|
||||
expect(await cache.write(restoredToken, snapshot), isFalse);
|
||||
expect(await eventuallyExists(orphanMedia), isFalse);
|
||||
expect(await oldMedia.exists(), isTrue);
|
||||
expect(await eventuallyExists(retiredDirectory), isFalse);
|
||||
expect(await oldDirectory.exists(), isTrue);
|
||||
|
||||
await communityStorage.saveActiveId(community.id);
|
||||
|
||||
@@ -260,15 +260,11 @@ void main() {
|
||||
replaceExternal = null;
|
||||
expect(fetched, _externalUrls.take(2));
|
||||
|
||||
// A replaced account's directory goes, including writes that land after
|
||||
// its delete; removing the community deletes its directory.
|
||||
// Removing the community deletes its directory.
|
||||
expect(await directory.exists(), isTrue);
|
||||
await MediaDiskCache.removeOrphans(root, () => {'community-1': 'signer-2'});
|
||||
expect(await directory.exists(), isFalse);
|
||||
await directory.create(recursive: true);
|
||||
await MediaDiskCache.removeOrphans(root, () => {'community-1': 'signer-1'});
|
||||
await MediaDiskCache.removeOrphans(root, () => {'community-1'});
|
||||
expect(await directory.exists(), isTrue);
|
||||
await MediaDiskCache.removeOrphans(root, () => {'community-2': 'signer-1'});
|
||||
await MediaDiskCache.removeOrphans(root, () => {'community-2'});
|
||||
expect(
|
||||
await MediaDiskCache.communityDirectory(root, 'community-1').exists(),
|
||||
isFalse,
|
||||
|
||||
Reference in New Issue
Block a user