Skip to content

Commit 4a3f77b

Browse files
committed
fix(mobile): recover saturated notification cache exports
Signed-off-by: Tom Brow <[email protected]>
1 parent 16c83d6 commit 4a3f77b

5 files changed

Lines changed: 351 additions & 8 deletions

File tree

‎mobile/lib/features/channels/channels_provider.dart‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
77

88
import '../../shared/community/community_provider.dart';
99
import '../../shared/push/push_presentation_cache.dart';
10+
import '../../shared/push/push_presentation_export_recovery.dart';
1011
import '../../shared/relay/relay.dart';
1112
import '../../shared/theme/theme_provider.dart';
1213
import '../../shared/utils/string_utils.dart';
@@ -41,6 +42,8 @@ const _authoredRootIdsPrefix = 'buzz-thread-authored.v1';
4142
/// for any visible channel event kind. Chunks stay within the relay's explicit
4243
/// channel cap and incoming events bump `lastMessageAt` for their channel.
4344
class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
45+
final _pushExport = PushPresentationExportRecovery();
46+
4447
static const _backstopInterval = Duration(seconds: 60);
4548

4649
final Map<String, _LiveChunkSubscription> _liveSubscriptionsByChunk = {};
@@ -300,10 +303,12 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
300303
// linkage validation and member-count hydration.
301304
if (memberEvents.isNotEmpty) _cacheMemberSnapshots(memberEvents);
302305
unawaited(
303-
cacheBuzzPushChannelEvents(communityID, dedupedMetas, [
304-
...memberships,
305-
...memberEvents,
306-
]),
306+
_pushExport.export(
307+
() => cacheBuzzPushChannelEvents(communityID, dedupedMetas, [
308+
...memberships,
309+
...memberEvents,
310+
]),
311+
),
307312
);
308313
final memberCounts = _memberCountsByChannelId(memberEvents);
309314
for (var i = 0; i < channels.length; i++) {

‎mobile/lib/shared/profile/user_cache_provider.dart‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
55
import '../community/community_provider.dart';
66
import '../crypto/nip_oa.dart';
77
import '../push/push_presentation_cache.dart';
8+
import '../push/push_presentation_export_recovery.dart';
89
import '../relay/relay.dart';
910
import 'user_profile.dart';
1011

@@ -14,6 +15,7 @@ import 'user_profile.dart';
1415
/// kind:0 batch query (NIP-01 `authors` filter) every 50ms.
1516
class UserCacheNotifier extends Notifier<Map<String, UserProfile>> {
1617
final Set<String> _pending = {};
18+
final _pushExport = PushPresentationExportRecovery();
1719
final Map<String, ({int createdAt, String eventId})> _profileEventOrders = {};
1820
Timer? _batchTimer;
1921
Completer<bool>? _batchCompleter;
@@ -141,7 +143,11 @@ class UserCacheNotifier extends Notifier<Map<String, UserProfile>> {
141143
..addAll(updatedOrders);
142144
state = updated;
143145
if (communityID != null) {
144-
unawaited(cacheBuzzPushProfileEvents(communityID, events));
146+
unawaited(
147+
_pushExport.export(
148+
() => cacheBuzzPushProfileEvents(communityID, events),
149+
),
150+
);
145151
}
146152
succeeded = true;
147153
} catch (_) {

‎mobile/lib/shared/push/push_presentation_cache.dart‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ Future<void> _presentationExportTail = Future.value();
1515
const _maximumPresentationExports = 8;
1616
int _outstandingPresentationExports = 0;
1717

18+
/// Temporary admission failure; detached producers must retain and retry input.
19+
class PushPresentationExportQueueFull extends StateError {
20+
/// Creates a saturation error for the shared eight-export budget.
21+
PushPresentationExportQueueFull()
22+
: super('Push presentation export queue is full (8 outstanding exports)');
23+
}
24+
1825
/// The latest best-effort App Group presentation-cache failure.
1926
final pushPresentationCacheError = ValueNotifier<String?>(null);
2027

@@ -106,9 +113,7 @@ Future<void> _serializePresentationExport(
106113
Future<void> Function() export,
107114
) async {
108115
if (_outstandingPresentationExports >= _maximumPresentationExports) {
109-
throw StateError(
110-
'Push presentation export queue is full (8 outstanding exports)',
111-
);
116+
throw PushPresentationExportQueueFull();
112117
}
113118
_outstandingPresentationExports++;
114119
final previous = _presentationExportTail;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import 'package:flutter/foundation.dart';
2+
3+
import 'push_presentation_cache.dart';
4+
5+
/// Latest terminal export failure, retained across unrelated successful writes.
6+
///
7+
/// Unlike [pushPresentationCacheError], this records an export that could not
8+
/// reach native storage. Retries are bounded and do not guarantee delivery.
9+
final pushPresentationExportError = ValueNotifier<String?>(null);
10+
11+
/// Handles detached exports with one retained retry snapshot per producer.
12+
///
13+
/// Queue saturation retries the exact operation five times over 7.75 seconds.
14+
/// A second saturated operation while recovery is occupied, exhausted retries,
15+
/// or a worker failure returns false and records a terminal error. No extra
16+
/// queue or retry loop is created, and unverified candidates are never merged.
17+
class PushPresentationExportRecovery {
18+
bool _retrying = false;
19+
20+
/// Exports once, recovering transient saturation without detached errors.
21+
Future<bool> export(Future<void> Function() operation) async {
22+
try {
23+
await operation();
24+
return true;
25+
} on PushPresentationExportQueueFull catch (error, stack) {
26+
if (_retrying) return _failed(error, stack);
27+
} catch (error, stack) {
28+
return _failed(error, stack);
29+
}
30+
31+
_retrying = true;
32+
try {
33+
for (final milliseconds in [250, 500, 1000, 2000, 4000]) {
34+
await Future<void>.delayed(Duration(milliseconds: milliseconds));
35+
try {
36+
await operation();
37+
return true;
38+
} on PushPresentationExportQueueFull catch (error, stack) {
39+
if (milliseconds == 4000) return _failed(error, stack);
40+
} catch (error, stack) {
41+
return _failed(error, stack);
42+
}
43+
}
44+
return false;
45+
} finally {
46+
_retrying = false;
47+
}
48+
}
49+
50+
bool _failed(Object error, StackTrace stack) {
51+
pushPresentationExportError.value = error.toString();
52+
debugPrint('Push presentation export could not be delivered: $error');
53+
debugPrintStack(stackTrace: stack);
54+
return false;
55+
}
56+
}

0 commit comments

Comments
 (0)