This commit is contained in:
nielsandriesse 2020-06-12 14:36:21 +10:00
parent 1b45a50fe2
commit 6533af8ba9
6 changed files with 30 additions and 94 deletions

View File

@ -4,55 +4,14 @@ public extension LokiAPI {
fileprivate static let seedNodePool: Set<String> = [ "https://storage.seed1.loki.network", "https://storage.seed3.loki.network", "https://public.loki.foundation" ]
internal static var _snodeFailureCount: [LokiAPITarget:UInt] = [:]
internal static var snodeFailureCount: [LokiAPITarget:UInt] {
get {
let (promise, seal) = Promise<[LokiAPITarget:UInt]>.pending()
stateQueue.async {
seal.fulfill(_snodeFailureCount)
}
return try! promise.wait()
}
set {
LokiAPI.stateQueue.async {
_snodeFailureCount = newValue
}
}
}
/// - Note: Should only be accessed from `LokiAPI.workQueue` to avoid race conditions.
internal static var snodeFailureCount: [LokiAPITarget:UInt] = [:]
// TODO: Read/write this directly from/to the database
internal static var _snodePool: Set<LokiAPITarget> = []
internal static var snodePool: Set<LokiAPITarget> {
get {
let (promise, seal) = Promise<Set<LokiAPITarget>>.pending()
stateQueue.async {
seal.fulfill(_snodePool)
}
return try! promise.wait()
}
set {
LokiAPI.stateQueue.async {
_snodePool = newValue
}
}
}
/// - Note: Should only be accessed from `LokiAPI.workQueue` to avoid race conditions.
internal static var snodePool: Set<LokiAPITarget> = []
// TODO: Read/write this directly from/to the database
internal static var _swarmCache: [String:[LokiAPITarget]] = [:]
internal static var swarmCache: [String:[LokiAPITarget]] {
get {
let (promise, seal) = Promise<[String:[LokiAPITarget]]>.pending()
stateQueue.async {
seal.fulfill(_swarmCache)
}
return try! promise.wait()
}
set {
LokiAPI.stateQueue.async {
_swarmCache = newValue
}
}
}
/// - Note: Should only be accessed from `LokiAPI.workQueue` to avoid race conditions.
internal static var swarmCache: [String:[LokiAPITarget]] = [:]
// MARK: Settings
private static let minimumSnodePoolCount = 32

View File

@ -2,7 +2,7 @@ import PromiseKit
@objc(LKAPI)
public final class LokiAPI : NSObject {
internal static let stateQueue = DispatchQueue(label: "LokiAPI.stateQueue", qos: .userInitiated)
internal static let workQueue = DispatchQueue(label: "LokiAPI.workQueue", qos: .userInitiated)
internal static var storage: OWSPrimaryStorage { OWSPrimaryStorage.shared() }

View File

@ -3,22 +3,10 @@ import PromiseKit
@objc(LKMentionsManager)
public final class MentionsManager : NSObject {
@objc public static var _userPublicKeyCache: [String:Set<String>] = [:]
/// A mapping from thread ID to set of user hex encoded public keys.
@objc public static var userPublicKeyCache: [String:Set<String>] {
get {
let (promise, seal) = Promise<[String:Set<String>]>.pending()
LokiAPI.stateQueue.async {
seal.fulfill(_userPublicKeyCache)
}
return try! promise.wait()
}
set {
LokiAPI.stateQueue.async {
_userPublicKeyCache = newValue
}
}
}
///
/// - Note: Should only be accessed from the main queue to avoid race conditions.
@objc public static var userPublicKeyCache: [String:Set<String>] = [:]
internal static var storage: OWSPrimaryStorage { OWSPrimaryStorage.shared() }

View File

@ -15,22 +15,10 @@ import PromiseKit
@objc(LKMultiDeviceProtocol)
public final class MultiDeviceProtocol : NSObject {
public static var _lastDeviceLinkUpdate: [String:Date] = [:]
/// A mapping from hex encoded public key to date updated.
public static var lastDeviceLinkUpdate: [String:Date] {
get {
let (promise, seal) = Promise<[String:Date]>.pending()
LokiAPI.stateQueue.async {
seal.fulfill(_lastDeviceLinkUpdate)
}
return try! promise.wait()
}
set {
LokiAPI.stateQueue.async {
_lastDeviceLinkUpdate = newValue
}
}
}
///
/// - Note: Should only be accessed from `LokiAPI.workQueue` to avoid race conditions.
public static var lastDeviceLinkUpdate: [String:Date] = [:]
internal static var storage: OWSPrimaryStorage { OWSPrimaryStorage.shared() }

View File

@ -3,74 +3,74 @@ import PromiseKit
public extension Thenable {
func then2<U>(_ body: @escaping (T) throws -> U) -> Promise<U.T> where U : Thenable {
return then(on: DispatchQueue.global(qos: .default), body)
return then(on: LokiAPI.workQueue, body)
}
func map2<U>(_ transform: @escaping (T) throws -> U) -> Promise<U> {
return map(on: DispatchQueue.global(qos: .default), transform)
return map(on: LokiAPI.workQueue, transform)
}
func done2(_ body: @escaping (T) throws -> Void) -> Promise<Void> {
return done(on: DispatchQueue.global(qos: .default), body)
return done(on: LokiAPI.workQueue, body)
}
func get2(_ body: @escaping (T) throws -> Void) -> Promise<T> {
return get(on: DispatchQueue.global(qos: .default), body)
return get(on: LokiAPI.workQueue, body)
}
}
public extension Thenable where T: Sequence {
func mapValues2<U>(_ transform: @escaping (T.Iterator.Element) throws -> U) -> Promise<[U]> {
return mapValues(on: DispatchQueue.global(qos: .default), transform)
return mapValues(on: LokiAPI.workQueue, transform)
}
}
public extension Guarantee {
func then2<U>(_ body: @escaping (T) -> Guarantee<U>) -> Guarantee<U> {
return then(on: DispatchQueue.global(qos: .default), body)
return then(on: LokiAPI.workQueue, body)
}
func map2<U>(_ body: @escaping (T) -> U) -> Guarantee<U> {
return map(on: DispatchQueue.global(qos: .default), body)
return map(on: LokiAPI.workQueue, body)
}
func done2(_ body: @escaping (T) -> Void) -> Guarantee<Void> {
return done(on: DispatchQueue.global(qos: .default), body)
return done(on: LokiAPI.workQueue, body)
}
func get2(_ body: @escaping (T) -> Void) -> Guarantee<T> {
return get(on: DispatchQueue.global(qos: .default), body)
return get(on: LokiAPI.workQueue, body)
}
}
public extension CatchMixin {
func catch2(_ body: @escaping (Error) -> Void) -> PMKFinalizer {
return self.catch(on: DispatchQueue.global(qos: .default), body)
return self.catch(on: LokiAPI.workQueue, body)
}
func recover2<U: Thenable>(_ body: @escaping(Error) throws -> U) -> Promise<T> where U.T == T {
return recover(on: DispatchQueue.global(qos: .default), body)
return recover(on: LokiAPI.workQueue, body)
}
func recover2(_ body: @escaping(Error) -> Guarantee<T>) -> Guarantee<T> {
return recover(on: DispatchQueue.global(qos: .default), body)
return recover(on: LokiAPI.workQueue, body)
}
func ensure2(_ body: @escaping () -> Void) -> Promise<T> {
return ensure(on: DispatchQueue.global(qos: .default), body)
return ensure(on: LokiAPI.workQueue, body)
}
}
public extension CatchMixin where T == Void {
func recover2(_ body: @escaping(Error) -> Void) -> Guarantee<Void> {
return recover(on: DispatchQueue.global(qos: .default), body)
return recover(on: LokiAPI.workQueue, body)
}
func recover2(_ body: @escaping(Error) throws -> Void) -> Promise<Void> {
return recover(on: DispatchQueue.global(qos: .default), body)
return recover(on: LokiAPI.workQueue, body)
}
}

View File

@ -23,7 +23,8 @@ NS_ASSUME_NONNULL_BEGIN
static dispatch_once_t onceToken;
static dispatch_queue_t queue;
dispatch_once(&onceToken, ^{
queue = dispatch_queue_create("org.whispersystems.signal.sendQueue", NULL);
dispatch_queue_attr_t attributes = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, 0);
queue = dispatch_queue_create("org.whispersystems.signal.sendQueue", attributes);
});
return queue;
}