Skip to content

Commit 36fa0fe

Browse files
browsquare-tombstudio
authored
perf(mobile): subscribe to presence only for people on screen (#9444)
The mobile app subscribed to every presence event in the community, though it only shows presence for people on screen. It now subscribes only to the people it tracks. ### Validation **Impact:** on an iPhone 12 mini signed in to Block's Builderlab, 5 minutes idle on Messages: | | Presence downloaded | All WebSocket traffic | | --- | --- | --- | | Before | 2.9 MB (8153 events) | 3.1 MB | | After | 3.6 KB (10 events) | 66 KB | That's about 35 MB per hour saved at Builderlab's presence rate (about 27 events per second). Measured on an instrumented profile build of the first revision, [3f789d1](3f789d1). The later swap fix doesn't change what is subscribed. **Device runs (automated, profile builds on the 12 mini):** - First revision: cold start, opening and leaving the home list, a Control Center pull, and a resume after 60 s in the background. Presence dots kept updating for people on screen. - Final head [9844c43](9844c438dd): cold start, opening two channels, a 60 s background resume, and scrolling the home list through the DM section. Every DM row showed its presence dot (online and away), including rows newly scrolled into view. Opening a DM and its details sheet worked as before. **Added tests:** - Swapping subscriptions: a late replacement is promoted without clearing anyone's dot and re-reads the newly covered people; a retryable close or an outright failure keeps the current stream; a second track while one is opening doesn't open a third subscription. Each guard was removed in turn and the test failed each time. - A widening that keeps failing is retried each poll, and expired leases are still corrected. - Replacements the relay never answers are dropped and retried, and after the current stream also closes, the next poll covers and reads everyone. - A first subscription still retrying when it opens waits for its ready, then reads everyone and widens. **Updated test:** the filter test now expects no subscription while nobody is tracked, then `authors: [alice]`, then `[alice, bob]`. The full mobile test suite passes. ### How it works - The live subscription filters by `authors`: everyone shown with presence this session. The cache never untracks; it resets on community or account change. Nothing is subscribed while nobody is tracked. - When someone new is tracked, a replacement subscription opens for the wider set. The current one keeps delivering until the replacement's first EOSE, then it's swapped in and the old one closed. Newly covered people get a fresh snapshot at that point. - If the replacement fails or closes first, it's dropped and nothing on screen changes. The 60-second poll retries the widening and keeps reading the people already covered. - One replacement opens at a time; people tracked meanwhile join the next round. ### Regressions - A person tracked mid-session gets live presence after one extra subscription round trip (up to about a second on a slow link). People already on screen keep live presence throughout the swap. - Each widening resends the full tracked list (about 67 bytes per person), so a few hundred people cost tens of KB per widening, against 35 MB per hour saved. The relay indexes `authors` filters up to 4096 authors, so this is cheap for it. --------- Signed-off-by: Tom Brow <[email protected]> Co-authored-by: Tom Brow <[email protected]> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
1 parent 359839f commit 36fa0fe

2 files changed

Lines changed: 448 additions & 33 deletions

File tree

‎mobile/lib/features/profile/presence_cache_provider.dart‎

Lines changed: 149 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,18 @@ class PresenceCacheNotifier extends Notifier<Map<String, String>> {
2727
final Map<String, int> _timestamps = {};
2828
final Map<String, int> _snapshotAt = {};
2929
final List<String> _seenIds = [];
30+
// Keys the live subscription asks for. Only tracked keys are subscribed:
31+
// a community's presence heartbeats arrive many times a second.
32+
final Set<String> _subscribedKeys = {};
3033
void Function()? _presenceUnsub;
34+
// Drops the replacement being opened, until it is promoted or abandoned.
35+
void Function()? _abandonPending;
3136
Timer? _poll;
3237
Timer? _queued;
38+
Timer? _resubscribe;
39+
int _nextSubscription = 0;
40+
int _activeSubscription = 0;
41+
bool _subscribeAttempted = false;
3342
int _generation = 0;
3443
int _epoch = 0;
3544
bool _querying = false;
@@ -53,23 +62,36 @@ class PresenceCacheNotifier extends Notifier<Map<String, String>> {
5362
_timestamps.clear();
5463
_snapshotAt.clear();
5564
_seenIds.clear();
65+
_subscribedKeys.clear();
66+
_activeSubscription = 0;
67+
_subscribeAttempted = false;
5668
_pending
5769
..clear()
5870
..addAll(_tracked);
5971
ref.onDispose(() {
6072
_generation++;
6173
_presenceUnsub?.call();
6274
_presenceUnsub = null;
75+
_abandonPending?.call();
76+
_abandonPending = null;
6377
_poll?.cancel();
6478
_queued?.cancel();
6579
_queued = null;
80+
_resubscribe?.cancel();
81+
_resubscribe = null;
6682
});
6783
if (sessionState.status == SessionStatus.connected &&
6884
lifecycle == AppLifecycleState.resumed) {
6985
_poll = Timer.periodic(const Duration(seconds: 60), (_) {
70-
if (_presenceUnsub == null) {
86+
// A replacement the relay never answered must not hold the slot, so a
87+
// lost REQ costs at most one poll interval.
88+
_abandonPending?.call();
89+
if (_presenceUnsub == null || !_subscribedKeys.containsAll(_tracked)) {
7190
unawaited(_subscribe(generation));
72-
} else if (!_querying) {
91+
}
92+
// The snapshot backstop runs for a live stream even while a widening
93+
// keeps failing, so expired leases are still corrected.
94+
if (_presenceUnsub != null && !_querying) {
7395
_pending.addAll(_tracked);
7496
_schedule(generation);
7597
}
@@ -82,10 +104,32 @@ class PresenceCacheNotifier extends Notifier<Map<String, String>> {
82104

83105
/// Request initial presence for exact keys. Repeated render calls coalesce.
84106
void track(List<String> pubkeys) {
107+
var added = false;
85108
for (final key in pubkeys.map((key) => key.trim().toLowerCase())) {
86-
if (key.isNotEmpty && _tracked.add(key)) _pending.add(key);
109+
if (key.isNotEmpty && _tracked.add(key)) {
110+
_pending.add(key);
111+
added = true;
112+
}
113+
}
114+
// While live (the poll runs only then), a new key needs a subscription
115+
// that covers it. A snapshot read before that one opens can miss a change,
116+
// so the key is read again once it is covered.
117+
if (added && (_poll?.isActive ?? false)) {
118+
_queueResubscribe(_generation);
119+
} else {
120+
_schedule(_generation);
87121
}
88-
_schedule(_generation);
122+
}
123+
124+
void _queueResubscribe(int generation) {
125+
_resubscribe ??= Timer(Duration.zero, () {
126+
_resubscribe = null;
127+
// Without a subscription, the next poll opens one with every key.
128+
if (generation == _generation &&
129+
(_presenceUnsub != null || !_subscribeAttempted)) {
130+
unawaited(_subscribe(generation));
131+
}
132+
});
89133
}
90134

91135
void _schedule(int generation) {
@@ -105,47 +149,133 @@ class PresenceCacheNotifier extends Notifier<Map<String, String>> {
105149
}
106150

107151
Future<void> _subscribe(int generation) async {
108-
if (_opening || generation != _generation) return;
152+
if (_opening ||
153+
generation != _generation ||
154+
_tracked.isEmpty ||
155+
(_presenceUnsub != null && _subscribedKeys.containsAll(_tracked))) {
156+
return;
157+
}
158+
// One subscription opens at a time. A replacement holds this until it is
159+
// promoted or abandoned, so tracking bursts widen it in later rounds.
109160
_opening = true;
110-
_retrying = false;
161+
_subscribeAttempted = true;
162+
final keys = _tracked.toList()..sort();
163+
final subscription = ++_nextSubscription;
164+
// A replacement becomes active only on its own first ready: returning from
165+
// subscribeWithStatus also happens on a retryable CLOSED or a timeout.
166+
// Until then the active subscription and the cache stay as they are.
167+
final replacing = _presenceUnsub != null;
168+
var pending = replacing;
111169
var closed = false;
170+
// Status the first subscription reports before it becomes active.
171+
var retrying = false;
172+
void Function()? unsub;
173+
void close() {
174+
if (closed) return;
175+
closed = true;
176+
unsub?.call(); // otherwise released when subscribeWithStatus returns
177+
}
178+
179+
bool current() =>
180+
generation == _generation &&
181+
!closed &&
182+
subscription == _activeSubscription;
183+
void promote() {
184+
pending = false;
185+
_abandonPending = null;
186+
_opening = false;
187+
final previous = _presenceUnsub;
188+
final added = keys.where((key) => !_subscribedKeys.contains(key));
189+
_pending.addAll(added);
190+
_activeSubscription = subscription;
191+
_retrying = retrying;
192+
_presenceUnsub = close;
193+
_subscribedKeys
194+
..clear()
195+
..addAll(keys);
196+
previous?.call();
197+
// Still backing off: its ready reads everything once it is live.
198+
if (_retrying) {
199+
_invalidateSnapshot();
200+
return;
201+
}
202+
// Keys tracked while this one opened need a wider subscription.
203+
if (!_subscribedKeys.containsAll(_tracked)) _queueResubscribe(generation);
204+
// Newly covered keys are read only now, so no change between an earlier
205+
// snapshot and their live coverage is lost.
206+
_schedule(generation);
207+
}
208+
209+
void abandon() {
210+
pending = false;
211+
_abandonPending = null;
212+
close();
213+
if (generation != _generation) return;
214+
_opening = false;
215+
// Keys only it would cover get a snapshot; the poll retries the widening.
216+
_schedule(generation);
217+
}
218+
219+
if (pending) _abandonPending = abandon;
112220
try {
113-
final unsub = await ref
221+
unsub = await ref
114222
.read(relaySessionProvider.notifier)
115223
.subscribeWithStatus(
116-
const NostrFilter(kinds: [EventKind.presenceUpdate], limit: 0),
224+
NostrFilter(
225+
kinds: const [EventKind.presenceUpdate],
226+
authors: keys,
227+
limit: 0,
228+
),
117229
(event) {
118-
if (generation == _generation && !closed && !_retrying) {
119-
_handlePresenceEvent(event);
120-
}
230+
if (current() && !_retrying) _handlePresenceEvent(event);
121231
},
122232
onStatusChanged: (status) {
123233
if (generation != _generation || closed) return;
234+
if (pending) {
235+
status == RelaySubscriptionStatus.ready ? promote() : abandon();
236+
return;
237+
}
238+
if (subscription != _activeSubscription) {
239+
retrying = status == RelaySubscriptionStatus.retrying;
240+
return;
241+
}
124242
_retrying = status == RelaySubscriptionStatus.retrying;
125243
_invalidateSnapshot();
126-
if (!_retrying) _schedule(generation);
244+
if (_retrying) return;
245+
// Keys tracked during the backoff need a wider subscription.
246+
if (!_subscribedKeys.containsAll(_tracked)) {
247+
_queueResubscribe(generation);
248+
}
249+
_schedule(generation);
127250
},
128251
onClosed: (_) {
129252
if (generation != _generation || closed) return;
253+
if (pending) {
254+
abandon();
255+
return;
256+
}
130257
closed = true;
131-
_presenceUnsub?.call();
258+
if (subscription != _activeSubscription) return;
132259
_presenceUnsub = null;
260+
_subscribedKeys.clear();
133261
_invalidateSnapshot();
134262
},
135263
);
136264
if (generation != _generation || closed) {
137265
unsub();
138266
return;
139267
}
140-
_presenceUnsub = unsub;
141-
_schedule(generation);
268+
// With nothing to protect, the first subscription is active once open.
269+
if (!replacing) promote();
142270
} catch (error) {
143-
if (generation == _generation) {
271+
if (generation != _generation) return;
272+
debugPrint('[PresenceCacheNotifier] subscription failed: $error');
273+
if (replacing) {
274+
if (pending) abandon();
275+
} else if (subscription != _activeSubscription) {
276+
_opening = false;
144277
_invalidateSnapshot();
145-
debugPrint('[PresenceCacheNotifier] subscription failed: $error');
146278
}
147-
} finally {
148-
if (generation == _generation) _opening = false;
149279
}
150280
}
151281

0 commit comments

Comments
 (0)