Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up unecessary subscriptions in PagedRelaySubscription #1205

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions Nos/Service/Relay/PagedRelaySubscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class PagedRelaySubscription {
private var subscriptionManager: RelaySubscriptionManager

/// A set of subscriptions fetching older events.
private var pagedSubscriptionIDs = [RelaySubscription.ID]()
private var pagedSubscriptionIDs = Set<RelaySubscription.ID>()

/// A set of subscriptions always listening for new events published after the `startDate`.
private var newEventsSubscriptionIDs = [RelaySubscription.ID]()
private var newEventsSubscriptionIDs = Set<RelaySubscription.ID>()

init(
startDate: Date,
Expand All @@ -36,10 +36,10 @@ class PagedRelaySubscription {
var newEventsFilter = filter
newEventsFilter.since = startDate
for relayAddress in relayAddresses {
newEventsSubscriptionIDs.append(
newEventsSubscriptionIDs.insert(
await subscriptionManager.queueSubscription(with: filter, to: relayAddress)
)
pagedSubscriptionIDs.append(
pagedSubscriptionIDs.insert(
await subscriptionManager.queueSubscription(with: pagedEventsFilter, to: relayAddress)
)
}
Expand All @@ -61,6 +61,7 @@ class PagedRelaySubscription {
func loadMore() {
Task { [self] in
var newUntilDates = [URL: Date]()
var subscriptionsToRemove = [RelaySubscription]()

for subscriptionID in pagedSubscriptionIDs {
if let subscription = await subscriptionManager.subscription(from: subscriptionID),
Expand All @@ -73,13 +74,18 @@ class PagedRelaySubscription {

newUntilDates[subscription.relayAddress] = newDate
await subscriptionManager.decrementSubscriptionCount(for: subscriptionID)
subscriptionsToRemove.append(subscription)
}
}

subscriptionsToRemove.forEach { subscription in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like when the solution involves using the right data structure. Given that we are already switching to a Set, how about making subscriptionsToRemove a Set of IDs, so that this line I'm commenting can be just pagedSubscriptionIDs.subtract(subscriptionsToRemove)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah, great idea!

pagedSubscriptionIDs.remove(subscription.id)
}

for (relayAddress, until) in newUntilDates {
var newEventsFilter = self.filter
newEventsFilter.until = until
pagedSubscriptionIDs.append(
pagedSubscriptionIDs.insert(
await subscriptionManager.queueSubscription(with: newEventsFilter, to: relayAddress)
)
}
Expand Down
Loading