mirror of
https://github.com/oxen-io/session-ios.git
synced 2023-12-13 21:30:14 +01:00
Throttle profile fetch and retry on failure.
// FREEBIE
This commit is contained in:
parent
3c7d47f565
commit
f8f4e5ee6f
1 changed files with 55 additions and 22 deletions
|
@ -14,6 +14,9 @@ class ProfileFetcherJob: NSObject {
|
|||
|
||||
let thread: TSThread
|
||||
|
||||
// This property is only accessed on the default global queue.
|
||||
static var fetchDateMap = [String: Date]()
|
||||
|
||||
public class func run(thread: TSThread, networkManager: TSNetworkManager) {
|
||||
ProfileFetcherJob(thread: thread, networkManager: networkManager).run()
|
||||
}
|
||||
|
@ -26,9 +29,34 @@ class ProfileFetcherJob: NSObject {
|
|||
}
|
||||
|
||||
public func run() {
|
||||
AssertIsOnMainThread()
|
||||
|
||||
DispatchQueue.global().async {
|
||||
for recipientId in self.thread.recipientIdentifiers {
|
||||
self.getProfile(recipientId: recipientId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func getProfile(recipientId: String, remainingRetries: Int = 3) {
|
||||
|
||||
if let lastDate = ProfileFetcherJob.fetchDateMap[recipientId] {
|
||||
let lastTimeInterval = fabs(lastDate.timeIntervalSinceNow)
|
||||
// Don't check a profile more often than every N minutes.
|
||||
let kGetProfileMaxFrequencySeconds = 60.0 * 5.0
|
||||
if lastTimeInterval < kGetProfileMaxFrequencySeconds {
|
||||
Logger.info("\(self.TAG) skipping getProfile: \(recipientId), lastTimeInterval: \(lastTimeInterval)")
|
||||
return
|
||||
}
|
||||
}
|
||||
ProfileFetcherJob.fetchDateMap[recipientId] = Date()
|
||||
|
||||
Logger.error("\(self.TAG) getProfile: \(recipientId)")
|
||||
|
||||
let request = OWSGetProfileRequest(recipientId: recipientId)
|
||||
|
||||
// We don't need to retainUntilComplete() since the success and failure
|
||||
// handlers both close over a strong reference to self.
|
||||
self.networkManager.makeRequest(
|
||||
request,
|
||||
success: { (_: URLSessionDataTask?, responseObject: Any?) -> Void in
|
||||
|
@ -48,9 +76,14 @@ class ProfileFetcherJob: NSObject {
|
|||
}
|
||||
|
||||
Logger.error("\(self.TAG) failed to fetch profile for recipient: \(recipientId) with error: \(error)")
|
||||
})
|
||||
|
||||
if remainingRetries > 1 {
|
||||
DispatchQueue.global().async {
|
||||
self.getProfile(recipientId: recipientId, remainingRetries:remainingRetries - 1)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private func processResponse(signalServiceProfile: SignalServiceProfile) {
|
||||
Logger.debug("\(TAG) in \(#function) for \(signalServiceProfile)")
|
||||
|
|
Loading…
Reference in a new issue