From 7c346e33262225e24d92a7cf565357c68c8404d0 Mon Sep 17 00:00:00 2001 From: greenart7c3 Date: Mon, 23 Feb 2026 13:33:11 -0300 Subject: [PATCH] Improve profile subscription management by closing connections after receiving EOSE from all relays - Track active relays per subscription ID in `ProfileSubscription`. - Automatically close subscriptions and clean up state once an End of Stored Events (EOSE) message is received from all relevant relays. - Ensure `relaysPerSubId` is cleared when the subscription is stopped. --- .../nostrsigner/service/ProfileSubscription.kt | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/service/ProfileSubscription.kt b/app/src/main/java/com/greenart7c3/nostrsigner/service/ProfileSubscription.kt index 40ce933b..0ba96e64 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/service/ProfileSubscription.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/service/ProfileSubscription.kt @@ -47,6 +47,7 @@ class ProfileSubscription( val scope: CoroutineScope, ) : IRelayClientListener { private val subIds = mutableMapOf() + private val relaysPerSubId = mutableMapOf>() init { // listens until the app crashes. @@ -55,6 +56,16 @@ class ProfileSubscription( override fun onIncomingMessage(relay: IRelayClient, msgStr: String, msg: Message) { if (msg is EoseMessage) { + val subId = msg.subId + val relays = relaysPerSubId[subId] + if (relays != null) { + relays.remove(relay.url) + if (relays.isEmpty()) { + client.close(subId) + relaysPerSubId.remove(subId) + } + } + scope.launch { LocalPreferences.allAccounts(appContext).forEach { if (msg.subId == subIds[it.hexKey]) { @@ -111,7 +122,10 @@ class ProfileSubscription( val oneDayAgo = TimeUtils.oneDayAgo() val fifteenMinutesAgo = TimeUtils.fifteenMinutesAgo() if ((lastMetaData == 0L || oneDayAgo > lastMetaData) && (lastCheck == 0L || fifteenMinutesAgo > lastCheck)) { - client.openReqSubscription(subIds[it.hexKey]!!, createProfileFilter(it)) + val subId = subIds[it.hexKey]!! + val profileFilter = createProfileFilter(it) + relaysPerSubId[subId] = profileFilter.keys.toMutableSet() + client.openReqSubscription(subId, profileFilter) } } } @@ -123,6 +137,7 @@ class ProfileSubscription( subIds.values.forEach { client.close(it) } + relaysPerSubId.clear() } private fun createProfileFilter(account: Account): Map> {