From e0da0fd64e4fa441b3978adcb075c0fff0e8de6a Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 19 Jun 2020 12:02:05 +1000 Subject: [PATCH] Spam the work queue a bit less --- SignalServiceKit/src/Loki/API/LokiPoller.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/LokiPoller.swift b/SignalServiceKit/src/Loki/API/LokiPoller.swift index 58fe10165..fac50bd68 100644 --- a/SignalServiceKit/src/Loki/API/LokiPoller.swift +++ b/SignalServiceKit/src/Loki/API/LokiPoller.swift @@ -10,6 +10,7 @@ public final class LokiPoller : NSObject { private var pollCount = 0 // MARK: Settings + private static let pollInterval: TimeInterval = 1 private static let retryInterval: TimeInterval = 0.25 /// After polling a given snode this many times we always switch to a new one. /// @@ -95,7 +96,7 @@ public final class LokiPoller : NSObject { private func poll(_ target: LokiAPITarget, seal longTermSeal: Resolver) -> Promise { guard !hasStopped else { return Promise { $0.fulfill(()) } } - return LokiAPI.getRawMessages(from: target, usingLongPolling: false).then2 { [weak self] rawResponse -> Promise in + return LokiAPI.getRawMessages(from: target, usingLongPolling: false).then(on: DispatchQueue.main) { [weak self] rawResponse -> Promise in guard let strongSelf = self, !strongSelf.hasStopped else { return Promise { $0.fulfill(()) } } let messages = LokiAPI.parseRawMessagesResponse(rawResponse, from: target) strongSelf.onMessagesReceived(messages) @@ -103,7 +104,10 @@ public final class LokiPoller : NSObject { if strongSelf.pollCount == LokiPoller.maxPollCount { throw Error.pollLimitReached } else { - return strongSelf.poll(target, seal: longTermSeal) + return withDelay(LokiPoller.pollInterval, completionQueue: LokiAPI.workQueue) { + guard let strongSelf = self, !strongSelf.hasStopped else { return Promise { $0.fulfill(()) } } + return strongSelf.poll(target, seal: longTermSeal) + } } } }