Makes sure Filters are not updated if the since is the only change

This commit is contained in:
Vitor Pamplona
2025-07-07 13:37:57 -04:00
parent 28d182cc55
commit cf48516602
44 changed files with 397 additions and 525 deletions
@@ -20,70 +20,34 @@
*/
package com.vitorpamplona.ammolite.relays.datasources
import com.vitorpamplona.quartz.nip01Core.relay.client.pool.RelayBasedFilter
import com.vitorpamplona.quartz.nip01Core.relay.client.single.newSubId
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import kotlin.collections.forEachIndexed
import kotlin.contracts.ExperimentalContracts
data class Subscription(
val id: String = newSubId(),
val onEose: ((time: Long, relayUrl: NormalizedRelayUrl) -> Unit)? = null,
) {
var relayBasedFilters: List<RelayBasedFilter>? = null // Inactive when null
private var filters: Map<NormalizedRelayUrl, List<Filter>>? = null // Inactive when null
fun reset() {
relayBasedFilters = null
filters = null
}
fun updateFilters(newFilters: Map<NormalizedRelayUrl, List<Filter>>?) {
filters = newFilters
}
fun filters() = filters
@OptIn(ExperimentalContracts::class)
fun isActive() = filters != null
fun callEose(
time: Long,
relay: NormalizedRelayUrl,
) {
onEose?.let { it(time, relay) }
}
fun hasChangedFiltersFrom(otherFilters: List<RelayBasedFilter>?): Boolean {
if (relayBasedFilters == null && otherFilters == null) return false
if (relayBasedFilters?.size != otherFilters?.size) return true
relayBasedFilters?.forEachIndexed { index, relaySetFilter ->
val otherFilter = otherFilters?.getOrNull(index) ?: return true
if (relaySetFilter.relay != otherFilter.relay) return true
return isDifferent(relaySetFilter.filter, otherFilter.filter)
}
return false
}
fun isDifferent(
filter1: Filter,
filter2: Filter,
): Boolean {
// Does not check SINCE on purpose. Avoids replacing the filter if SINCE was all that changed.
// fast check
if (filter1.authors?.size != filter2.authors?.size ||
filter1.ids?.size != filter2.ids?.size ||
filter1.tags?.size != filter2.tags?.size ||
filter1.kinds?.size != filter2.kinds?.size ||
filter1.limit != filter2.limit ||
filter1.search?.length != filter2.search?.length ||
filter1.until != filter2.until
) {
return true
}
// deep check
if (filter1.ids != filter2.ids ||
filter1.authors != filter2.authors ||
filter1.tags != filter2.tags ||
filter1.kinds != filter2.kinds ||
filter1.search != filter2.search
) {
return true
}
return false
}
}
@@ -20,16 +20,13 @@
*/
package com.vitorpamplona.ammolite.relays.datasources
import com.vitorpamplona.ammolite.relays.BundledUpdate
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient
import com.vitorpamplona.quartz.nip01Core.relay.client.listeners.IRelayClientListener
import com.vitorpamplona.quartz.nip01Core.relay.client.pool.RelayBasedFilter
import com.vitorpamplona.quartz.nip01Core.relay.client.single.IRelayClient
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import java.util.concurrent.atomic.AtomicBoolean
import com.vitorpamplona.quartz.utils.LargeCache
/**
* Semantically groups Nostr filters and subscriptions in data source objects that
@@ -37,13 +34,9 @@ import java.util.concurrent.atomic.AtomicBoolean
*/
class SubscriptionController(
val client: NostrClient,
val updateSubscriptions: () -> Unit,
) : SubscriptionControllerService {
private val subscriptions = SubscriptionSet()
private var active: Boolean = false
private val changingFilters = AtomicBoolean()
val stats = SubscriptionStats()
) {
private val subscriptions = LargeCache<String, Subscription>()
private val stats = SubscriptionStats()
private val clientListener =
object : IRelayClientListener {
@@ -54,10 +47,10 @@ class SubscriptionController(
arrivalTime: Long,
afterEOSE: Boolean,
) {
if (subscriptions.contains(subId)) {
if (subscriptions.containsKey(subId)) {
stats.add(subId, event.kind)
if (afterEOSE) {
runAfterEOSE(subId, relay, arrivalTime)
subscriptions.get(subId)?.callEose(arrivalTime, relay.url)
}
}
}
@@ -67,145 +60,63 @@ class SubscriptionController(
subId: String,
arrivalTime: Long,
) {
if (subscriptions.contains(subId)) {
runAfterEOSE(subId, relay, arrivalTime)
if (subscriptions.containsKey(subId)) {
subscriptions.get(subId)?.callEose(arrivalTime, relay.url)
}
}
}
private fun runAfterEOSE(
subscriptionId: String,
relay: IRelayClient,
arrivalTime: Long,
) {
subscriptions[subscriptionId]?.callEose(arrivalTime, relay.url)
}
init {
client.subscribe(clientListener)
}
override fun destroy() {
// makes sure to run
stop()
fun destroy() {
client.unsubscribe(clientListener)
bundler.cancel()
}
override fun start() {
active = true
invalidateFilters()
}
@OptIn(DelicateCoroutinesApi::class)
override fun stop() {
active = false
subscriptions.forEach { subscription ->
client.close(subscription.id)
subscription.reset()
}
}
override fun printStats(tag: String) = stats.printCounter(tag)
fun printStats(tag: String) = stats.printCounter(tag)
fun getSub(subId: String) = subscriptions.get(subId)
fun requestNewSubscription(onEOSE: ((Long, NormalizedRelayUrl) -> Unit)? = null): Subscription = subscriptions.newSub(onEOSE)
fun requestNewSubscription(onEOSE: ((Long, NormalizedRelayUrl) -> Unit)? = null): Subscription = Subscription(onEose = onEOSE).also { subscriptions.put(it.id, it) }
fun dismissSubscription(subId: String) = getSub(subId)?.let { dismissSubscription(it) }
fun dismissSubscription(subscription: Subscription) {
client.close(subscription.id)
subscription.reset()
subscriptions.remove(subscription)
subscriptions.remove(subscription.id)
}
fun isUpdatingFilters() = changingFilters.get()
// Refreshes observers in batches.
private val bundler = BundledUpdate(300, Dispatchers.Default)
override fun invalidateFilters() {
bundler.invalidate {
// println("DataSource: ${this.javaClass.simpleName} InvalidateFilters")
// adds the time to perform the refresh into this delay
// holding off new updates in case of heavy refresh routines.
resetFiltersSuspend()
}
}
private fun resetFiltersSuspend() {
// only runs one at a time. Ignores the others
if (changingFilters.compareAndSet(false, true)) {
try {
resetFiltersSuspendInner()
} finally {
changingFilters.getAndSet(false)
fun updateRelays() {
val currentFilters =
subscriptions.associateWith { id, sub ->
client.getSubscriptionFiltersOrNull(id)
}
}
}
private fun resetFiltersSuspendInner() {
// saves the channels that are currently active
val activeSubscriptions = subscriptions.actives()
// saves the current content to only update if it changes
val currentFilters = activeSubscriptions.associate { it.id to client.getSubscriptionFiltersOrNull(it.id) }
// updates all filters
updateSubscriptions()
// Makes sure to only send an updated filter when it actually changes.
subscriptions.forEach { newSubscriptionFilters ->
val currentFilters = currentFilters[newSubscriptionFilters.id]
updateRelaysIfNeeded(newSubscriptionFilters, currentFilters)
subscriptions.forEach { id, sub ->
updateRelaysIfNeeded(id, sub.filters(), currentFilters[id])
}
}
fun updateRelaysIfNeeded(
updatedSubscription: Subscription,
currentFilters: List<RelayBasedFilter>?,
subId: String,
updatedFilters: Map<NormalizedRelayUrl, List<Filter>>?,
currentFilters: Map<NormalizedRelayUrl, List<Filter>>?,
) {
val updatedSubscriptionNewFilters = updatedSubscription.relayBasedFilters
val isActive = client.isActive(updatedSubscription.id)
if (!isActive && updatedSubscriptionNewFilters != null) {
// Filter was removed from the active list
// but it is supposed to be there. Send again.
if (active) {
client.sendFilter(updatedSubscription.id, updatedSubscriptionNewFilters)
if (currentFilters != null) {
if (updatedFilters == null) {
// was active and is not active anymore, just close.
client.close(subId)
} else {
client.sendRequest(subId, updatedFilters)
}
} else {
if (currentFilters != null) {
if (updatedSubscriptionNewFilters == null) {
// was active and is not active anymore, just close.
client.close(updatedSubscription.id)
} else {
// was active and is still active, check if it has changed.
if (updatedSubscription.hasChangedFiltersFrom(currentFilters)) {
client.close(updatedSubscription.id)
if (active) {
client.sendFilter(updatedSubscription.id, updatedSubscriptionNewFilters)
}
} else {
// hasn't changed, does nothing.
// unless the relay has disconnected, then reconnect.
if (active) {
client.sendFilterOnlyIfDisconnected(updatedSubscription.id, updatedSubscriptionNewFilters)
}
}
}
if (updatedFilters == null) {
// was not active and is still not active, does nothing
} else {
if (updatedSubscriptionNewFilters == null) {
// was not active and is still not active, does nothing
} else {
// was not active and becomes active, sends the filter.
if (active) {
client.sendFilter(updatedSubscription.id, updatedSubscriptionNewFilters)
}
}
// was not active and becomes active, sends the entire filter.
client.sendRequest(subId, updatedFilters)
}
}
}
@@ -1,33 +0,0 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.ammolite.relays.datasources
interface SubscriptionControllerService {
fun start()
fun stop()
fun invalidateFilters()
fun destroy()
fun printStats(tag: String)
}
@@ -1,47 +0,0 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.ammolite.relays.datasources
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
class SubscriptionSet {
private var subscriptions = mapOf<String, Subscription>()
fun contains(subId: String) = subscriptions.containsKey(subId)
fun add(sub: Subscription) {
subscriptions = subscriptions + Pair(sub.id, sub)
}
fun remove(subId: String) {
subscriptions = subscriptions.minus(subId)
}
fun remove(sub: Subscription) = remove(sub.id)
fun newSub(onEOSE: ((Long, NormalizedRelayUrl) -> Unit)? = null): Subscription = Subscription(onEose = onEOSE).also { add(it) }
fun forEach(action: (Subscription) -> Unit) = subscriptions.values.forEach(action)
operator fun get(subId: String) = subscriptions[subId]
fun actives() = subscriptions.values.filter { it.relayBasedFilters != null }
}
@@ -21,17 +21,19 @@
package com.vitorpamplona.ammolite.relays.datasources
import android.util.Log
import com.vitorpamplona.quartz.utils.LargeCache
class SubscriptionStats {
data class Counter(
val subscriptionId: String,
val eventKind: Int,
var counter: Int,
)
) {
var counter: Int = 0
}
private var eventCounter = mapOf<Int, Counter>()
private var eventCounter = LargeCache<Int, Counter>()
fun eventCounterIndex(
private fun eventCounterIndex(
str1: String,
str2: Int,
): Int = 31 * str1.hashCode() + str2.hashCode()
@@ -41,19 +43,15 @@ class SubscriptionStats {
eventKind: Int,
) {
val key = eventCounterIndex(subscriptionId, eventKind)
val keyValue = eventCounter[key]
if (keyValue != null) {
keyValue.counter++
} else {
eventCounter = eventCounter + Pair(key, Counter(subscriptionId, eventKind, 1))
}
val stats = eventCounter.getOrCreate(key) { Counter(subscriptionId, eventKind) }
stats.counter++
}
fun printCounter(tag: String) {
eventCounter.forEach {
eventCounter.forEach { _, stats ->
Log.d(
tag,
"Received Events ${it.value.subscriptionId} ${it.value.eventKind}: ${it.value.counter}",
"Received Events ${stats.subscriptionId} ${stats.eventKind}: ${stats.counter}",
)
}
}