Skip to content

Commit

Permalink
Merge pull request #1313 from planetary-social/fix-too-many-concurren…
Browse files Browse the repository at this point in the history
…t-reqs

Lower concurrent queue limit in RelaySubscriptionManager
  • Loading branch information
mplorentz authored Jul 18, 2024
2 parents c5da9cd + a89fc4b commit 9fb77a7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
10 changes: 8 additions & 2 deletions Nos/Service/Analytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,14 @@ class Analytics {

// MARK: - Relays

func rateLimited(by socket: WebSocket) {
track("Rate Limited", properties: ["relay": socket.request.url?.absoluteString ?? "null"])
func rateLimited(by socket: WebSocket, requestCount: Int) {
track(
"Rate Limited",
properties: [
"relay": socket.request.url?.absoluteString ?? "null",
"count": requestCount,
]
)
}

func badRequest(from socket: WebSocket, message: String) {
Expand Down
11 changes: 9 additions & 2 deletions Nos/Service/Relay/RelayService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,15 @@ extension RelayService {
let response = responseArray.description
Log.debug("Notice from \(socket.host): \(response)")
if let notice = responseArray[safe: 1] as? String {
if notice == "rate limited" {
analytics.rateLimited(by: socket)
if notice == "rate limited" || notice == "ERROR: too many concurrent REQs" {
Task {
let numberOfRequests = await subscriptionManager.active()
.filter { subscription in
subscription.relayAddress == socket.url
}
.count
analytics.rateLimited(by: socket, requestCount: numberOfRequests)
}
} else if notice.contains("bad req:") {
analytics.badRequest(from: socket, message: response)
}
Expand Down
16 changes: 11 additions & 5 deletions Nos/Service/Relay/RelaySubscriptionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ actor RelaySubscriptionManagerActor: RelaySubscriptionManager {
}

/// Limit of the number of active subscriptions in a single relay
private let queueLimit = 25
private let queueLimit = 10

// MARK: - Protocol conformance

Expand Down Expand Up @@ -207,10 +207,16 @@ actor RelaySubscriptionManagerActor: RelaySubscriptionManager {
}

#if DEBUG
let allCount = all.count
let activeCount = active.count
if allCount > activeCount {
Log.debug("\(allCount - activeCount) subscriptions waiting in queue.")
// Print number of waiting subscriptions for each relay
if all.count > active.count {
var waitingSubscriptionsByRelay = [URL: Int]()
for subscription in all where subscription.subscriptionStartDate == nil {
let count = waitingSubscriptionsByRelay[subscription.relayAddress] ?? 0
waitingSubscriptionsByRelay[subscription.relayAddress] = count + 1
}
for (relayAddress, count) in waitingSubscriptionsByRelay {
Log.debug("\(relayAddress) has \(count) subscriptions waiting in queue.")
}
}
#endif
}
Expand Down

0 comments on commit 9fb77a7

Please sign in to comment.